Good day. Need help. I am using Power Automate to copy data from Table1 of Workbook1 into Table3 of Workbook2 using 2 Excel office scripts. The first office script copies the first 7 columns of Table1 of Workbook1 into json file. The 2nd office script is supposed to transfer the values from the json file into the first 7 column of 12 columns Table3. But when I tried to run the 2nd script it fails saying that the input array does not match the dimensions of the range. How to rectify it?
The 1st office script to generate the 7 column of Table1 of workbook1 into Json file is as below:-
function main(workbook: ExcelScript.Workbook): string {
// Get work sheet
let dailySht = workbook.getWorksheet("Sheet1");
// Get table
let dailyTab = dailySht.getTables()[0].getRangeBetweenHeaderAndTotal();
const colCnt =7 ;
let selectedTab = dailyTab.getAbsoluteResizedRange(dailyTab.getRowCount(), colCnt);
// Load data
let dailyValue = selectedTab.getTexts();
let dailyJSON = JSON.stringify(dailyValue);
// console.log(dailyJSON);
return dailyJSON;
}
The columns of Table3 of workbook2 are as below :-
[[“Dy-Group”, “Dy-MonitorType”, “Dy-Monitor”, “Dy-LastTicketNo”, “Dy-LastTicketBy”, “Dy-LastTicketDate”, “Dy-LastDayInCritical”, “Fu-Monitor”, “Fu-Group”, “Fu-Avail”, “Fu-Health”,”Fu-message”]]
The 2nd script to insert the Json file into the first 7columns of Table3 of workbook2 is as below. This is the one the fails.
function main(workbook: ExcelScript.Workbook, jsonData: string): void {
// Parse the JSON data received from Power Automate
// const parsedData = JSON.parse(jsonData);
const parsedData:string[][] = JSON.parse(jsonData);
// Get the target worksheet and table (Table3 is the 3rd table)
const targetSheet = workbook.getWorksheet("Sheet1");
const targetTable = targetSheet.getTables()[2];
// Define the number of columns to update (first 7 columns)
const numColumnsToUpdate = 7;
// Loop through the parsed data and update the table rows
for (const row of parsedData) {
// const rowData = row.slice(0, numColumnsToUpdate);
let rowData = row.slice(0, numColumnsToUpdate);
// Extract the first 7 values
targetTable.addRow(-1,rowData);
}
}
Could advise how to rectify it? Thank you very much.