1

我觉得这应该如此简单和明显,但我无法弄清楚......

我正在使用 2 个表格和 2 个电子表格。表格 1 提交到表格 1。在表格 1 上,对于每条记录,都有一个链接将用户带到表格 2,表格 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});
      }
    }
4

1 回答 1

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将是具有此处记录的属性的对象(用于表单提交事件)。

于 2014-07-09T10:42:29.327 回答