简体   繁体   English

在编辑时将动态单元格的值复制到另一个工作表

[英]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.

相关问题 将单元格值复制到另一个工作表以匹配值 - Copy cell value to another sheet for matching value 根据单元格值将行从一张纸复制到另一张纸 - Copy row from one sheet to another base on cell value Google Apps脚本:将单元格值复制到另一个工作表中,条件是要复制并放置该值 - Google Apps Script: Copy cell value to another sheet with condition to copy and position the value google脚本:根据触发将公式计算出的值从一张纸复制到另一张纸上(在编辑或时间驱动下) - google script: To copy the value calculated by formula from one sheet to another based on TRIGGER (on edit or time driven) Google脚本:将行复制到另一张工作表中进行编辑 - Google Script: Copy row to another sheet on edit ALMOST THERE 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 如果cell = 0,则循环遍历一列并将数据复制到另一张工作表 - loop over a column and copy data to another sheet if cell = 0 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell 将动态字段值复制到另一个字段 - Copy dynamic field value to another field 复制单元格中的值并将值粘贴到同一工作表中的另一个单元格 - Copy values from cell and paste the values onto another cell in the same sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM