Google apps script Google应用程序脚本以静默方式运行我的onEdit脚本的部分两次
我有下面的谷歌应用程序脚本代码,它是由对一个单元格的编辑触发的,该单元格上有一个下拉数据验证(从列表中选择)。有时,它会向目标表中添加两个重复的行,而不是所需的单行。我在不同的位置添加了日志记录,以查看是否可以检测到某段代码何时/何地/为何运行了两次。日志不会显示脚本的任何部分意外运行。我还添加了锁定。它没有帮助解决这个问题,尽管它确实使脚本以更可靠的方式运行。我真的很想弄清这件事的真相,尽管这真的是一件麻烦事。如果您需要任何其他信息,请告诉我:Google apps script Google应用程序脚本以静默方式运行我的onEdit脚本的部分两次,google-apps-script,google-sheets,triggers,Google Apps Script,Google Sheets,Triggers,我有下面的谷歌应用程序脚本代码,它是由对一个单元格的编辑触发的,该单元格上有一个下拉数据验证(从列表中选择)。有时,它会向目标表中添加两个重复的行,而不是所需的单行。我在不同的位置添加了日志记录,以查看是否可以检测到某段代码何时/何地/为何运行了两次。日志不会显示脚本的任何部分意外运行。我还添加了锁定。它没有帮助解决这个问题,尽管它确实使脚本以更可靠的方式运行。我真的很想弄清这件事的真相,尽管这真的是一件麻烦事。如果您需要任何其他信息,请告诉我: function onEdit(e) {
function onEdit(e) {
Logger.log('Running onEdit');
var lock = LockService.getUserLock();
lock.waitLock(10000);
var cache = CacheService.getUserCache();
var spreadsheet = SpreadsheetApp.getActive().getActiveSheet();
var range = e.range;
var col = range.columnStart;
var row = range.rowStart;
var status = range.getDisplayValue();
if (col == 7 && row >= 13 && row <= 53) { //Ongoing Block
cache.put("oldStatus", e.oldValue);
Logger.log(row,col,status, e.oldValue);
if (status == 'ToVendor') { //Send to Vendor Block
copyEquipRow(spreadsheet,row,col,1,true);
Logger.log(row,col,cache.get("equipID"),cache.get("dateAdded"),cache.get("priority"),cache.get("problem"),cache.get("location"),cache.get("targetDate"));
pasteToVendor(spreadsheet);
}
if (status == 'AddReq') { //Add Related Req to the Req Block
copyEquipRow(spreadsheet,row,col,1,false);
Logger.log(row,col,cache.get("equipID"),cache.get("dateAdded"),cache.get("priority"),cache.get("problem"),cache.get("location"),cache.get("targetDate"));
addReq(spreadsheet);
spreadsheet.getRange(row, col).setValue(e.oldValue)
}
}
if (col == 16 && row >= 31 && row <= 37) { //Vendor Block
cache.put("oldStatus", e.oldValue);
Logger.log(row,col,status, e.oldValue);
if (status == 'ToOngoing') {
copyEquipRow(spreadsheet,row,col,0,true);
Logger.log(row,col,cache.get("equipID"),cache.get("dateAdded"),cache.get("priority"),cache.get("problem"),cache.get("location"),cache.get("targetDate"));
pasteToOngoing(spreadsheet);
}
}
lock.releaseLock();
}
function copyEquipRow(spreadsheet,row,col,problemCol,deleteRow) { //Status Equip # Date Added Priority Problem Location Target Date
Logger.log('Running CopyEquipRow');
var cache = CacheService.getUserCache();
var locationCol = 5 + problemCol;
var targetCol = 6 + problemCol;
var clearCol = 7 + problemCol;
var dateAdded = Utilities.formatDate(spreadsheet.getCurrentCell().offset(0, 2).getValue(), "EST", "M/d/yyyy");
var targetDate = Utilities.formatDate(spreadsheet.getCurrentCell().offset(0, targetCol).getValue(), "EST", "M/d/yyyy");
cache.put("equipID", spreadsheet.getCurrentCell().offset(0, 1).getValue());
cache.put("dateAdded",dateAdded);
cache.put("priority",spreadsheet.getCurrentCell().offset(0, 3).getValue());
cache.put("problem",spreadsheet.getCurrentCell().offset(0, 4).getValue());
cache.put("location",spreadsheet.getCurrentCell().offset(0, locationCol).getValue());
cache.put("targetDate",targetDate);
if (deleteRow == true ) {
spreadsheet.getRange(row, col, 1, clearCol).clearContent();
}
}
function pasteRow(spreadsheet,startCell,problemCol,includeStatus,dateOverride) {
Logger.log('Running pasteRow');
var cache = CacheService.getUserCache();
var currentCell = spreadsheet.getRange(startCell).activate();
var row = 0;
while (currentCell.offset(row, 1).isBlank() == false) {
row = ++row;
}
var oldStatus = cache.get("oldStatus");
var equipID = cache.get("equipID");
var dateAdded = cache.get("dateAdded");
var priority = cache.get("priority");
var problem = cache.get("problem");
var location = cache.get("location");
var targetDate = cache.get("targetDate");
var locationcol = 5 + problemCol;
var targetcol = 6 + problemCol;
Logger.log(equipID,dateAdded,priority,problem,location,targetDate);
if (includeStatus == true) {
currentCell.offset(row, 0).setValue(oldStatus);
}
currentCell.offset(row, 1).setValue(equipID);
if (dateOverride == true ){
currentCell.offset(row, 2).setValue(Utilities.formatDate(new Date(), "EST", "M/d/yyyy"));
}
else {
currentCell.offset(row, 2).setValue(dateAdded);
}
currentCell.offset(row, 3).setValue(priority);
currentCell.offset(row, 4).setValue(problem);
currentCell.offset(row, locationcol).setValue(location);
currentCell.offset(row, targetcol).setValue(targetDate);
}
function pasteToVendor(spreadsheet) {
Logger.log('Running pasteToVendor');
pasteRow(spreadsheet,'P30',0,true);
}
function pasteToOngoing(spreadsheet) {
Logger.log('Running pasteToOngoing');
pasteRow(spreadsheet,'G12',1,true);
}
function addReq(spreadsheet) {
Logger.log('Running addReq');
pasteRow(spreadsheet,'O12',1,false,true);
}
函数onEdit(e){
Logger.log('Running onEdit');
var lock=LockService.getUserLock();
lock.waitLock(10000);
var cache=CacheService.getUserCache();
var spreadsheet=SpreadsheetApp.getActive().getActiveSheet();
var范围=e范围;
var col=range.columnStart;
var row=range.rowStart;
var status=range.getDisplayValue();
如果(col==7&&row>=13&&row=31&&row,onEdit(e)函数将响应无法执行需要权限的操作的简单触发器
不稳定的onEdit触发器与您选择的函数绑定,它获得与简单触发器相同的事件对象。如果您不将可安装的触发器重命名为onEdit()以外的其他名称,那么您肯定会一直获得两个触发器
还要记住,OneEdit触发的功能必须在30秒内完成
onEdit(e)函数响应无法执行需要权限的操作的简单触发器
不稳定的onEdit触发器与您选择的函数绑定,它获得与简单触发器相同的事件对象。如果您不将可安装的触发器重命名为onEdit()以外的其他名称,那么您肯定会一直获得两个触发器
还要记住,OneEdit触发的功能必须在30秒内完成
是否为onEdit()函数创建了可安装触发器?如果已创建,则应将该函数重命名为onEdit()以外的其他函数并删除onEdit。您可能同时获得了可安装触发器和简单触发器。是否为onEdit()创建了可安装触发器函数?如果有,那么应该将该函数重命名为onEdit()以外的其他函数并删除onEdit。您可能同时获得了可安装触发器和简单触发器。信息非常丰富。我不知道为什么我在网上找不到如此简单的答案。为什么在日志中没有看到重复的运行?简单触发器日志是否保存在其他地方?它们都只是触发器。但在您的情况下,bec因为您的函数名为onEdit()然后你的函数将同时接收这两个。只需更改名称并删除名为onEdit的名称,简单触发器将在编辑时继续触发,但它不会再进入你的函数。信息非常好。不确定为什么我在网上找不到如此简单的答案。为什么在日志中看不到重复的运行?是吗简单触发器日志保存在其他地方?它们都只是触发器。但在您的情况下,因为您的函数名为onEdit(),那么您的函数将同时接收这两个日志。只需更改名称并删除名为onEdit的日志,简单触发器将在编辑时继续触发,但不会再转到您的函数。