Fix Office Script Error: Cannot Read Properties of Undefined in Excel
Question details
The user needs to resolve an Office Script error that occurs when deleting and hiding specific Excel worksheets.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Running an Office Script to delete multiple worksheets while keeping and modifying the visibility of specific sheets.
- Observed behavior
- The script fails with the error 'Cannot read properties of undefined (reading setVisibility)' because it attempts to modify a worksheet object that has been deleted or cannot be found.
Make sure to review your script logic to ensure no worksheet references are reused after a deletion operation.
Verify Worksheet Object Existence Before Modifying Visibility
Check if the worksheet object exists in the current collection before calling the setVisibility method.
The 'Cannot read properties of undefined' error happens because the script tries to access a worksheet reference that has already been deleted or is misspelled. Rebuilding or refreshing the worksheet collection after a deletion prevents the script from calling properties on non-existent objects.
Open your Office Script code and locate the exact line where the 'setVisibility' method is called.
Wrap the visibility change in an if-statement to verify the worksheet object exists (e.g., if (sheet !== undefined)).
If you are iterating through a list of sheets and deleting some of them, re-fetch the active workbook's sheets (using workbook.getWorksheets()) before attempting to hide the remaining ones.

Consult the Microsoft Office Scripts Developer Forum
Seek specialized code-level assistance from the Microsoft Excel developer community.
Use WPS Office for Powerful Macro and Spreadsheet Automation
Having trouble with complex Excel Office Scripts? WPS Office offers robust spreadsheet capabilities with built-in VBA and modern JavaScript Macro (JSA) support, providing a lightweight, highly compatible, and cost-free alternative to Microsoft Office.
- 1. Download WPS Office: Visit the official WPS website to download and install the free suite.
- 2. Open Your Spreadsheet: Launch WPS Spreadsheet and easily open your existing Excel workbooks.
- 3. Manage Macros: Navigate to the Tools tab to start using VBA or JSA macros for your automation needs.

Frequently Asked Questions
What does 'Cannot read properties of undefined' mean in Office Scripts?
This error means your script is trying to read or modify an object (like a worksheet's visibility) that doesn't exist, either because it was already deleted or the reference name is incorrect.
Can I recover a worksheet deleted by an Office Script?
Generally, actions performed by Office Scripts on the web cannot be undone using the standard Undo button. You must restore your workbook from a previous version history or backup.
Does WPS Office support Microsoft Excel macros?
Yes, WPS Office supports both traditional VBA macros and modern JavaScript macros (JSA), ensuring high compatibility with your existing Excel automated workflows.




