Embedding onFormSubmit Triggers for multiple Google forms that I generate with Script

吃可爱长大的小学妹 提交于 2019-12-25 05:16:07

问题


I have a script for creating multiple Google forms. within each Google form, I want to create an onFormSubmit trigger to post the responses to a selected spreadsheet.

Using installable triggers but the trigger doesn't seem to get embedded into the forms I create. The code as follows:

for(i = 0; i<arrayOfArrays.length; i++) {
    var form = FormApp.create("Name");
    form.setTitle('Blah blah');

    form.addTextItem();
    // my questions

   function writeToSpreadsheet() {
     var formResponses = form.getResponses();
     var ss = SpreadsheetApp.getSpreadsheetByName("1bFjwHt_8Ct_iJCDc5F3lFgrCqSTMjQVOHrL3DQEzLmM");
     var sheet = ss.getSheets()[0]; 
     var newRow = sheet.getLastRow() + 1;

     for (j=0; j<array.length; j++) {
       var questionResponse = formResponses[j];
       ss.getRange(newRow,j+1).setValue(questionResponse);
     }
   }// end writeToSpreadsheet

  function createFormSubmitTrigger() {
     ScriptApp.newTrigger(writeToSpreadsheet)
           .forForm(form)
           .onFormSubmit()
           .create();
  }

}// ends loop to create many forms

Am I doing it right, or is this not even possible? Thanks!


回答1:


  • One thing to note is that you cannot create a function inside a for loop but you can call the function.
  • Second thing is that a trigger does not fire unless you have authorized the script that is you need to manually run the trigger function for every form to get it to work
  • You need to manually install each script to each form or you can install the script in one form and copy the form and the script would be installed in the copied form but you need to run the trigger once to authorize it.

Your script is error prone because functions cannot be defined inside another function let alone a for loop.



来源:https://stackoverflow.com/questions/43510581/embedding-onformsubmit-triggers-for-multiple-google-forms-that-i-generate-with-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!