我正在尝试编写一个脚本,该脚本将在从表单(不是谷歌表单)插入到 Google 工作表的行(第 23 列)上插入时间戳;它是其他供应商将数据发送到工作表和不传递时间戳)。
我一直在尝试根据示例编写一些脚本,但我似乎无法让它工作。到目前为止,我的脚本如下:
function setUpTrigger() {
ScriptApp.newTrigger('timestamp')
.forSpreadsheet('spreadsheet_id_goes_here')
.onChange()
.create();
}
function timestamp(e){
if (e.changeType == 'INSERT_ROW'){
SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange(e.range.getRow(), 23).setValue(new Date());
}
}
非常感谢任何帮助
最佳答案
问题:
onChange
事件对象 e
不包含键 range
。它不直接提供有关 range
的信息。
解决方案:
在 onChange
的大多数情况下,特别是在您的情况下,更改类型是 INSERT_ROW
,activeRange
表示插入的当前行。
function timestamp(e){
if (e.changeType == 'INSERT_ROW'){
const sh = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
sh.getRange(sh.getActiveRange().getRow(), 23).setValue(new Date());
}
}
相关回答:
关于google-apps-script - 用于在从表单插入的行上插入时间戳的 Google 表格脚本?如何使用 onChange 检测插入的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63839264/