我有 2 个电子表格,即 Source和 Destination .在 Source 中,我列出了我的示例产品及其指定价格,并使用 IMPORTRANGE 功能将其复制到 Destination。现在,我正在尝试仅在 'Destination Sheet 1'B:B 上有更新时触发 OnChange 触发器。这是我的代码:
function testFunction(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Destination Sheet 1");
if (e.source.getSheetName = "Destination Sheet 1" && e.range.getRow() > 1 && e.range.getColumn() == 2){
var range = ss.getRange(e.range.getRow(), 3, ss.getLastRow());
range.setValue("There's an update");
}
}
我还添加了函数 testFunction on OnChange Trigger.
但是,基于Event Objects Documentation我无法获得从事件对象更新的范围。 “范围”是指 Destination Sheet 1'B:B。我收到此错误:
"TypeError: Cannot read property 'getRow' of undefined at testFunction(Code:3:64)"
期望的结果(在 'Destination Sheet 1'!B:B 中更新定价后):
我对 google scripts 还是很陌生,我真的尝试了所有我能想到的方法,但它并没有让我到任何地方。任何帮助是极大的赞赏。提前致谢!
更新 1: 当在 IMPORTRANGE 函数上的工作表上有更新时,似乎无法触发 OnChange 触发器。
备选方案: 由于它不起作用,我正在考虑在 Source 上使用 OnEdit 触发器来处理该编辑并在 Destination 上应用我想要的结果。但是,我遇到了权限问题。
Exception: You do not have permission to call SpreadsheetApp.openById. Required permissions: https://www.googleapis.com/auth/spreadsheets
这是我的代码:
function onEdit(e){
aSourceFunction(e);
}
function aSourceFunction(e) {
//IF stmt to make sure that OnEdit Trigger is not triggerred anywhere
if (e.source.getSheetName = "Source Sheet 1" && e.range.getRow() > 1 && e.range.getColumn() == 2){
//my attempt to call Destination Sheet 1 from Destination
var ss = SpreadsheetApp
.openByID("1QMtU6VbNQyDLXMHmaLBBDePH5l")
.setActiveSheet("Destination Sheet 1");
//desired outcome
ss.getRange(e.range.getRow(), 4, ss.getLastRow()).setValue("There's an update");
}
}
此外,我不确定我的担忧是否相关,但我在某处读到我的代码不应该有 @OnlyCurrentDoc
但我的代码中没有任何地方,即使在我的 list 文件。
{
"timeZone": "Asia/Hong_Kong",
"dependencies": {
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8"
}
更新 2:成功了!最初,我有一个简单的 OnEdit 触发器。然而,根据this ,您需要有一个可安装的 OnEdit 触发器才能正常工作(还修复了我的错误代码),并且当我运行代码时,出现“权限提示”让我访问目标电子表格。这是我的代码,供任何需要它的人使用:
function aSourceFunction(e) {
//IF stmt to make sure that OnEdit Trigger is not triggerred anywhere
if (e.source.getSheetName = "Source Sheet 1" && e.range.getRow() > 1 && e.range.getColumn() == 2){
//my attempt to call Destination Sheet 1 from Destination
var ss = SpreadsheetApp.openById("1QMtU6VbNQyDLXMHmaLBBDePH5l-4wDPUNQ827gb3jXY");
var sheet = ss.getSheetByName("Destination Sheet 1");
//desired outcome
sheet.getRange(e.range.getRow(), 4).setValue("There's an update");
}
}
注意:转到编辑>当前项目的触发器>使用 aSourceFunction 作为您的函数并使用 OnEdit 作为触发器添加触发器。
最佳答案
来自问题
However, based on Event Objects Documentation there's no option for me to get the range that was updated from the events object. By "range", I mean the Destination Sheet 1'B:B.
你是对的,没有办法从更改事件对象中获取更新的范围。
但是如果您已经知道感兴趣的范围是'Destination Sheet 1'!B:B
,您可以使用:
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var range = spreadsheet.getRange("Destination Sheet 1!B:B");
关于错误消息,它的发生是因为更改事件对象不包含 range
属性。
请记住(来自Installable Triggers)
An installable change trigger runs when a user modifies the structure of a spreadsheet itself—for example, by adding a new sheet or removing a column.
关于如何使 OnChange 触发器仅在 B 列更新时触发,您可以尝试使用 getActiveRange()
但有时它不起作用。有一个与此相关的未决问题 getActiveRange() incorrect in onChange trigger for some column/row operations
相关
关于javascript - 指定列有更新时触发的 OnChange 触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63787077/