https://developer.cybozu.io/hc/ja/articles/115003856483
の記事を元に、見様見真似で作成したのですが、Kintoneに反映されません。
お知恵を頂けないでしょうか。
~~~
functiongetFormResponse(e){'use strict';varitemResponses=e.response.getItemResponses();// アンケートの回答を取得varrecords='[';records+=Utilities.formatString('{"Email": { "value": "%s" }',e.response.getRespondentEmail());// 回答者のEmailアドレスの取得for(vari=0;i\<itemResponses.length;i++){varitemResponse=itemResponses[i];switch(itemResponse.getItem().getTitle()){case'お名前(フルネーム)':records+=Utilities.formatString(',"文字列\_\_1行\_\_0" : { "value": "%s" }',itemResponse.getResponse());// 質問に対する回答を取得break;case'会社名および部署名(所属)':records+=Utilities.formatString(',"文字列\_\_1行\_\_1" : { "value": "%s" }',itemResponse.getResponse());// 質問に対する回答を取得break;case'掲載(放送)予定媒体':records+=Utilities.formatString(',"文字列\_\_1行\_\_4" : { "value": "%s" }',itemResponse.getResponse());// 質問に対する回答を取得break;}}records+='}]';Logger.log('Response JSON is "%s"',records);returnrecords;}functionsendToKintone(e){'use strict';Logger.log('Form submitted');varsubdomain='{●●●●-pr}.cybozu.com';// サブドメイン名varapps={YOUR\_APPLICATION1:{appid:4,name:'kintone Meetup 参加者',token:'6p2azDdMxxWeBCS1ywfYO23Rse1qZJMNwVL13JU2'}};varmanager=newKintoneManager.KintoneManager(subdomain,apps);// ライブラリーの初期化varstr=getFormResponse(e);str=str.replace(//g,'\').replace(/\r/g,'\\r').replace(/\t/g,'\\t');varrecords=JSON.parse(str);// JSON形式に変換varresponse=manager.create('YOUR\_APPLICATION1',records);// kintone レコードの生成// ステータスコード// 成功すれば200になるvarcode=response.getResponseCode();Logger.log('Response code is "%s"',code);}
小嶋さん、こんにちは。
Kintoneに反映されません。
何かエラーメッセージは出ていますでしょうか?
ログを確認する方法は、次の投稿が参考になると思います。
https://developer.cybozu.io/hc/ja/community/posts/360060227692
sio様
エラー
ReferenceError: manager is not defined
getFormResponse @ コード.gs:3
と出ました。ググっても分からず、、、よろしくお願いいたします。
小嶋 様
ReferenceError: manager is not defined
manager が定義されていない、とのエラーになると思いますが、
記事35行目の通り、manager は定義されていますでしょうか。
varmanager=newKintoneManager.KintoneManager(subdomain,apps);// ライブラリーの初期化
また、念のためですが、subdomainを記載する際に、サンプルコードにあった{}の記載は必要ありません。
var subdomain = '{●●●●-pr}.cybozu.com';// サブドメイン名
function getFormResponse(e)
側に
Logger.log(‘response=> “%s”’, response);
を入れてしまっていたため、function sendToKintone(e) 側(44行目)に入れました。
そうしましたところ、
TypeError: Cannot read property ‘response’ of undefined
getFormResponse @ コード.gs:3
というエラーが表示されております。よろしくお願いします。
小嶋 様
こちらの投稿の通り、記事サンプルコードの39行目の次に
Logger.log(‘response=> “%s”’, response); を追記すると、ログが正常に出力されるかと思います。
varresponse=manager.create("YOUR\_APPLICATION1",records);//kintone レコードの生成
Logger.log('response=\> "%s"', response);
また、最初に共有していただいたコードで試してみましたが、
自分の環境では、kintoneに回答が正常に反映されました。
認証情報やkintoneのフィールドの指定などに、間違いがないかを確認してみると良いかもしれません。
重ねてお手間をおかけし恐縮です。
試したところ、同じエラー(TypeError: Cannot read property ‘response’ of undefined)が出てしまいました。
以下、38~44行目の部分です。
var records = JSON.parse(str);//JSON形式に変換
var response = manager.create(“YOUR_APPLICATION1”, records);//kintone レコードの生成
Logger.log(‘response=> “%s”’, response);
// ステータスコード
// 成功すれば200になる
var code = response.getResponseCode();
Logger.log(‘Response code is “%s”’, code);
よろしくお願いします。
TypeError: Cannot read property ‘response’ of undefined は
undefined
のプロパティ ‘response’ が読み取れないというエラーなので、
Google フォームの回答(e) を取得できていないように見えます。
Google Apps Scriptのエディタから直接コードを実行するのではなく、
記事の「5. プログラムの作成>Step 12」のトリガーを設定した上で、
「6. 動作の確認」の通りにフォームの送信を行ったでしょうか。
遅くなりました。フォームからの送信を行い、GASの左側メニューの「実行数」のところを見たところ、以下の記述がありました。
ご確認いただけますでしょうか(一部伏せ字にしています)。
2022/01/20 16:13:45
情報
Form submitted
2022/01/20 16:13:48
情報
Response JSON is “[{“Email”: { “value”: “●●●●@●●●●.●●.jp” },“文字列__1行__0” : { “value”: “名前テスト” },“文字列__1行__1” : { “value”: “●●テスト” },“文字列__1行__4” : { “value”: “掲載テスト” }}]”
2022/01/20 16:13:48
情報
response=> “{“code”:“CB_VA01”,“id”:“aUPR3W6ajb5nH7RM1Bbx”,“message”:“入力内容が正しくありません。”,“errors”:{“records[0].日付.value”:{“messages”:[“必須です。”]},“records[0].文字列__1行__6.value”:{“messages”:[“必須です。”]}}}”
2022/01/20 16:13:48
情報
Response code is “400.0”
“message”:“入力内容が正しくありません。”,“errors”:{“records[0].日付.value”:{“messages”:[“必須です。”]},“records[0].文字列__1行__6.value”:{“messages”:[“必須です。”]}
エラーメッセージを見る限り、「日付」と「文字列__1行__6」の2つのフィールドはkintone上で必須項目に設定されていますが、
Googleフォームの項目には含まれていないため、必須項目が空の状態でレコードを登録できず、エラーが出ているように見えます。
「日付」と「文字列__1行__6」フィールドの「必須項目にする」チェックを外す、
もしくは上記2つのフィールドを、Googleフォームの必須項目として追加するのはいかがでしょうか?
ありがとうございます。ここのエラーはKintone側のものだったのですね。必須項目を外したところ、登録されました!
ありがとうございました。今回はテストで一部項目のみをGASに書きましたので、日付など残りの項目もフォーム上に追加していきたいと思います。本当にありがとうございました。