简体   繁体   English

Google脚本-将动态范围复制到新的电子表格

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

相关问题 将模板电子表格复制到文件夹,然后写入新副本(Google脚本) - Copy template spreadsheet to folder, then write to the new copy (google script) 将过滤后的范围从一个电子表格复制到另一个 - Google 应用脚本 - Copy filtered range from one spreadsheet to another - Google app script Google脚本可复制列的值并在电子表格中插入新列 - Google Script to copy the values of a column and insert a new column in spreadsheet Google脚本:如何将范围复制并粘贴到不为空的行中的新表中 - Google Script: How to copy and paste range to a new sheet on a row that is not empty 如何在给定时间使用Apps脚本在Google Spreadsheet中“复制-粘贴值”动态数据? - How To Use an Apps Script To “Copy-PasteValue” Dynamic Data In a Google Spreadsheet at a given time? Google脚本电子表格范围返回未知 - Google Script Spreadsheet range returns unidentified Google Spreadsheet脚本“单元格引用超出范围” - Google Spreadsheet script “Cell reference out of range” Google Spreadsheet Script RegEx on Cells of Cells - Google Spreadsheet Script RegEx on Range of Cells 将工作表复制到另一个电子表格[Google Apps脚本] - Copy sheet to another spreadsheet [Google Apps Script] 传递动态范围:Google脚本 - Passing Dynamic Range:Google Script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM