简体   繁体   English

如何将范围从母版表复制到另一张表的最后一行(另一张表名称= Mastersheet Z1中的单元格值)

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

相关问题 1:从工作表 1 static 行更新到 MasterSheet 动态行 2:根据单元格值显示和隐藏列和行 - 1:Update from Sheet 1 static row to MasterSheet dynamic row 2:Show & Hide columns and rows base on cell value 使用主表中的一行数据自动创建一个新表 - Automatically create a new sheet with data of a row in the mastersheet 根据单元格值将最后一行从一个工作表复制到另一个工作表,并在主工作表中不断更新 - Copy last row from one sheet to another depending on cell value with constant updates in master sheet 从范围复制单元格值并将其粘贴到另一个工作表的单行中 - To copy Cell values from a range and paste it in a single row of another sheet 从1张纸中复制值并将值设置到另一张纸的最后一行 - Copy values from 1 sheet and set values to last row of another sheet 将谷歌工作表中的数据复制到另一个谷歌工作表的最后一行 - Copy data from a google sheet to the last row in another google sheet 将活动行复制到另一个工作表,最后一行第一个空单元格 - Copy active row to another sheet with last row first empty cell 如何将范围从一个工作表复制到另一工作表的范围,然后重复复制到下一行 - How to copy a range from one sheet to a range in another sheet, then repeat copying to the next row down Google Sheet Script:通过最后一行唯一值查找另一个工作表范围中单元格的行号,然后更新目标行的列 - Google Sheet Script: Find row number of a cell in another sheet range by last row unique value then update columns of target row 根据单元格值将特定范围(不是整行)复制到另一个Google工作表 - Copy over a specific range not whole row to another google sheet based on cell value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM