[英]Google Script: How to copy and paste range to a new sheet on a row that is not empty
I'm trying to copy and paste a range from Sheet1 B4:E to a new target range, Sheet 2 B4:E to a non empty row as I also have a formula in column F of Sheet2 . 我正在尝试将Sheet1 B4:E的范围复制并粘贴到新的目标范围,将Sheet 2 B4:E的范围粘贴到非空行,因为在Sheet2的 F列中也有一个公式。
The script I have currently will only paste to the next available empty row, but as there is a formula in column F , it will paste the source range at the very bottom of Sheet2 . 我目前拥有的脚本仅会粘贴到下一个可用的空行,但是由于F列中有一个公式,它将脚本源范围粘贴到Sheet2的最底部。 (As the script treats the formula in F as a non empty row)
(因为脚本将F中的公式视为非空行)
Is there a way to copy a range and paste it to a specific range on another sheet whilst not overwriting any data if is re-run again? 有没有一种方法可以复制范围并将其粘贴到另一张纸上的特定范围,同时又可以再次运行而不会覆盖任何数据?
I have written 2 scripts that would do the job if I knew how to "merge" them together. 我编写了2个脚本,如果我知道如何将它们“合并”在一起,它们便可以完成工作。
Script 1: Copies and pastes a specific range to the next empty row of a target sheet, and does not overwrite when the script is re run. 脚本1:将特定范围复制并粘贴到目标工作表的下一个空白行,并且在重新运行脚本时不会覆盖。
Script 1: function transferArchive() { //TAB 1 //Target Range - Tab 1, Range 1 var sss = SpreadsheetApp.getActiveSpreadsheet(); var ss = sss.getSheetByName('Front Sheet'); var range = ss.getRange('B4:E'); var data = range.getValues(); //Destination Range - Tab 1, Range 1 var tss = SpreadsheetApp.getActiveSpreadsheet(); var ts = tss.getSheetByName('Printed POs'); ts.getRange(ts.getLastRow()+1, 2, data.length, data[0].length).setValues(data); }
Script 2 : Will copy and paste to a specific range on another sheet but overwrites every time I re run the script. 脚本2 :将复制并粘贴到另一张纸上的特定范围,但是每次我重新运行脚本时都会覆盖。
Script 2: function getdata() { //Copies source range to specific target range, but overwrites, rather than copy down into a new row. // // Target range - Copy var sourcess = SpreadsheetApp.openById("1GDMpcONdRR56QbZkA17EcvetWggltAMhzeddhNEEUG0"); var sourcesheet = sourcess.getSheetByName('Front Sheet'); var sourcerange = sourcesheet.getRange('B4:E'); var sourcevalues = sourcerange.getValues(); // Destination range - Paste var ss = SpreadsheetApp.getActiveSpreadsheet(); var destsheet = ss.getSheetByName('Printed POs'); var destrange = destsheet.getRange('B6:E'); destrange.setValues(sourcevalues); }
Instead of using .getLastRow() try calling a function that computes the last cell in a column and see if that works. 而不是使用.getLastRow(),请尝试调用一个计算列中最后一个单元格的函数,看看是否可行。 Example:
例:
function transferArchive() {
//TAB 1
//Target Range - Tab 1, Range 1
var sss = SpreadsheetApp.getActiveSpreadsheet();
var ss = sss.getSheetByName('Front Sheet');
var range = ss.getRange('B4:E');
var data = range.getValues();
//Destination Range - Tab 1, Range 1
var tss = SpreadsheetApp.getActiveSpreadsheet();
var ts = tss.getSheetByName('Printed POs');
ts.getRange(lastRow(ts, 1), 2, data.length, data[0].length).setValues(data);
}
function lastRow(sheet, colNum) {
var v = sheet.getRange(1, colNum, sheet.getLastRow()).getValues(),
l = v.length,
r;
while (l > 0) {
if (v[l] && v[l][0].toString().length > 0) {
r = (l + 2);
break;
} else {
l--;
}
}
return r ? r : 1;
}
Thanks to the help of @JPV , I made a small change to their script and now it works great. 感谢@JPV的帮助,我对他们的脚本做了一些小的更改,现在效果很好。
Below is the final script @JPV helped me with in order to answer this question: 以下是@JPV帮助我解决此问题的最终脚本:
function transferArchive() { //TAB 1 //Target Range - Tab 1, Range 1 var sss = SpreadsheetApp.openById("1GDMpcONdRR56QbZkA17EcvetWggltAMhzeddhNEEUG0"); var ss = sss.getSheetByName('Front Sheet'); var range = ss.getRange('B4:E'); var data = range.getValues(); //Destination Range - Tab 1, Range 1 var tss = SpreadsheetApp.openById("1GDMpcONdRR56QbZkA17EcvetWggltAMhzeddhNEEUG0") var ts = tss.getSheetByName('Printed POs'); ts.getRange(lastRow(ts, 2), 2, data.length, data[0].length).setValues(data); } function lastRow(sheet, colNum) { var v = sheet.getRange(1, colNum, sheet.getLastRow()).getValues(), l = v.length, r; while (l > 0) { if (v[l] && v[l][0].toString().length > 0) { r = (l + 2); break; } else { l--; } } return r; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.