如何在 onFormSubmit 中获取 Google 表单数据

huangapple go评论63阅读模式
英文:

How to get google form data at onFormSubmit

问题

  1. 为什么onFormSubmit事件的事件数据中没有任何响应数据(即表单数据)?

  2. 还需要执行其他操作吗(例如安装其他触发器)?

  3. 为了正确的操作,我应该做什么?

(注意:这只是返回翻译好的部分,不包含问题的答案。)

英文:

I want to send some google form data to outside API using UrlFetchApp.fetch when the google form is submitted but there is no form data in event data.

  1. My script is here

     function onFormSubmit(e) {
         Logger.log(JSON.stringfy(e))
    
         UrlFetchApp.fetch('https://api.server.com/result', {
             method: 'POST',
             headers: {
                 'Content-Type': 'application/json',
             },
             payload: JSON.stringify({
               answer1 : e.response.getItemResponses()[0].getResponse(),
               answer2 : e.response.getItemResponses()[1].getResponse()
             }),
         })
     }
    
  2. I add trigger to google form

enter image description here

  1. When google form is submitted, the function, onFormSubmit is called correctly but e has no response data like below log.

     {"authMode":"FULL","response":{},"source":{},"triggerUid":"16459009"}
    

My question is

  1. Why the event of onFormSubmit doesn't have any response data(=form data)?

  2. Is there something to do more (like install other trigger?)

  3. What should I do for correct action?

答案1

得分: 0

以下是您要翻译的内容:

您的脚本容器绑定了Google表单,并且具有一个名为onFormSubmit的触发器,您认为您的日志显示脚本未收集任何表单响应数据。

您的日志结果并不意味着未收集任何响应数据。

事件对象response包含一个FormResponse对象,代表用户对整个表单的响应。

要查看您的表单响应数据,请在您的“stringify”行之后添加以下内容:


let responses = e.response.getItemResponses();
for (i = 0; i<responses.length ; i++){
  Logger.log(responses[i].getResponse())
}

UrlFetchApp.fetch - 负载

建议的修改

payload: JSON.stringify({
           answer1 : e.response.getItemResponses()[0].getResponse(),
           answer2 : e.response.getItemResponses()[1].getResponse()
         }),

payload: answer1 : e.response.getItemResponses()[0].getResponse(),
         answer2 : e.response.getItemResponses()[1].getResponse()
        ,
英文:

You have an onFormSubmit trigger for a script container-bound to a Google Form and you think that your log shows that the script is not collecting any form response data.

Your log results do NOT mean that no response data was collected.

The Event Objects response contains a FormResponse object, representing the user's response to the form as a whole.

To see your form response data, add the following after your "stringify" line:


let responses = e.response.getItemResponses();
for (i = 0; i<responses.length ; i++){
  Logger.log(responses[i].getResponse())
}

UrlFetchApp.fetch - PayLoad

Suggested modification

From

payload: JSON.stringify({
           answer1 : e.response.getItemResponses()[0].getResponse(),
           answer2 : e.response.getItemResponses()[1].getResponse()
         }),

To

payload: answer1 : e.response.getItemResponses()[0].getResponse(),
         answer2 : e.response.getItemResponses()[1].getResponse()
        ,

huangapple
  • 本文由 发表于 2023年5月17日 17:48:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定