Fix Excel Office Script Recording Incorrect Column Widths
Question details
The user needs to correct an issue where Excel Office Scripts record inaccurate column widths during automation tasks.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Using the Excel Action Recorder or writing Office Scripts to automate column width adjustments in a workbook.
- Observed behavior
- The generated Office Script records and applies column widths that are significantly smaller than the values manually selected in the UI, although column-hiding functionality continues to work correctly.
Before modifying your script code, duplicate your Excel workbook to prevent accidental data loss, and create a small, blank sample file to safely test API property changes.
Verify Script Width Units and Modify the Excel API Property
Check the numerical values generated by the script and adjust them manually, as Office Scripts often use points or pixels instead of the character units displayed in the Excel UI.
The Excel Action Recorder can sometimes misinterpret the UI's character-based width and output the wrong equivalent in the script API. Manually adjusting the recorded values usually resolves the discrepancy.
Navigate to the 'Automate' tab on the Excel ribbon and click on 'Code Editor' to view your generated Office Script.
Scroll through your script to find the line where the column width is being set, which typically looks like `range.getFormat().setColumnWidth(value);`.
Increase the numerical value passed into the `setColumnWidth` method. Since the API expects point values rather than character counts, you may need to experiment by typing a larger number to match your intended visual width.
Click 'Save' in the Code Editor, navigate to your test workbook, and run the script to verify if the new column width appears correct.

Report the Bug to Microsoft Q&A Office Development
If the Action Recorder consistently fails to calculate the correct width units across different workbooks, reporting the issue with a sanitized sample helps Microsoft patch the recording feature.
Use WPS Spreadsheet for Reliable Macro Automation
If Excel's Office Scripts are causing workflow disruptions with inaccurate recordings, consider switching to WPS Office. WPS Spreadsheet provides a robust and highly compatible environment for data management, complete with powerful Macro and VBA support that reliably records your formatting adjustments.

Frequently Asked Questions
Why does my Excel Office Script set the column width too small?
The Action Recorder may misinterpret the Excel UI's character-based column width and record it as points or pixels without applying the proper conversion in the script API. This results in the script applying a much smaller width when executed.
Does this recording bug affect my ability to hide columns in the script?
Generally, no. Hiding a column relies on changing its visibility property (setting the hidden flag to true), which does not depend on specific numerical width values. Hiding columns usually continues to work normally despite width recording errors.
How can I manually set the correct column width in an Office Script?
Open the Automate tab's Code Editor, locate the range you want to adjust, and use the `setColumnWidth(width)` method. You will need to input a larger point value to achieve the desired character width you see in the standard Excel UI.
Can I use VBA instead of Office Scripts to fix column width issues?
Yes. VBA uses the `ColumnWidth` property (based on characters) which closely matches the UI, or the `Width` property (based on points). If Office Scripts are bugged in your web or desktop version, utilizing standard VBA macros may provide more accurate results.




