Fix Excel Office Scripts setColumnWidth Producing Incorrect Widths
Question details
The user needs to fix or work around an issue where the Excel Office Scripts setColumnWidth method creates narrower columns than specified.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating column width adjustments using Excel Office Scripts.
- Observed behavior
- The setColumnWidth method calculates and sets an incorrect, much narrower column width than the value defined in the script.
Ensure that your Office Script targets the correct worksheet and cell range, and verify that no subsequent formatting operations or macros are overwriting your initial column width settings.
Use Adjusted Values as a Temporary Workaround
Since this is a known calculation bug within Excel Office Scripts (tracked internally by Microsoft as #9333095), the most reliable immediate fix is to manually scale your width values until the desired visual width is achieved.
The setColumnWidth method currently miscalculates the unit mapping, producing narrower columns. Until an official patch is deployed, developers must use a trial-and-error approach to offset the calculation discrepancy.
Run your current Office Script and measure the actual output column width compared to your intended width to find the scaling ratio.
Multiply your desired column width value by the discrepancy ratio and mathematically increase the argument passed to the setColumnWidth(value) method in your code editor.
Execute the script again in the Excel workbook and tweak the numeric value iteratively until the displayed column width exactly meets your visual requirements.

Verify Script Execution Order and Formatting Overwrites
Ensure that the incorrect width isn't being caused by conflicting script commands running after the setColumnWidth method.
Try WPS Office for Reliable Spreadsheet Automation
If you are frustrated by Office Script bugs affecting your workflow, consider switching to WPS Office. WPS Spreadsheet offers a lightweight, highly compatible alternative with robust support for traditional VBA macros, ensuring your automation runs exactly as intended without unexpected formatting glitches.
- 1. Download WPS Office: Visit the official WPS website and download the free WPS Office suite for your operating system.
- 2. Open your spreadsheet: Launch WPS Spreadsheet and easily open your existing .xlsx or .xlsm files.
- 3. Run your macros: Navigate to the Developer tab to reliably manage and execute your formatting macros with precision.

Frequently Asked Questions
Why does setColumnWidth produce narrower columns in Excel Office Scripts?
This is a known calculation bug in Excel Office Scripts (Microsoft internal tracking #9333095) where the method miscalculates the pixel or character width mapping, resulting in columns that are significantly narrower than the specified numeric value.
Are there more stable alternatives to setColumnWidth in Excel automation?
If Office Scripts are failing, you can rely on traditional VBA macros. Using the Columns.ColumnWidth property in VBA remains highly stable and accurate for setting exact column dimensions without the calculation bugs present in the newer Office Scripts.
Can other automated formatting operations affect my column widths?
Yes. If your script uses getFormat().autofitColumns() after manually setting the column width, or if external data refreshes trigger table resizing, they will overwrite your predefined width settings. Always place static width adjustments at the end of your script.




