简体   繁体   English

如何使用 Google Sheet Script 将数据范围从一个工作表复制到另一个工作表

[英]How to Copy a data range from one sheet to another sheet using Google Sheet Script

Disclaimer: I am just new in coding and I just want to automate things up in my work.免责声明:我只是编码方面的新手,我只想在我的工作中实现自动化。

I want to copy a data from 'Data Entry' sheet to another sheet specified by the cell in 'Data Entry' sheet.我想将“数据输入”工作表中的数据复制到“数据输入”工作表中单元格指定的另一个工作表。

here is the code that I am using:这是我正在使用的代码:

 function DataEntry() { let spreadSheet = SpreadsheetApp.getActiveSpreadsheet(); let sourceSheet = spreadSheet.getSheetByName('Data Entry'); let sourceRange = sourceSheet.getDataRange(); let sourceValues = sourceRange.getValues(); let rowCount = sourceValues.length(); let columnCount = sourceValues[0].length(); let targetSheet = spreadSheet.getSheetByName('6'); let targetRange = targetSheet.getRange(2,1,rowCount,columnCount); targetRange.setValues(sourceValues); }

In the source sheet, I want to copy the data starting from row 3, column L. (the number of rows varies every data batch)在源工作表中,我想复制从第 3 行,L 列开始的数据。(每个数据批次的行数不同)

Then in the target sheet, which will be specified by a cell in source sheet (cell T2).然后在目标工作表中,将由源工作表中的单元格(单元格 T2)指定。 The pasting should start at row 2, column A.粘贴应从第 2 行,A 列开始。

function DataEntry() {
  let spreadSheet = SpreadsheetApp.getActiveSpreadsheet();
  let sourceSheet = spreadSheet.getSheetByName('Data Entry');

  let sourceRange = sourceSheet.getDataRange();
  let sourceValues = sourceRange.getValues();
  let sheetName = sourceValues[1][19];
  sourceValues = sourceValues.slice(2).map(row => row.slice(11));

  let rowCount = sourceValues.length;
  let columnCount = sourceValues[0].length;

  let targetSheet = spreadSheet.getSheetByName(sheetName);
  let targetRange = targetSheet.getRange(2,1,rowCount,columnCount);

  targetRange.setValues(sourceValues);
}

Explanation:解释:

  • Since getValues() returns a 2D array, in order to get the value from a certain cell you just have to use the appropriate array indexes to access the desired element (in this case, 1 and 19 , corresponding to T2 ).由于getValues()返回一个二维数组,为了从某个单元格中获取值,您只需使用适当的数组索引来访问所需的元素(在本例中为119 ,对应于T2 )。
  • Use map and slice to retrieve a subset of the range values.使用mapslice检索范围值的子集。 You could also retrieve the corresponding range directly (eg sourceSheet.getRange("L3:L").getValues().flat().filter(String) ), but in this way you are minimizing the amount of calls to the spreasdheet service, which is a good practice .您也可以直接检索相应的范围(例如sourceSheet.getRange("L3:L").getValues().flat().filter(String) ),但通过这种方式可以最大限度地减少对 spreasdheet 服务的调用量,这是一个很好的做法

暂无
暂无

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

相关问题 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 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? Google Script:根据值将行从一张纸复制到另一张纸 - Google Script: Copy row from one sheet to another depending on value 如何将范围从一个工作表复制到另一工作表的范围,然后重复复制到下一行 - How to copy a range from one sheet to a range in another sheet, then repeat copying to the next row down 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 使用谷歌应用脚本将谷歌工作表中的数据拆分到另一张工作表 - Split data in google sheet to another sheet using google app script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM