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