我有一个脚本,当它从编辑一张获取数据,并把最近添加的数据到一个ScriptDb。
该onEdit()
当我真正打开页,然后编辑触发器成功地发射。
然而,这种薄片是通过脚本编程编辑。 被onEdit()
能够基于通过脚本进行修改之后火? 我一直无法让它这样做。
与该开除该脚本onEdit()
触发:
function sheetWasEdited(event) {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var lastRowValues = sheet.getRange(lastRow, 2, 1, 2).getValues()[0];
CgcEmailDatabase.addEmail(now=lastRowValues[0].toString(), email=lastRowValues[1].toString());
}
该onEdit触发旨在当实际用户编辑电子表格的工作,你所描述的用例应该很容易通过调用你干脆直接sheetWasEdited()
与其他功能的功能,如果最新的是同一项目的一部分。
如果更改从另一个项目(其他电子表格或Web应用程序)发那么它会变得非常难以到位。 (让我们知道,如果它是你的使用情况)
编辑以下您的意见:
这样做是为了监测表的长度,保持价值的地方(在脚本属性为例),并调用你的函数,如果行已添加。
这个小的代码应该做的伎俩,你应该设定一个时间触发调用lookatsheet()
在一段时间后,这取决于你需要如何快速反应......我想说每个小时或30'不应该过多, 你决定。
function lookatsheet(){
var ss = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxx');// the ID of the SS you want to look at
var sh = ss.getSheets()[0];// first sheet
var lastrow = sh.getLastRow();
var formertest = ScriptProperties.getProperty('lastTest');// recover the value from the last test (will need to get called once to initiate a valid value)
if (formertest < lastrow){
sheetWasEdited(lastrow);// call your function with lastRow as parameter
ScriptProperties.setProperties({'lastTest': lastrow}, true); // store for next time
}
}
function sheetWasEdited(row) { // modified to work with the other function
var sheet = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxxx').getSheets()[0]
var lastRowValues = sheet.getRange(row, 2, 1, 2).getValues()[0];
CgcEmailDatabase.addEmail(now=lastRowValues[0].toString(), email=lastRowValues[1].toString());
}
注: 可能是有用的评论在第一次运行邮件呼吁避免发送邮件(刚启动脚本属性)