简体   繁体   English

Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格

[英]Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells

I'm trying to copy certain cells from sheet 1 (source sheet) to sheet 2 (target sheet), without deleting existing cells in sheet 2.我正在尝试将某些单元格从工作表 1(源工作表)复制到工作表 2(目标工作表),而不删除工作表 2 中的现有单元格。

I want to copy A5:A800 from sheet 1 to C7:C802 from sheet 2.我想将工作表 1 中的 A5:A800 复制到工作表 2 中的 C7:C802。

Conditions:状况:

  • One-to-one copying: So for example, content of cell A25 goes to cell C27.一对一复制:例如,单元格 A25 的内容转到单元格 C27。
  • No overwriting: if C27 already has a value or text, it should stay like that.不覆盖:如果 C27 已经有一个值或文本,它应该保持原样。 A25 doesn't get copied. A25 不会被复制。 The script should skip non-empty cells.该脚本应跳过非空单元格。
  • A cell with a zero should not be considered as an empty cell.带有零的单元格不应被视为空单元格。

My usage我的用法

I will add the script to a 'button'.我会将脚本添加到“按钮”。 My sheet 1 keeps getting updated automatically.我的工作表 1 不断自动更新。 Sometimes I manually change cells in sheet 2. I don't want those changes to be overwritten when I press the button (run the script).有时我手动更改工作表 2 中的单元格。我不希望在按下按钮(运行脚本)时覆盖这些更改。

function CopyCells() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  
  var sheet1 = ss.getSheetByName("sheet1"); //source sheet
  var sheet2 = ss.getSheetByName("sheet2"); //target sheet

  //missing for loop magic
  
  var getCopyRange = sheet1.getRange("A5:A800");
  getCopyRange.copyTo(sheet2.getRange("C7:C802"));

}

Explanation:解释:

  • My approach would be to get the data of sheet2 and use map to fill in the empty values with the values of sheet1 :我的方法是获取sheet2的数据并使用mapsheet1的值填充空值:

     vals2.map((v,i)=>[v==''?vals1[i]:v]);

    For the non-empty values keep the original values.对于非空值,保留原始值。

  • I also added an onOpen() function to create a menu button linked to the CopyCells function.我还添加了一个onOpen() function 来创建一个链接到CopyCells function 的菜单按钮。

  • I use a ternary operator to simplify the code and I flat ten the values from the sheets since we consider values of a column and therefore a 1D array makes more sense.我使用三元运算符来简化代码,并将工作表中的值平整十个,因为我们考虑列的值,因此一1D数组更有意义。

Solution:解决方案:

function CopyCells() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet1 = ss.getSheetByName("sheet1"); //source sheet
  const sheet2 = ss.getSheetByName("sheet2"); //target sheet
  const range1 = sheet1.getRange('A5:A800');
  const range2 = sheet2.getRange('C7:C802');
  const vals1 = range1.getValues().flat();
  const vals2 = range2.getValues().flat();
  const fvals = vals2.map((v,i)=>[v==''?vals1[i]:v]);
  range2.clearContent();
  range2.setValues(fvals);
}
 
function onOpen() {
  SpreadsheetApp.getUi()
  .createMenu('Button')
  .addItem('Copy Cells', 'CopyCells')
  .addToUi();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty 将特定范围复制到从一个工作表到另一个工作表中的第一个空行和特定列 - Copy a specific range to from one sheet to the first empty row and specific column in another sheet 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 进行编辑后,如何将范围从一个Google表格复制到另一个表格? - How do I copy a range from one Google Sheet to another when edits are made? 如何将范围从一个工作表复制到另一工作表的范围,然后重复复制到下一行 - How to copy a range from one sheet to a range in another sheet, then repeat copying to the next row down Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM