Copy data between 2 sheets on top or bottom using an office script in an excel report

This online script allows me to copy the full data of a table to another table from one sheet to another sheet. By this way, I combine 2 tables into 1 single one in 1 sheet.

Sheet 1 Sheet 2
script excel
script excel script excel

 

When I use the script ?

When I have to merge 2 tables into 1 in 1 single sheet.

 

How to create the script ?

Read How to create, edit and select an Office Script in an excel report

 

How to create the button to associate it with the script ?

Read How to create a button and associated it to an Office Script in an excel report

 

How is/are the script(s) ?

Copy the code below and paste it into your script. You will see my comments in green if exist so follow the help to adapt to your need.

Copy from table1 on sheet1 to table2 on sheet2 at the end:


function main(workbook: ExcelScript.Workbook) {
  // change sheet1 and sheet2 by yours  
  let ssrc1 = workbook.getWorksheet('Sheet1');
  let ssrc2 = workbook.getWorksheet('Sheet2');
  // change table1 and table2 by yours
  let tsrc = ssrc1.getTable("Table1");
  let tdes = ssrc2.getTable("Table2");
  let fcelldes = tdes.getRange().getColumn(0).getLastCell().getOffsetRange(1, 0);
  fcelldes.copyFrom(tsrc.getRangeBetweenHeaderAndTotal(), ExcelScript.RangeCopyType.all, false, false);
}              
              
script excel

Copy from table1 on sheet1 to table2 on sheet2 at the top:


function main(workbook: ExcelScript.Workbook) {
  // change sheet1 and sheet2 by yours
  let ssrc1 = workbook.getWorksheet('Sheet1');
  let ssrc2 = workbook.getWorksheet('Sheet2');
  // change table1 and table2 by yours
  let tsrc = ssrc1.getTable("Table1");
  let rowCount = tsrc.getRowCount();
  let tdes = ssrc2.getTable("Table2");
  for (let i = 0; i < rowCount; i++) { tdes.addRow(0); }
  let fcelldes = tdes.getRangeBetweenHeaderAndTotal().getCell(0, 0);
  fcelldes.copyFrom(tsrc.getRangeBetweenHeaderAndTotal(), ExcelScript.RangeCopyType.all, false, false);
}              
              
script excel

Interesting Topics