How to Fix Excel Office Script Sorting Columns Instead of Rows in Power Automate
Question details
The user needs to correct an Excel Office Script used in Power Automate that is sorting columns left-to-right rather than rows top-to-bottom.

- Product
- Excel Office Scripts / Power Automate
- Device & OS
- not provided
- Scenario
- Automating data sorting operations in an Excel spreadsheet using an Office Script triggered via Power Automate.
- Observed behavior
- The script unexpectedly reorganizes the columns instead of the rows due to an incorrectly configured sort orientation parameter in the Sort.apply method.
Ensure you have editing permissions for the specific Excel file and access to the Power Automate flow containing the Office Script.
Update Sort Orientation to Rows in Sort.apply Method
Change the sorting parameter in your script from columns to rows to ensure the data is sorted vertically as intended.
When the ExcelScript.SortOrientation.columns property is used in the getSort().apply method, the script reorganizes columns horizontally. To fix this, you must explicitly declare row orientation.
Navigate to the Power Automate portal, locate your flow, and open the step that runs the Excel script, or open the script directly via the Automate tab in Excel on the web.
Find the line of code where the sorting is applied to your range, which typically looks like: `.getSort().apply(...)`.
Replace `ExcelScript.SortOrientation.columns` with `ExcelScript.SortOrientation.rows` at the end of your apply method's parameters.
Ensure your sort keys are relative to the selected range. For example: `selectedSheet.getRange("A1:D7733").getSort().apply([{key: 0, ascending: true}], false, true, ExcelScript.SortOrientation.rows);`.

Try WPS Office for Advanced Data Processing and Macros
While Power Automate and Office Scripts are tied to Microsoft 365, WPS Office offers a powerful, lightweight, and free alternative with excellent compatibility. Enjoy built-in JS Macros, VBA support, and familiar data sorting tools without complex cloud integrations.
- 1. Download WPS Office: Visit the official WPS website and download the free installation package.
- 2. Install and Open: Follow the simple on-screen instructions to install the suite and open WPS Spreadsheet.
- 3. Automate with Macros: Open your Excel workbooks and use the robust JS Macro editor under the Tools tab to automate your sorting tasks.

Frequently Asked Questions
Why is my Excel Office Script sorting left to right instead of top to bottom?
This happens when the sorting orientation in your script is set to 'ExcelScript.SortOrientation.columns'. Changing it to 'ExcelScript.SortOrientation.rows' will restore standard top-to-bottom sorting.
How do I specify multiple columns to sort by in an Office Script?
You can pass an array of sort conditions to the apply method. For example: `[{key: 0, ascending: true}, {key: 1, ascending: false}]` will sort by the first column in ascending order, and then by the second column in descending order.
Are sort keys in Office Scripts absolute column letters or numbers?
No, sort keys are zero-based and relative to the range you have selected in your script. If your specified range is 'C1:F10', then 'key: 0' refers to column C.




