Need help with office script - 'Worksheet getRange: Timeout' error

BK 6 Reputation points
2022-09-30T08:49:55.69+00:00

I am new to office script and I have created the below office script. The script is run by Power-Automate flow on total 12 files in a folder. The script runs fine on 6 files, but for remaining 6 files it give the Worksheet getRange: Timeout' error on line 'newTable.getWorksheet().getRange(address).delete(ExcelScript.DeleteShiftDirection.up);'
.Can you please help with what is causing the issue?

function main(workbook: ExcelScript.Workbook,
KeyColumn: string = "Project_Code", //Specify Key Column Name to Filter On
MainTable: string = "Table1", //Either existing OR new table name
//Default sheet name
) {
// Your code here
let selectedSheet = workbook.getActiveWorksheet( );
let usedRange = workbook.getActiveWorksheet().getUsedRange();
let newTable = workbook.addTable(usedRange, true);
const newTableRange = newTable.getRangeBetweenHeaderAndTotal();
let rowAddressToRemove: string[] = [];
const dataRows: (
| number
| string
| boolean
)[][] = newTable.getRangeBetweenHeaderAndTotal().getValues();

for (let i = 0; i < dataRows.length; i++) {
if (dataRows[i][5] === "RefDocNo  ") {

  // Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.  
  let address = newTableRange  
    .getIntersection(newTableRange.getCell(i, 0).getEntireRow())  
    .getAddress();  
  rowAddressToRemove.push(address);  
}  
if (dataRows[i][5] === "") {  


  // Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.  
  let address = newTableRange  
    .getIntersection(newTableRange.getCell(i, 0).getEntireRow())  
    .getAddress();  
  rowAddressToRemove.push(address);  
}  
if (dataRows[i][5] === "          ") {  


  // Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.  
  let address = newTableRange  
    .getIntersection(newTableRange.getCell(i, 0).getEntireRow())  
    .getAddress();  
  rowAddressToRemove.push(address);  
}  
//if (dataRows[i][4] === "PReq  ") {  
  //if (dataRows[i][13] === "           0.00 "){  


  // Get the intersection between table address and the entire row where we found the match. This provides the address of the range to remove.  
  //let address = newTableRange  
    //.getIntersection(newTableRange.getCell(i, 0).getEntireRow())  
    //.getAddress();  
  //rowAddressToRemove.push(address);  
//}}  

}

rowAddressToRemove.reverse().forEach((address) => {
newTable.getWorksheet().getRange(address).delete(ExcelScript.DeleteShiftDirection.up);
});
selectedSheet.getRange("V2").setFormulaLocal("=LEFT([@WBS Element           ],9)");
// Set number format for range V2 on selectedSheet
selectedSheet.getRange("V2").setNumberFormatLocal("0.00");
selectedSheet.getRange("V1").setValue("Project_Code");
selectedSheet.getRange("V1").copyFrom(selectedSheet.getRange("V1").getExtendedRange(ExcelScript.KeyboardDirection.down), ExcelScript.RangeCopyType.values, false, false);
selectedSheet.getRange("W:W").copyFrom(selectedSheet.getRange("N:N"), ExcelScript.RangeCopyType.all, false, false);
// Paste to range W:W on selectedSheet from range W:W on selectedSheet
selectedSheet.getRange("W:W").copyFrom(selectedSheet.getRange("W:W"), ExcelScript.RangeCopyType.values, false, false);
// Replace all "." with "" on range W:W on selectedSheet
selectedSheet.getRange("W:W").replaceAll(".", "", { completeMatch: false, matchCase: false });
// Replace all "," with "" on range W:W on selectedSheet
selectedSheet.getRange("W:W").replaceAll(",", "", { completeMatch: false, matchCase: false });
selectedSheet.getRange("W:W").insert(ExcelScript.InsertShiftDirection.right);
selectedSheet.getRange("W2").setFormulaLocal("=(TRIM(SUBSTITUTE(X2,CHAR(160),CHAR(32)))/100)");
// Paste to range W2 on selectedSheet from extended range obtained by extending down from range W2 on selectedSheet
selectedSheet.getRange("W2").copyFrom(selectedSheet.getRange("W2").getExtendedRange(ExcelScript.KeyboardDirection.down), ExcelScript.RangeCopyType.values, false, false);
selectedSheet.getRange("X:X").delete(ExcelScript.DeleteShiftDirection.left);
// Set range W1 on selectedSheet
selectedSheet.getRange("W1").setValue("Values");
// Set visibility of column(s) at range W:W on selectedSheet to true
// selectedSheet.getRange("W:W").setColumnHidden(true);

// newTable.convertToRange();
let worksheetInformation: WorksheetData[] = [];
//Define Table Name
const TableName = workbook.getTable(MainTable);
//Get all values for key column
const keyColumnValues: string[] = TableName.getColumnByName(KeyColumn).getRangeBetweenHeaderAndTotal().getValues().map(value => value[0] as string);
// Filter out repeated keys. This call to filter only returns the first instance of every unique element in the array.
const uniqueKeys = keyColumnValues.filter((value, index, array) => array.indexOf(value) === index);
console.log(uniqueKeys);
// Filter the table to show only rows corresponding to each key and then for each filter
uniqueKeys.forEach((key: string) => {
TableName.getColumnByName(KeyColumn).getFilter().applyValuesFilter([${key}]);
// Get the visible view when a single filter is active.
const rangeView = TableName.getRange().getVisibleView();
// Get values from filter
let values = rangeView.getValues()
worksheetInformation.push({
name: ${key},
data: values as string[][]
});
});
//Clear Filter
TableName.getColumnByName(KeyColumn).getFilter().clear();
return worksheetInformation
}
// An interface to pass the worksheet name and cell values through a flow.
interface WorksheetData {
name: string;
data: string[][];

}

Thanks,
BK

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
3,479 questions
0 comments No comments
{count} vote