LoginSignup
5

More than 3 years have passed since last update.

Google Apps ScriptでGoogle Formsの回答を取得する方法

Posted at

はじめに

GASでGoogle Formsのフォームに紐づいていないスクリプトで回答を取得するのに詰まったのでメモ。

なお、フォームに紐づいているスクリプトで回答を取得するのはこちらのページが参考になる。

トリガー設定

Google Formsのトリガー設定(公式ドキュメント)に書いてある通り、
下記スクリプトを、紐づけたい関数があるプロジェクトで実行すれば良い。

ScriptApp.newTrigger('<関数名>')
  .forForm('<フォームID>')
  .onFormSubmit()
  .create();
  • 関数名 はフォームの回答があった時に実行したい関数の名前
  • フォームID はフォームのID
    • フォームの編集画面のURLは https://docs.google.com/forms/d/<フォームID>/edit となっている

回答取得

2パターンあるが、どっちを使用すべきなのか分からないので両方試してほしい。

パターン1

function onSubmit(e){
  Logger.log(e.namedValues['フォームの項目名1'][0]);
  Logger.log(e.namedValues['フォームの項目名2'][0]);
}

のようにする。

パターン2

function onSubmit(e){
  let items = e.response.getItemResponses();
  for (i in items){
    Logger.log("title=%s, response=%s", items[i].getItem().getTitle(), items[i].getResponse());
  }
}

のようにする。

参考

  1. How to get form values in the submit event handler? (stack overflow)
  2. https://memordm.com/googleform/
  3. Google Formsのトリガー設定(公式ドキュメント)

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5