我需要知道用户是在删除行、编辑单元格还是粘贴一堆单元格。我一直在查看文档,但找不到确定用户操作的方法。有没有办法知道在 onEdit 中使用了什么样的操作?
最佳答案
您可以为表格使用可安装的更改事件监听器。
https://developers.google.com/apps-script/guides/triggers/events#change
可以通过测试 REMOVE_ROW
更改类型来确定行是否被删除。
还可以确定编辑的是单个单元格还是多个单元格。请参阅下面的代码。
粘贴不会插入行或列,因此如果更改类型“EDIT”发生,那么您可以假设没有插入行或列。我不知道有什么方法可以同时手动编辑多个单元格,所以如果事件范围包含多个单元格,那么它可能是“粘贴”。
function nameOfFunction(e) {
var A1Notation,typeOfChange;
//Install this function as a trigger for Sheets change
typeOfChange = e.changeType;//Get the type of change that was made
Logger.log('typeOfChange: ' + typeOfChange)
Logger.log('typeof typeOfChange: ' + typeof typeOfChange)
switch(typeOfChange) {
case 'REMOVE_ROW':
Logger.log('A row was deleted')
break;
case 'EDIT':
A1Notation = SpreadsheetApp.getActiveRange().getA1Notation();
Logger.log('A1Notation: ' + A1Notation)
if (A1Notation.indexOf(":") === -1) {//There is NOT a colon in the A1 notation
Logger.log('An Edit was made to a single cell')
} else {
Logger.log('An Edit was made to MULTIPLE cells')
}
break;
case 'OTHER':
/* This is NOT an edit of types
EDIT, INSERT_ROW, INSERT_COLUMN, REMOVE_ROW, REMOVE_COLUMN, INSERT_GRID, REMOVE_GRID, FORMAT
*/
Logger.log('This is NOT an edit or a row Deletion')
break;
default:
}
}
关于javascript - 有没有办法确定在 Google 表格脚本中与 onEdit 一起使用的编辑类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56378947/