Forum Discussion

Kipopstok's avatar
Kipopstok
Copper Contributor
May 28, 2024
Solved

Simple automate script mangles number formatting

I have a new laptop and I thought - i/o moving my macro - to create an Automated script. I recorded the script with the same steps as the macro: Select column A > data > text to column > Delimited ...
  • JKPieterse's avatar
    May 29, 2024

    Kipopstok The reason it fails is because your recorded code converts string values to numbers. But Office Script expects US syntax for the numbers. So the comma poses issues, as that is normally the thousands separator.

    You could try this version of the script (adjust the ranges):

    function main(workbook: ExcelScript.Workbook) {
    	let selectedSheet = workbook.getActiveWorksheet();
    	// Text to columns on range A1:A3 on selectedSheet
    	for (let row = 0; row < selectedSheet.getRange("A1:A3").getRowCount() ; row++) {
    		let sourceRange = selectedSheet.getRange("A1:A3");
    		let destinationRange = selectedSheet.getRange("A1");
    		let sourceRangeValues = sourceRange.getRow(row).getValues()[0][0].toString().replace(/,/g,".").split(/[\t|]/)
    		destinationRange.setNumberFormat("General");
    		destinationRange.getOffsetRange(row, 0).getResizedRange(0, sourceRangeValues.length - 1).setValues([sourceRangeValues]);
    	}
    }

Resources