Others

Fix the selectedSheet Is Not Defined Error in Office Scripts and Power Automate

Amos GikundaAmos Gikunda Nov 13, 2026 868 views

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.

Fix the selectedSheet Is Not Defined Error in Office Scripts and 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.
Before you start

Open the affected Power Automate flow and the Office Script code, and confirm the worksheet names created by your CSV conversion before testing changes.

Solution 1Recommended

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.

1
Open the Office Script in the Power Automate action

In Power Automate, edit the flow, select the Excel Online (Business) action that runs the Office Script, and open the script code.

2
Verify the selectedSheet assignment

Locate the selectedSheet variable and make sure it is assigned before use, for example: const selectedSheet = workbook.getWorksheet('Sheet1');

3
Run the flow and inspect the output workbook

After running the flow, open the generated Excel file and confirm that the converted sheet is named exactly Sheet1.

4
Rename or update the sheet reference

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.

Declare selectedSheet and Confirm the Converted Worksheet Is Named Sheet1
Fixed: With selectedSheet assigned and the Sheet1 name matched, the Office Script should run without the selectedSheet error.
Free Microsoft Office alternative

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. 1. Download WPS Office: Go to the official WPS Office download page and install WPS Office on your computer.
  2. 2. Open the converted workbook: Launch WPS Spreadsheets, click Menu > Open, and select the .xlsx workbook generated by Power Automate.
  3. 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.
Opens and saves Microsoft Excel .xlsx files, including workbooks created from CSV by Power Automate.Free, lightweight office suite with spreadsheet features for reviewing and correcting converted data.Familiar Microsoft Office-style ribbon interface makes switching from Excel easy.Seamless migration for Excel workbooks without requiring an Office 365 subscription.
microsoft office alternative - wps office

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.