仅在提交特定表单时触发脚本 - Google Script / Sheets / Forms

时间:2014-07-08 20:52:27

标签: triggers google-apps-script google-sheets google-form event-triggers

我觉得这应该是如此容易和明显,但我无法弄清楚......

我正在处理2个表单和2个电子表格。表单1提交到表单1.在表单1上,对于每个记录,有一个链接将用户带到表单2,该表单是预先填充了表单1中的数据的一半。当用户提交表单2时,它填充表单2

我提交了几个应该在提交表单1时触发的脚本。我正在使用"来自电子表格" " OnFormSubmit"触发。但是,在提交表单2时,脚本也会触发。

如何才能使脚本仅在提交表单1时执行?另外,有没有办法确保脚本按特定顺序触发?

如果有帮助,脚本如下。除触发问题外,它们都按原样正常工作。我知道我可以合并第二和第三个脚本,我会,但我想先解决这个触发问题,因为我每次测试时都会收到双倍的电子邮件。

第一个脚本:

function onFormSubmit(e) {

  //Uses time in milleseconds to create unique ID #
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("ComplaintLog");
  var d = new Date();
  var n = d.getTime();
  var lastRow = sheet.getLastRow();
  var cell = sheet.getRange("BA" + lastRow);
  cell.setValue(n);
  var cell = sheet.getRange("BB" + lastRow);
  cell.setFormula("=right(BA"+lastRow+",6)");
  sheet.getRange("BB"+lastRow).copyTo(sheet.getRange("BC"+lastRow), + 
    {contentsOnly:true});
}

第二个脚本:

function formSubmitReply(e) {
//Sends email to certain users when a new complaint has been entered  
  var emailAddresses = 'person@organzation.com';

  MailApp.sendEmail(emailAddresses,
                  "person@organzation.com",
                  "New Guest Complaint",
                  "A new guest complaint has been entered into the database."
                  + "\n\n To vew the database, click here: http://goo.gl/DI33EC");
}

第三个脚本:

function createResolutionForm() {

  var ss = SpreadsheetApp.getActive()
  var sheet = ss.getSheetByName("ComplaintLog")
  var lastRow = sheet.getLastRow();
  var data = ss.getSheetByName("ComplaintLog") +
    .getRange("A"+lastRow+":Z"+lastRow).getValues();
  var form = FormApp.openById('The form's ID goes here.  that part works.');
  var items = form.getItems();

  for (var i = 0; i < data.length; i++) {
    var formResponse = form.createResponse();

    //ID
    var formItem = items[1].asTextItem();
    var response = formItem.createResponse(data[i][0]);
    formResponse.withItemResponse(response);

    //Guest Name
    var formItem = items[2].asTextItem();
    var response = formItem.createResponse(data[i][3]);
    formResponse.withItemResponse(response);

    //email
    var formItem = items[3].asTextItem();
    var response = formItem.createResponse(data[i][4]);
    formResponse.withItemResponse(response);

    // The pre-populated form is being created here.  I didn't include every 
    // form item for brevity's sake.
  }

    //Create Link
    var formUrl = formResponse.toPrefilledUrl();

    //Enable Clickable ID
    var idNum = sheet.getRange("BC"+lastRow).getValues();

    sheet.getRange("A"+lastRow).setFormula +
       ('=HYPERLINK("' + formUrl + '","' + idNum + '")');

    var sheetUrl = "The URL to the spreadsheet goes here - that part works.";


    //Send Email to assigned managers
    var j,tempname=[],name, subject, managername, message;

    managername = sheet.getRange("P"+lastRow).getValue();
    tempname=managername.split(" ");

    Logger.log(managername)

    if (tempname.length==2) {
        name=tempname[0].slice(0,1) + tempname[1] + '@organization.com';
        subject = 'Action Required';
        var message = "<html><body>"
        + "<p> You have been assigned to follow-up on a complaint,"
        + "or your contact information has been given to a customer in"
        + "regards to a complaint."
        + "<p><p>The complaint ID number is " + idNum +"."
        + "<p>To go directly to this complaint," 
        + "<a href="+formUrl+"><b>click here</b></a>."
        + "<p>To vew the database so that you can take action," 
        +<a href="+sheetUrl+">click here</a>."
        + "</body></html>";


      MailApp.sendEmail(name, subject,"",{htmlBody : message});
      }
    }

1 个答案:

答案 0 :(得分:0)

从脚本中设置触发器可能就是您要查找的内容。您可以创建基于每个表单响应表单提交事件的触发器,如下所示:

function setup () {
  ScriptApp.newTrigger('onForm1ResponseHandler').forForm(form1).onFormSubmit().create();
  ScriptApp.newTrigger('onForm2ResponseHandler').forForm(form2).onFormSubmit().create();
}

其中form1和form2是Form个对象,'onForm1ResponseHandler'是处理函数的名称。

使用如下设置的处理函数:

function onForm1ResponseHandler (e) {
  ...
}

e将是一个对象,其属性记录为here(对于Form Submit事件)。