Fix Excel Add-ins Cannot Read Cookies with JavaScript API
Question details
The user needs to understand why document.cookie returns an empty value in an Excel Office Add-in and is looking for safer alternative ways to store authentication tokens.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Developing or troubleshooting an Excel Office Add-in that relies on client-side JavaScript to read authentication cookies.
- Observed behavior
- The document.cookie property returns an empty value because the Office Add-in environment restricts cookie access.
Before modifying your add-in's authentication flow, check your browser's privacy and third-party cookie settings, as strict tracking prevention can further impact iframe storage behavior.
Configure the Shared Runtime in Your Add-in Manifest
Using a shared runtime allows the task pane, custom functions, and dialogs to run in the same browser runtime, potentially bypassing some cookie and storage isolation issues.
By default, Excel custom functions run in a separate JavaScript runtime from the task pane. This separation means they do not share global variables or the same cookie context.
Configuring your add-in to use a shared runtime ensures all components execute in the same hidden browser process, unifying access to local storage and applicable cookies.
Locate the manifest.xml file in the root directory of your Excel Add-in project.
Insert the <Requirements> tag and specify <Set Name="SharedRuntime" MinVersion="1.1"/> to ensure the host supports it.
Add a <Host Name="Workbook"> section and define your <Runtime resid="Taskpane.Url" lifetime="long" /> to connect the UI and custom functions.

Use Web Storage APIs for Authentication Tokens
Since cookies are restricted inside the sandboxed iframe of Office Add-ins, web storage APIs like localStorage or sessionStorage offer a more reliable alternative for storing non-sensitive tokens.
Implement the Office Dialog API for Authentication
For highly sensitive authentication flows (like OAuth 2.0), utilize the Office Dialog API to handle credential input outside the restricted iframe.
Try WPS Office for Your Spreadsheet Needs
While developing and troubleshooting custom JavaScript add-ins for Excel can lead to complex iframe sandboxing and authentication hurdles, managing your daily spreadsheet tasks shouldn't be difficult. WPS Office offers a free, lightweight, and highly compatible alternative to Microsoft Office, providing powerful built-in tools without the need to rely on complex third-party extensions.
- 1. Download the installer: Visit the official WPS Office website and download the free installation package for your operating system.
- 2. Install WPS Office: Run the setup file and follow the on-screen instructions for a quick, lightweight installation.
- 3. Open your spreadsheets seamlessly: Launch WPS Spreadsheets and open your existing .xlsx files directly. The format and layouts will be perfectly preserved.

Frequently Asked Questions
Why do custom functions in Excel act differently with cookies?
By default, Excel custom functions execute in a separate JavaScript runtime independent of the main task pane. Because they run in an isolated environment, they do not share the same cookie, localStorage, or sessionStorage context as the task pane unless a Shared Runtime is explicitly configured in the manifest.
Can I use localStorage instead of cookies in an Office Add-in?
Yes. While cookies are often blocked due to strict third-party iframe restrictions, localStorage and sessionStorage are generally supported within the task pane's browser control context and serve as the recommended alternatives for storing authentication tokens.
Does the user's browser choice impact cookie access in Excel Add-ins?
Yes. Excel Add-ins use built-in browser controls (like Edge WebView2 on Windows or WebKit on Mac). If the underlying browser's privacy settings are configured to block third-party cookies or enforce strict tracking prevention, the add-in will be unable to read or write standard cookies.




