如果单元格自动更新,则“编辑"不起作用-需要变通方法Google表格 [英] On Edit doesnt work if cell auto updates - Need a workaround Google Sheets
问题描述
当您手动更改单元格时,此代码将按预期工作.
This code works as intended when you manually change a cell.
function onEdit(event) {
// assumes source data in sheet named Prepsheet
// target sheet of move to named TopUp Required
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(s.getName() == "PrepSheet" && r.getColumn() == 15 && r.getValue() == 0) {
var row = r.getRow();
var numColumns = s.getLastColumn();
var targetSheet = ss.getSheetByName("TopUp Required");
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
我的问题是我正在使用=sum(A2-B2)
更改单元格.如果单元格则为= 0,则该脚本应运行脚本,但不是因为该单元格不是手动更改的.
My problem is I am using =sum(A2-B2)
to change the cell. if cell then =0 it should run the script, but doesn't because the cell wasnt manually changed.
如何修改代码来做到这一点?或者你会怎么做
How could I modify code to do this? Or how would you do it
感谢任何帮助
推荐答案
解决方法是使用可安装的onChange
触发器与具有 =IMPORTRANGE()
函数.
说明:
虽然onEdit
触发器根本无法检测到由单元格公式引起的任何单元格内容更新,但onChange
可以检测某些更改,例如通过=IMPORTRANGE()
公式进行的更新.
While an onEdit
trigger does not detect any cell content update caused by a cell formula at all, onChange
can detect certain changes, such as the update occuring through the =IMPORTRANGE()
formula.
工作流程:
- 创建第二个空电子表格
- 将第二个电子表格的一个单元格
=IMPORTRANGE(IMPORTRANGE(spreadsheet_url, range_string)
分配给第二个电子表格的一个单元格,其中,sheet_url是包含所有数据和公式(例如=sum(A2-B2)
)的第一个电子表格的URL,而range_string是您感兴趣的范围(例如表PrepSheet
中第15列的"PrepSheet!O1:O" - 将脚本附加到第二个电子表格,而不是原始电子表格
- 使用 Scriptproperties 存储单元格中的旧单元格值并将其与新单元格进行比较值-为了检测哪个单元格已被编辑
- Create a second, empty spreadsheet
- Assign to a cell in a sheet of the second Spreadsheet the formula
=IMPORTRANGE(IMPORTRANGE(spreadsheet_url, range_string)
, whereby spreadsheet_url is the URL of the first spreadsheet that contains all your data and formulas (e.g.=sum(A2-B2)
) and range_string the range of interest (e.g. "PrepSheet!O1:O" for column 15 in sheetPrepSheet
) - Attach a script to the second spreadsheet, rather then to the original spreadsheet
- Use Scriptproperties to store cell old cell values and compare to the new values - in order to detect which cell has been edited
示例:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("name"); // the name of the sheet containing the =IMPORTRANGE() formula
var origin=SpreadsheetApp.openById('ID of original spreadsheet');
var s=origin.getSheetByName("PrepSheet");
var lastRow=s.getLastRow();
var range=sheet.getRange(1,1,lastRow,1); //the column into which you imported column 15 from the original spreadsheet
function initialSetUp(){//run this function only once, unless your range of interest changes
//change if required
var values=range.getValues();
for(var i=0;i<values.length;i++){
PropertiesService.getScriptProperties().setProperty('values '+i, values[i][0]);
}
}
function triggeredOnChange() {
var values=range.getValues();
var numColumns = s.getLastColumn();
var targetSheet = origin.getSheetByName("TopUp Required");
for(var i=0;i<values.length;i++){
var scriptValue=PropertiesService.getScriptProperties().getProperty('values '+i);
var newValue=values[i][0];
if(newValue!=scriptValue && newValue==0){
Logger.log(scriptValue);
Logger.log(newValue);
var row = i+1;
var target = targetSheet.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).copyTo(target);
}
}
}
这篇关于如果单元格自动更新,则“编辑"不起作用-需要变通方法Google表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!