如何从Google表单提取特定数据到Google表格

时间:2019-07-17 13:52:30

标签: google-apps-script google-sheets

我正在努力将数据从Google表单提取到Google表格中,而我的问题是我仍在寻找如何从表单将特定数据获取到Google表格中, 我有一个Google表单,其中有一些信息,我想知道如何将答案分隔在不同的Google表格中

在Google应用脚本编辑器中可以输入任何脚本吗?

谢谢。

1 个答案:

答案 0 :(得分:0)

使用Apps脚本,您有两个选择

  1. 您可以直接访问特定的表单响应并以所需的方式进行管理(例如,将其存储在特定的电子表格中或通过电子邮件发送),例如由表单提交触发:

 var form = FormApp.openById('XXXXXXXXXXXXXXXXXXXXXXXXX');
 ScriptApp.newTrigger('myFunction')
        .forForm(form)
        .onFormSubmit()
        .create();

 function myFunction() {
     var array=[];  
     var formResponses = form.getResponses();
      //i loops through all form submissions, e.g. if 3 users submitted a form - there will be three entries for formResponse
     for (var i = 0; i < formResponses.length; i++)
      {
       var formResponse = formResponses[i];
       var itemResponses = formResponse.getItemResponses();
       // if you are only interested in the answer to question number "j"
       var j=1;
      var itemResponse = itemResponses[j-1];
      array.push(itemResponse.getResponse());
      }
    Logger.log(array);  
 }
  1. 您从Google表单存储响应的电子表格中检索数据(可以通过从表单UI选择响应目标或使用Apps脚本方法setDestination()来设置):

 var form = FormApp.openById('XXXXXXXXXXXXXXXX');
 var ss=SpreadsheetApp.getActiveSpreadsheet();
 form.setDestination(FormApp.DestinationType.SPREADSHEET, ss.getId());

 function readSheet() {
     var ss=SpreadsheetApp.getActiveSpreadsheet();
     var sheet=ss.getActiveSheet();
     var responses=sheet.getDataRange();
     //If you are interested in the response to question 3:
     var k=3;
     for(var i=0;i<sheet.getLastRow();i++)
      {
        Logger.log("The response to question "+responses.getValues()[0][k]+" submitted "+ responses.getValues()[i][0]+" is: "+responses.getValues()[i][k])
      }
    }