简体   繁体   English

google脚本:根据触发将公式计算出的值从一张纸复制到另一张纸上(在编辑或时间驱动下)

[英]google script: To copy the value calculated by formula from one sheet to another based on TRIGGER (on edit or time driven)

So I have a google spreadsheet. 所以我有一个谷歌电子表格。 It has about 15 sheets in it. 里面有15张纸。 I want to copy the cell value from all of these 15 sheets to another workbook. 我想将所有这15张工作表中的单元格值复制到另一个工作簿中。

I tried using the importrange function. 我尝试使用importrange函数。 The values I am trying to copy are dynamic that is based on formula which calculates it. 我尝试复制的值是动态的,基于计算它的公式。

so I tried using the function export 所以我尝试使用功能导出

function copy(){

 var sss = SpreadsheetApp.openById('14wsBaW2HWqtdPZBDycsRVQCgOJuaNIYDKDJOjTDwWO8'); //replace with source ID
 var ss = sss.getSheetByName('Round 1'); //replace with source Sheet tab name

 var range = ss.getRange('G14'); //assign the range you want to copy
 var data = range.getValues();

 var tss = SpreadsheetApp.openById('1SWOZ_Xq-1E3LHfE1FwTt4O9bs-FIAk3I5Wjz9VKFh6A'); //replace with destination ID
 var ts = tss.getSheetByName('Net Worth'); //replace with destination Sheet tab name
 ts.getRange(2, 2, data.length, data[0].length).setValues(data);


 var sss = SpreadsheetApp.openById('14wsBaW2HWqtdPZBDycsRVQCgOJuaNIYDKDJOjTDwWO8'); //replace with source ID
 var ss = sss.getSheetByName('Round 2'); //replace with source Sheet tab name

 var range = ss.getRange('G14'); //assign the range you want to copy
 var data = range.getValues();

 var tss = SpreadsheetApp.openById('1SWOZ_Xq-1E3LHfE1FwTt4O9bs-FIAk3I5Wjz9VKFh6A'); //replace with destination ID
 var ts = tss.getSheetByName('Net Worth'); //replace with destination Sheet tab name
 ts.getRange(3, 2, data.length, data[0].length).setValues(data);

}

NOW as you can see since I have to copy the values from about 15 sheets of the workbook I have to write multiple lines of the copy paste function by changing the Round i( where i=1-15) and the row number where it is posted. 现在您可以看到,因为我必须从大约15张工作簿中复制值,所以我必须通过更改回合i(其中i = 1-15)和行号来编写多行复制粘贴功能发布。

What I want to achieve is to reduce the size of code and base it on time trigger or on open or on edit trigger. 我要实现的是减少代码的大小,并基于时间触发器或打开或编辑触发器。 However, I am stuck and can't find much 但是,我被困住了,找不到很多东西

You can get all the sheets on the book and loop through each of them to copy the value in 'G14' cell. 您可以获取书中的所有工作表,并循环浏览每个工作表,以将值复制到'G14'单元格中。 Save them in a 2D array. 将它们保存在2D数组中。 Refer the code below. 请参考下面的代码。

 ////copying values from each sheet
 var copyBook = SpreadsheetApp.getActiveSpreadsheet();
 var copySheets = copyBook.getSheets();
 var selectedValues = []; 
 for(var sheet in copySheets)
 {
   //Logger.log(copySheets[sheet].getName());
   var value = copySheets[sheet].getRange('G14').getValue();
   selectedValues.push([value]);
 }
 //Logger.log(selectedValues);

Now your values are copied in the array and now you want to paste it your destination sheet. 现在,您的值已复制到数组中,现在您要将其粘贴到目标表中。

////pasting copied values
 var pasteBook = SpreadsheetApp.openById('destination book id'); 
 var pasteSheet = pasteBook.getSheetByName('destination sheet name');
 pasteSheet.getRange(2, 2, selectedValues.length, 1).setValues(selectedValues);

If you want to set a trigger, refer this answer: https://stackoverflow.com/a/34684370/1134291 如果要设置触发器,请参考以下答案: https : //stackoverflow.com/a/34684370/1134291

replace above code with // your work to be done section 将上面的代码替换为// your work to be done部分

暂无
暂无

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

相关问题 Google 工作表应用程序脚本 - 根据循环条件将数据从一张工作表复制到另一张工作表 - Google sheet app script - Copy data from one sheet to another based on condition with loop 用于匹配和复制基于另一个相邻单元格的 Google 表格脚本 - Google sheet script to match and copy based from another adjacent cell 将数据从一张纸复制到另一张 - Google Script - Copy Data from one sheet to another - Google Script 根据条件将单元格值从一个Google工作表复制到另一个 - Copy cell values from one google sheet to another based on condition 根据特定的键值将数据从一张纸复制到另一张纸 - Copy data from one sheet to another based on a specific key value Google脚本:将行复制到另一张工作表中进行编辑 - Google Script: Copy row to another sheet on edit ALMOST THERE Google 工作表脚本将数据集从一张工作表复制到另一张工作表的顶部(仅限值) - Google sheets script copy data sets from one sheet to another (values only) at the top of the sheet 将范围从一个工作表复制到Google工作表中的另一个工作表 - Copy a range from one sheet to another in google sheet Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 在编辑时将动态单元格的值复制到另一个工作表 - Copy value of a dynamic cell to another sheet on edit
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM