[英]How to copy a range from a mastersheet to the last row of another sheet (Another sheet name = cell value in Mastersheet Z1)
I have a mastersheet named "ENTRATE MAIN", I want to copy range "A2:J50" to the last column of another sheet whose name is found in Cell Z1 in the Mastersheet. 我有一个名为“ ENTRATE MAIN”的工作表,我想将范围“ A2:J50”复制到另一个工作表的最后一列,该工作表的名称在工作表的单元格Z1中找到。 But the code needs to check if the data is already copied first.
但是代码需要检查是否先复制了数据。
I'm actually new to Google App Script, so I've actually tried using some formulas but they dont do the job since the data in the master sheet is dynamic. 我实际上是Google App Script的新手,所以实际上我尝试使用一些公式,但是由于主表中的数据是动态的,因此它们无法完成工作。
I have looked at the code in this URL but it doesn't exactly what I want. 我已经看过了该URL中的代码,但这并不是我想要的。 Copy data from one sheet to the last row of another sheet
将数据从一张纸复制到另一张纸的最后一行
I expect to check if the master sheet data is already there in the other sheet. 我希望检查其他工作表中是否已存在主工作表数据。 I also need it to copy to the last row of the sheet name in cell Z1 of mastersheet
我还需要将其复制到母版表的单元格Z1中工作表名称的最后一行
Maybe this is what you are looking for. 也许这就是您想要的。 Keep in mind that it's not possible to check for the
Master data
in the second sheet
without using the name in Z1
, unless you manually input the name of the second Sheet in the code first. 请记住,除非先在代码中手动输入第二张工作表的名称,否则不使用
Z1
的名称就无法在second sheet
检查Master data
。
function main(){
//Master Sheet
var sprsheet = SpreadsheetApp.getActiveSpreadsheet();
var master_sheet = sprsheet.getSheetByName("ENTRATE MAIN");
var master_range = master_sheet.getRange("AJ2:J50");
//Second Sheet
var sheetName = master_sheet.getRange("Z1").getValue();
var second_sheet = sprsheet.getSheetByName(sheetName);
var lastrow = second_sheet.getLastRow();
var master_data = master_range.getValues();
if (lastrow > 50){ //This is in case your second sheet has less than 50 rows before copying the data
//We have to check the previous 49 rows in case the data is already there
var second_range = second_sheet.getRange("AJ"+(lastrow-49)+":J"+(lastrow));
var second_data = second_range.getValues();
if (!isCopied(master_data, second_data)){
//Data is not there so we copy it in the next 49 rows
//If you want to overwrite the last row, just remove the +1
second_range = second_sheet.getRange("AJ"+(lastrow+1)+":J"+(lastrow+49));
second_range.setValues(master_data);
}
} else {
//The second sheet has less than 50 rows so the data is obviously not there
var second_range = second_sheet.getRange("AJ"+(lastrow+1)+":J"+(lastrow+49));
second_range.setValues(master_data);
}
}
function isCopied(master_data, second_data){
for (i in master_data){
for (j in master_data[i]){
if (master_data[i][j] != second_data[i][j]){
return false;
}
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.