I need your help, I have an Excel file type .xlsx, which was sent automatically from a system to my email, this Excel comes with the information distributed in several tabs within the same file, what I want to do is an Office script that takes the information from each of the sheets and unifies it into a single sheet, once all the data has been copied, delete all the sheets and the file is left with a single tab, with all the data unified. Thanks for your help.
function main(workbook: ExcelScript.Workbook) {
// Get all the worksheets in the workbook
let sheets = workbook.getWorksheets();
// Create a new worksheet named "Consolidate"
let consolidateSheet = workbook.getWorksheet("Consolidate") || workbook.addWorksheet("Consolidate");
// Loop through each sheet in the workbook
for (let sheet of sheets) {
// Get the data range of the current sheet
let dataRange = sheet.getUsedRange();
// Get the values from the data range
let values = dataRange.getValues();
// Find the last row with data in the Consolidate sheet
let lastRow = consolidateSheet.getUsedRange().getRowCount();
// Get the range where the data will be pasted in the Consolidate sheet
let targetRange = consolidateSheet.getRange(lastRow + 1, 1, values.length, values[0].length);
// Paste the values into the Consolidate sheet
targetRange.setValues(values);
}
}
let values = dataRange.getValues()includes the header row, then there are multiple header on Consolidate sheet.getRangeByIndexes()andgetRange()are different.Update:
concatmethod to consolidate the data and writing data to sheet all at once are more efficient.