简体   繁体   English

Google脚本:如何将范围复制并粘贴到不为空的行中的新表中

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

相关问题 谷歌脚本将数据从工作表 1 复制到第一个空行工作表 2 - google script copy data from sheet 1 to first empty row sheet 2 如果值存在于工作表 1 和工作表 2 中,则复制行并将其粘贴到工作表 3。Google 表格。 Google Apps 脚本 - Copy row if value exists in both sheet 1 and sheet 2 and paste it to sheet 3. Google Sheets. Google Apps Script 如何连续搜索昨天的日期,然后将值复制/粘贴到新的 Google 表格 - How to search for yesterday's date in a row, then copy / paste values to new Google Sheet 从工作表 A 复制粘贴到工作表 B 的最后一行谷歌应用程序脚本 - Copy fro Sheet A to paste in Sheet B's last row google app script Google Spreadsheets 脚本 - 如何将范围从一张纸复制到另一张纸,但不覆盖非空目标单元格 - Google Spreadsheets script - How to copy range from one sheet to another sheet, but without overwriting non-empty target cells 如何使用 Google 脚本从 1 个 Google 工作表中复制特定值并粘贴到另一个工作表? - How can I copy specific values from 1 Google Sheet and paste to another Sheet using Google Script? 我需要将可变范围的数据复制到另一张纸的新空行中。 我需要如何更改这些脚本? - I need to copy variable range of data into a new empty row of another sheet. How do I need to change those scripts? 如何使用 Google Sheet Script 将数据范围从一个工作表复制到另一个工作表 - How to Copy a data range from one sheet to another sheet using Google Sheet Script Google表格将行移动到新表格的脚本 - Script for Google Sheets to Move Row to New Sheet Google脚本-将动态范围复制到新的电子表格 - Google Script - Copy dynamic range to new spreadsheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM