【问题标题】:Sorry ... Another question about on form submit trigger抱歉...关于表单提交触发器的另一个问题
【发布时间】:2020-04-21 01:02:52
【问题描述】:

我有一个脚本可以将表单回复转换为 contactsApp 中的联系人。当我从脚本编辑器运行它时,它可以工作。

我想提交与电子表格关联的表单以触发脚本,因此创建了一个表单提交触发器,但它不起作用(不用说)。

触发器不起作用的解释是表单提交和电子表格获取新数据之间存在某种延迟吗?所以脚本在它有数据可以使用之前被触发。

不管怎样,代码如下:

function oneNewContact() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var shts = ss.getSheets();
  var lr = shts[0].getLastRow();

  var acol = shts[1].getRange(1, 1, lr-1, 1).getValues();
  var drng = shts[1].getRange(1, 2, lr-1, 6).getValues();

  for(var i = 0; i < lr-1; i++) {

    if(acol[i][0]!==1) {     

    var first = drng[i][0];
    var surname = drng[i][1];
    var phone = drng[i][2];
    var email = drng[i][3];
    var consentDate = drng[i][5];
    var grp = 'qcbread';

    //create contact
      var contact = ContactsApp.createContact(first, surname, email);
      var contactID = contact.getId();

    //add info via bug workaround ie getting the new contact via contactID 
    contact = ContactsApp.getContactById(contactID); 
      console.log(contact.getFullName());
    contact.addPhone('mobile', phone);    
    contact.setNotes('contact consent given to MQC on '+ consentDate);

    //update contact
    var group = ContactsApp.getContactGroup(grp);
    contact = contact.addToGroup(group);

    }// end of if

    //added contact marked as processed
    var acell = i + 1;// add 1 to array counter
    var mark = shts[1].getRange('A'+ acell );
    mark.setValue(1);

  }// end of for loop

}

【问题讨论】:

  • view>Stackdriver 错误?
  • 你不想让这个var lr = shts[0].getLastRow();成为这个var lr = shts[1].getLastRow();
  • 只是好奇为什么不使用 e.values?
  • 我不明白你为什么要在每次提交表单时浏览表单提交表的每一行。
  • e.range.getRow()exists

标签: google-apps-script


【解决方案1】:

为了使脚本按预期工作,您应该使用event objects。通过这种方式,您可以直接检索所需的数据,而不必在每次发送新的表单提交时都遍历表单提交表的每一行。

function newContact(e) {

    var sht = e.source.getActiveSheet();
    var row = e.range.getRow();

    var drng = sht.getRange(row, 2, 1, 6).getValues();
    // drng is a single row 2D array

    var first = drng[0][0];
    var surname = drng[0][1];
    var phone = drng[0][2];
    var email = drng[0][3];
    var consentDate = drng[0][5];
    var grp = 'qcbread';

    //create contact
    var contact = ContactsApp.createContact(first, surname, email);
    var contactID = contact.getId();

    //add info via bug workaround ie getting the new contact via contactID 
    contact = ContactsApp.getContactById(contactID);
    contact.addPhone('mobile', phone);
    contact.setNotes('contact consent given to MQC on ' + consentDate);

    //update contact
    var group = ContactsApp.getContactGroup(grp);
    contact = contact.addToGroup(group);

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sht2 = ss.getSheets()[1];
    sht2.getRange(row - 1, 1).setValue(1);

}

参考

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-19
    • 2012-10-14
    • 2010-12-06
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-10
    相关资源
    最近更新 更多