这个脚本对我来说很酷。我不希望过滤更改日志表来获取我的数据,而是希望脚本只记录特定范围内的更改,让我们说“A2:E150'”。由于我在编码方面的知识很少,我尝试在几个地方插入.getrange(),但没有运气..
希望有人可以帮我解决在脚本中完全定义范围的问题
代码:
function onEdit() {
var sheetsToWatch = ['outcome overview', 'Sheet1', 'Another sheet'];
// name of the sheet where the changelog is stored
var changelogSheetName = "Changelog";
var timestamp = new Date(new Date().setHours(0,0,0,0));
var ss = SpreadsheetApp.getActiveSpreadsheet().getRange('A2:A');
var sheet = ss.getActiveSheet();
var cell = sheet.getActiveCell();
var sheetName = sheet.getName();
var email = Session.getActiveUser().getEmail();
var email2 = Session.getEffectiveUser().getEmail();
// if it is the changelog sheet that is being edited, do not record the change
if (sheetName == changelogSheetName) return;
// if the sheet name does not appear in sheetsToWatch, do not record the change
var matchFound = false;
for (var i = 0; i < sheetsToWatch.length; i++) {
if (sheetName.match(sheetsToWatch[i])) matchFound = true;
}
if (!matchFound) return;
var columnLabel = sheet.getRange(/* row 1 */ 1, cell.getColumn()).getValue();
var rowLabel = sheet.getRange(cell.getRow(), /* column A */ 1).getValue();
var changelogSheet = ss.getSheetByName(changelogSheetName);
if (!changelogSheet) {
// no changelog sheet found, create it as the last sheet in the spreadsheet
changelogSheet = ss.insertSheet(changelogSheetName, ss.getNumSheets());
// Utilities.sleep(2000); // give time for the new sheet to render before going back
// ss.setActiveSheet(sheet);
changelogSheet.appendRow(["Timestamp", "Sheet name", "Cell address", "Column label", "Row label", "Value entered","email", "email2"]);
changelogSheet.setFrozenRows(1);
}
changelogSheet.appendRow([timestamp, sheetName, cell.getA1Notation(), columnLabel, rowLabel, cell.getValue(), email, email2]);
}
答案 0 :(得分:0)
您应该能够通过测试“ActiveCell”(sheet.getActiveCell()
)是否在指定范围内来记录给定范围内的更改。
要测试ActiveCell
是否在给定的Range
中,我会做这样的事情:
var cell = sheet.getActiveCell();
// .getColumn returns an integer, where A=1, B=2, etc.
if (cell.getColumn() < 6 && cell.getRow() > 1 && cell.getRow() < 151) {
//execute script
} else {
return
}
我还建议从变量.getRange('A2:A')
的声明中删除ss
。