1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Googleフォームのeventデータを使いやすくする

1
Last updated at Posted at 2023-10-08

使い方

事前に各質問にIDを付与しておいて、そのIDからデータを辿ることができます。


function onFormSubmit(e){

    const data = getInteraction(e);
}

以下のデータ構造を返します。

{
  email : <string>,
  timestamp : <Date>,
  editResponseUrl : <string>,
  questionId1 : {
    name : <string>,
    type : <FormApp.ItemType>,
    title : <string>,
    helptext : <string>,
    item : <FormApp.FormResponse>,
    value : <string | object>
  },
  questionId2 : {...}
}

そのため、次のように書くことができます。

    const data = getInteraction(e);

    //e.response.getItemResponses().filter(q=>q.getTitle() === "質問名").getResponse() と同等
    data["質問名"].value;
    //質問名ではなく、任意のIDで代替可
    data.hogehoge.value; 

    //email,timestamp,編集用URLの取得
    data.email;
    data.timestamp;
    data.editResponseUrl;

    //e.response.getItemResponses().filter(q=>q.getTitle() === "質問名").getItem() と同等
    data["質問名"].item;
    //質問名ではなく、任意のIDで代替可
    data.hogehoge.item; 

補足

GridItemのvalueは、オブジェクトの形式です。 ![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/2953722/c498a47f-7145-3dd0-aae9-e7b242d5d83a.png)
    data.gridItem.value;
    //{"A" : ["1"], "B" : ["1","2"]}

IDの設定

質問の説明欄に、以下の書式で記載します。

書式 : <ID>

Example : <hogehoge>

例)
image.png

IDは正規表現で抜き出しているので、説明文を記載しても問題ありません。
image.png

また、説明の文章に<>を使用しても大丈夫です。一番最後の<>をIDとして判定します。
image.png

ソースコード

function getInteraction(e){

  const res = {

    email : e.response.getRespondentEmail(),
    timestamp : e.response.getTimestamp(),
    editResponseUrl : e.response.getEditResponseUrl()

  };

  for(const itemResponse of e.response.getItemResponses()) {

    const item = itemResponse.getItem();
    const response = itemResponse.getResponse();
    const helpText = item.getHelpText()||null;
    const questionId = helpText.match(/<.*?>/g)?.slice(-1)[0].replace(/<|>/g,"") || item.getTitle();
    res[questionId] = new FormResponseClass(questionId,item,response);

  }
  
  return res;

}


class FormResponseClass{

  constructor(questionId,item,response){

    this.name = questionId;
    this.type = item.getType().toString();
    this.title = item.getTitle();
    this.helptext = item.getHelpText();
    this.item = item;
    this.value = response;

    if(this.isGridItem()){

      this.value = {};
      const rows = this.item.asGridItem().getRows();
      for(let i=rows.length;i--;){

        this.value[rows[i]] = response[i];

      }
      
    }

    if(this.isCheckboxGridItem()){

      this.value = {};
      const rows = this.item.asCheckboxGridItem().getRows();
      for(let i=rows.length;i--;){

        this.value[rows[i]] = response[i];

      }

    }

  }

  isCheckboxGridItem(){

    return this.type === FormApp.ItemType.CHECKBOX_GRID.toString();

  }

  isGridItem(){

    
    return this.type === FormApp.ItemType.GRID.toString();

  }


}
1
2
0

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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?