rrh
rrh

Reputation: 838

Copy table from one sheet to another

I would like to copy the whole table from one sheet(inputSheet) to another(outputSheet), and then, keep copying & adding more tables to the same ouputSheet

Here are some things I have tried:

function main(workbook: ExcelScript.Workbook) {
  const inputSheet = workbook.getWorksheet("Input")
  const outputSheet = workbook.getWorksheet("Output")


  // Worksheet addTable: The argument is invalid or missing or has an incorrect format.
  const tableRange = inputSheet.getTables()[0].convertToRange()
  const table = outputSheet.addTable(tableRange, true)

 
  // Worksheet addTable: The argument is invalid or missing or has an incorrect format.
  const tablaRange2 = inputSheet.getTables()[0].getRange()
  const tabla2 = outputSheet.addTable(tablaRange2, true)


  // this one kind of work but :
  //   1- have to know the range in advance
  //   2- the second table I copy(to the same outputSheet), gives an error saying that headers can't have rich text. 
  const newRange = outputSheet.getRange("A1:Z20")
    .copyFrom(inputSheet.getRange("A1:Z20"), ExcelScript.RangeCopyType.all, false, false);
  const newTable = eneSheet.addTable("A1:Z20", false)
}

I'm hoping there's something straightforward similar to this way to copy a sheet:

  const newSheet = previousSheet.copy(ExcelScript.WorksheetPositionType.after, anotherSheet)

But haven't found a table or sheet method that would do that.

Upvotes: 2

Views: 2186

Answers (1)

Sudhi Ramamurthy
Sudhi Ramamurthy

Reputation: 2478

If you are dealing with tables containing similar structure, you'll need to write some logic to combine them. There is no API to create a new copy of the table. Adding rows from other similar tables - that's a very custom logic.

Checkout this project here for two variations of the logic (as I'm not sure which one suites your needs). Do checkout the step-by-step video to see how this works. If this is not the scenario you have in mind, please let me know.

https://github.com/sumurthy/officescripts-projects/tree/main/Copy%20Tables%20to%20Master%20Table

Upvotes: 4

Related Questions