【发布时间】: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