Fix the selectedSheet Is Not Defined Error in Office Scripts and Power Automate
Question details
Resolve the selectedSheet is not defined runtime error that occurs when using the Microsoft Office Scripts Convert CSV Files to Excel Workbooks template in Power Automate.

- Product
- Microsoft Office Scripts in Power Automate
- Device & OS
- not provided
- Scenario
- The user is automating CSV to Excel workbook conversion with a Power Automate flow and the Office Scripts template, and the script stops with a variable reference error.
- Observed behavior
- The script fails with selectedSheet is not defined because selectedSheet is not assigned or the expected worksheet named Sheet1 is not present in the converted workbook.
Open the affected Power Automate flow and the Office Script code, and confirm the worksheet names created by your CSV conversion before testing changes.
Declare selectedSheet and Confirm the Converted Worksheet Is Named Sheet1
Start with this solution when you run the Microsoft template without changes, because the template expects a worksheet named Sheet1.
The standard Convert CSV Files to Excel Workbooks template often uses the exact name Sheet1 in its worksheet reference. If the conversion output uses a different sheet name, the variable may remain undefined.
In Power Automate, edit the flow, select the Excel Online (Business) action that runs the Office Script, and open the script code.
Locate the selectedSheet variable and make sure it is assigned before use, for example: const selectedSheet = workbook.getWorksheet('Sheet1');
After running the flow, open the generated Excel file and confirm that the converted sheet is named exactly Sheet1.
If the sheet is not named Sheet1, rename it in the output workbook, or change the script's worksheet reference to the actual sheet name.

Modify the Office Script to Select the First Worksheet by Position
Use this if your CSV files create different worksheet names or you do not want the script to depend on the fixed name Sheet1.
Use WPS Office as a Free Alternative for Working with Converted Excel Workbooks
Since WPS Office does not run Microsoft Office Scripts or Power Automate flows, use WPS Spreadsheets as a free, lightweight alternative for opening, reviewing, and editing the .xlsx workbooks produced by your automation. It supports familiar Excel workbooks and a ribbon-style interface for easy migration.
- 1. Download WPS Office: Go to the official WPS Office download page and install WPS Office on your computer.
- 2. Open the converted workbook: Launch WPS Spreadsheets, click Menu > Open, and select the .xlsx workbook generated by Power Automate.
- 3. Review and save the file: Check the worksheet names, edit cells if needed, and save the file as .xlsx to stay compatible with Excel.

Frequently Asked Questions
Why does the Office Scripts Convert CSV Files to Excel Workbooks template show selectedSheet is not defined?
The template normally expects a worksheet named Sheet1. If the CSV conversion creates a sheet with another name, or if selectedSheet is not assigned before it is used, the script fails with this error.
How can I fix the error without changing the CSV file sheet name?
Edit the Office Script and select the first worksheet by position using workbook.getWorksheets()[0] instead of workbook.getWorksheet('Sheet1').
Can WPS Office run Microsoft Office Scripts or Power Automate flows?
No, WPS Office does not execute Microsoft Office Scripts or Power Automate flows, but it can open and edit the Excel workbooks those flows create.
How do I open a Power Automate-created Excel workbook in WPS Office?
Install WPS Office, open WPS Spreadsheets, click Menu > Open, select the generated .xlsx file, and review the sheet names and data.




