text to columns very slow in office scripts

Suzi 21 Reputation points
2022-08-10T20:44:52.72+00:00

I used the Record Actions to do Text to Columns and used that in my code. I get problems listed in the log as:

"Invoking read methods inside of loop conditions could lead to slow performance of the script"

The script is so slow, and I know it can be made more efficient. I'd appreciate any tips. Thank you.

Here is my code:

function main(workbook: ExcelScript.Workbook) {  
               	let sheet1 = workbook.getWorksheet("Sheet1");  
          
          
        	// Text to columns on range A1:A60 on sheet1  
        	for (let row = 0; row < sheet1.getRange("A1:A60").getRowCount() ; row++) {  
        		let sourceRange = sheet1.getRange("A1:A60");  
        		let destinationRange = sheet1.getRange("A1");  
        		let sourceRangeValues = sourceRange.getRow(row).getValues()[0][0].toString().split(/[ ]/)  
        		destinationRange.getOffsetRange(row, 0).getResizedRange(0, sourceRangeValues.length - 1).setValues([sourceRangeValues]);  
        	}  
          
        }  
          
      
  
  
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,845 questions
0 comments No comments
{count} votes

Accepted answer
  1. Yutao Huang - MSFT 701 Reputation points Microsoft Employee
    2022-08-29T22:36:17.177+00:00

    @Suzi -

    You can find some general guidance/tips to improve the performance of your scripts here: https://learn.microsoft.com/en-us/office/dev/scripts/develop/web-client-performance

    Specifically, below is my attempt to optimize the text-to-columns. I did some testing on a sample workbook with 60 rows of data. The old script took almost 8 seconds and the new script took only 0.3 seconds!

    I feel there could be some room for improvement for the action recorder to generate more performant script for the "Text to Columns" action ...

       function main(workbook: ExcelScript.Workbook) {  
         let sheet = workbook.getWorksheet("Sheet1");  
         let sourceRange = sheet.getRange("A1:A60");  
         let sourceValues = sourceRange.getValues();  
         let rowCount = sourceRange.getRowCount();  
         let destinationValues: string[][] = [];  
         
         for (let row = 0; row < rowCount; row++) {  
           destinationValues[row] = sourceValues[row][0].toString().split(/[ ]/);  
         }  
         
         let maxColumnCount = destinationValues.reduce((prev, current) => Math.max(prev, current.length), 0);  
         destinationValues.forEach(row => row.length = maxColumnCount);  
         
         let destinationRange = sourceRange.getResizedRange(0, maxColumnCount - 1);  
         destinationRange.setValues(destinationValues);  
       }  
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.