[英]Copy value of a dynamic cell to another sheet on edit
I am trying to copy the value of a dynamic cell into a specific cell on another sheet upon edits being done in the first sheet.我试图在第一张表中进行编辑后将动态单元格的值复制到另一张表上的特定单元格中。
Example: Whenever cell A5 and B3 and C2 are edited on sheet 'work', it should take the current value of cell B1 and copy it to cell C5 on sheet 'overview'.示例:每当在“工作”表上编辑单元格 A5、B3 和 C2 时,它应该采用单元格 B1 的当前值并将其复制到工作表“概览”上的单元格 C5。
Here is the code I have so far, but it somehow does not work.这是我到目前为止的代码,但它不知何故不起作用。 What am I missing here?
我在这里想念什么?
function onEdit2() {
var s = SpreadsheetApp.getActiveSheet();
if( s.getName() == "work") {
var r =s.getActiveCell() ;
if(r.getCell('A5') || r.getCell('B3') || r.getCell('C2') {
var b = s.getCell('B1') ;
var uo = SpreadsheetApp.getSheet('overview')
b.copyTo(uo.getCell('C5'), SpreadsheetApp.CopyPasteType.PASTE_VALUES, false)
}
}
}
First of all onEdit2
can not be used as an onEdit
trigger unless it is installable.首先,
onEdit2
不能用作onEdit
触发器,除非它是可安装的。 Therefore I would advice onEdit
so you don't have to create the trigger yourself.因此,我会建议
onEdit
,这样您就不必自己创建触发器。
The optimal way is to use the event handler e
that will give you the relevant information you need regarding the cell that is edited.最佳方法是使用事件处理程序
e
,它将为您提供有关已编辑单元格的相关信息。
I also made your code cleaner by using includes to check whether the edited cell is part of a list of cells.我还通过使用包含检查编辑的单元格是否是单元格列表的一部分来使您的代码更清晰。
function onEdit(e) {
const as = e.source.getActiveSheet();
const ts = e.source.getSheetByName("overview");
const cell = e.range.getA1Notation();
const triggerCells = ["A5","B3","C2"];
if (as.getName() == "work" && triggerCells.includes(cell)){
let b = as.getRange("B1").getValue();
ts.getRange("C5").setValue(b);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.