[英]Google Script - Copy dynamic range to new spreadsheet
I'd appreciate some help on my first google script if possible. 如果可以的话,我会在第一个Google脚本中为您提供帮助。
I'm trying to archive our daily processing log by copying and pasting them to the bottom of an end of day log spreadsheet. 我正在尝试通过将日常处理日志复制并粘贴到日末日志电子表格的底部来归档它们。
The issue is that the number of rows processed each day will vary so I can't set a fixed number of rows to copy to the target spreadsheet. 问题在于每天处理的行数会有所不同,因此我无法设置要复制到目标电子表格的固定行数。
I just want to copy from row B7 down to the last row with values entered. 我只想从B7行复制到输入值的最后一行 。
function pasteprocessinglog() { var sss = SpreadsheetApp.getActiveSpreadsheet(); var ss = sss.getSheetByName('Sheet_name'); var range = ss.getRange('B7:J'); var data = range.getValues(); var tss = SpreadsheetApp.openById('URL'); var ts = tss.getSheetByName('Processing Log'); ts.getRange(ts.getLastRow() + 1, 2, ss.getMaxRows(), 9).setValues(data); }
I'm currently receiving error: Incorrect range height, was 6467 but should be 6473 我当前收到错误:范围高度不正确,原为6467,但应为6473
I'm guessing this is because it's trying to copy the empty rows too and the spreadsheet isn't long enough. 我猜这是因为它也试图复制空行并且电子表格不够长。
Any help would be appreciated :) 任何帮助,将不胜感激 :)
Thank you! 谢谢!
In data retrieved using ss.getRange('B7:J').getValues(), empty cells are included. 在使用ss.getRange('B7:J')。getValues()检索的数据中,包含空单元格。 So the length of retrieved data is larger than that of real data. 因此,检索到的数据的长度大于真实数据的长度。 6467 and 6473 means the length of data array and the value from getMaxRows(), respectively. 6467和6473分别表示数据数组的长度和getMaxRows()中的值。
And getMaxRows() retrieves the number of most bottom cell including empty cells. getMaxRows()检索包括空单元格在内的最底部单元格的数量。 So in the case for using setValues(), data can be copied by using the length of data array for setValues as numRows of getRange ( https://developers.google.com/apps-script/reference/spreadsheet/sheet#getRange(Integer,Integer,Integer,Integer) ). 因此,在使用setValues()的情况下,可以使用setValues的数据数组的长度作为getRange的numRows( https://developers.google.com/apps-script/reference/spreadsheet/sheet#getRange( Integer,Integer,Integer,Integer) )。
The script is as follows. 脚本如下。
function pasteprocessinglog() {
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ss = sss.getSheetByName('Sheet_name');
var firstrow = 7; // 7th row
var range = ss.getRange(firstrow, 2, ss.getLastRow() - firstrow + 1, 9);
var data = range.getValues();
var tss = SpreadsheetApp.openById('URL');
var ts = tss.getSheetByName('Processing Log');
ts.getRange(ts.getLastRow() + 1, 2, data.length, 9).setValues(data);
}
If my understanding have mistaken, I'm sorry. 如果我的理解有误,对不起。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.