How to Sort an Entire Used Range with Office Scripts in Excel
Question details
The user needs an Office Script to sort a complete dataset by a specific header without disconnecting row data or rearranging columns.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Automating the organization of spreadsheet data using Office Scripts.
- Observed behavior
- The current Office Script either sorts only a single column or unexpectedly rearranges the column order instead of sorting full rows.
Ensure your Excel worksheet contains a clear header row without merged cells, and that Office Scripts are enabled for your Microsoft 365 account.
Apply Row-Oriented Sorting on the Used Range
Use the getUsedRange() method and explicitly set the sort orientation to rows to keep records intact.
When an Office Script unexpectedly rearranges columns instead of sorting data rows, the sort orientation parameter is likely defaulting or set to columns. By fetching the worksheet's entire used range and explicitly setting the orientation to ExcelScript.SortOrientation.rows, you can ensure that complete records are sorted together based on your target column.
Retrieve the full dataset dynamically by adding this line to your script: const range = workbook.getActiveWorksheet().getUsedRange();
To prevent formatting issues, reset alignments by extracting the format with const format = range.getFormat(); and applying methods like format.setWrapText(false); and format.setIndentLevel(0);
Locate the column index you wish to sort by (for example, the 'Amount' column) and assign it to a variable, such as sortedColumnIndex.
Apply the sort to your range using the following syntax: range.getSort().apply([{ascending:true, key:sortedColumnIndex}], false, true, ExcelScript.SortOrientation.rows);. The SortOrientation.rows parameter ensures records are sorted properly.

Need a Lightweight Alternative for Spreadsheet Automation?
If you are struggling with complex Microsoft 365 subscriptions or Office Scripts limitations, WPS Office offers a powerful, free alternative. WPS Spreadsheet provides full compatibility with Excel files and includes a built-in JS Macro Editor, giving you a seamless JavaScript automation experience without the premium price tag.
- 1. Download and Install: Download WPS Office for free from the official website and install it on your computer.
- 2. Open Your Workbook: Launch WPS Spreadsheet and open your existing Microsoft Excel (.xlsx) file to preserve your data.
- 3. Explore JS Macros: Navigate to the Tools or Developer tab to open the WPS JS Macro editor and begin writing JavaScript automation scripts.

Frequently Asked Questions
Why is my Office Script rearranging columns instead of sorting rows?
This happens when the sort orientation parameter is set to columns (ExcelScript.SortOrientation.columns). To fix this, you must explicitly pass ExcelScript.SortOrientation.rows in your getSort().apply() method. This forces the script to use column values as the key while keeping whole rows together.
How do I dynamically select all data in an Office Script?
You can dynamically capture all populated data on a sheet by calling the getUsedRange() method on your active worksheet, like so: workbook.getActiveWorksheet().getUsedRange();. This automatically detects the outer boundaries of your data.
Can I clear formatting before sorting a range using Office Scripts?
Yes. You can extract the format object of your target range using range.getFormat(). Once extracted, you can chain commands to reset horizontal or vertical alignments, clear text wrapping with setWrapText(false), and reset text orientation prior to running your sort logic.




