我正在尝试使用Google工作表脚本编辑器创建自定义onEdit函数。我想知道是否可以添加一个输入参数,如onEdit(e,row),其中row是一个整数,用于指定目标单元格。这里是我的小白代码:如何将输入参数添加到onEdit函数
function onEdit(e,row) {
// writes the current date to the cell in column B on the same row when a cell in a specific column is edited
var sheetNameToWatch = "M2";
var columnNumberToWatch = /* column */ 7; // column A = 1, B = 2, etc.
var ss = SpreadsheetApp.getActiveSpreadsheet(); // not used atm
var sheet = SpreadsheetApp.getActiveSheet(); // sheet on which changes are tracked
var sheet2 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("GRID-TRACKING") // sheet that contains formula cells
var range = sheet.getActiveCell(); // active cell is cell being edited
if (sheet.getName() == sheetNameToWatch && range.getColumn() == columnNumberToWatch) {
var targetCell = sheet2.getRange(row, 2); // I want the input parameter to control the "row" so I can autofill
targetCell.setValue("" + Utilities.formatDate(new Date(), "GMT", "yyyy-MM-dd")); // writes current date to cell
}
}
我使用这个写任何编辑对表“M2” G列的最后日期与公式表(“GRID跟踪”)。我把公式放在列A中,并将日期写入列B.问题是我的“行”变量是未定义的...如果我用一个固定的数字替换它,并且不尝试添加参数,它的工作正常,但我需要一个可以自动填充的参数。
在细胞我的公式是= onedit(单元格,行)
感谢,
Umpsy
你不能喂其他任何参数为onEdit。你只是想让它发生在同一排?为什么不利用[事件对象](https://developers.google.com/apps-script/guides/triggers/events)通过? –
阅读onEdit文档你知道它的用途。 –