How to Access an Excel File on OneDrive Using JavaScript
Question details
The user needs to fetch and read an Excel file hosted on OneDrive via JavaScript, but the requests are being blocked.

- Product
- Microsoft OneDrive / Excel
- Device & OS
- not provided
- Scenario
- Attempting to programmatically access or download a shared Excel workbook on OneDrive from a browser-based JavaScript application.
- Observed behavior
- Browser fetch requests to the OneDrive sharing URL fail because of cross-origin resource sharing (CORS) restrictions, missing authentication headers, or redirects.
Determine your primary goal: embedding the file for viewing, triggering a direct download, or parsing the workbook's raw data. Direct browser fetch requests to public OneDrive URLs are usually blocked by CORS policies, requiring a different approach.
Use the Microsoft Graph API for Programmatic Access
The official and most secure way to read Excel files on OneDrive via JavaScript is by utilizing the Microsoft Graph API.
Microsoft Graph provides a unified programmability model to access data in Microsoft 365, including OneDrive and Excel. It handles authentication securely and prevents browser CORS issues when configured correctly with Azure Active Directory (Azure AD).
Go to the Azure portal and register your web application to obtain a Client ID and configure your redirect URIs.
Use MSAL.js (Microsoft Authentication Library for JavaScript) in your frontend code to prompt the user for login and obtain an access token.
Make an authenticated GET request using the token to the Graph API endpoint: 'https://graph.microsoft.com/v1.0/me/drive/items/{item-id}/content'.
Retrieve the response as a Blob or ArrayBuffer and pass it into a JavaScript spreadsheet parsing library (like SheetJS) to read the rows and columns.

Implement a Server-Side Proxy to Bypass CORS
If you cannot use the Graph API for a public file, use a backend server to fetch the file on behalf of your frontend JavaScript.
Embed the File Using Excel for the Web
If your goal is simply to display the Excel file to users rather than extracting its data programmatically, embedding is the fastest approach.
Seamlessly Manage and Edit Spreadsheets with WPS Office
While programmatic access to OneDrive APIs can be complex due to Microsoft's ecosystem restrictions, editing your data shouldn't be. WPS Office provides a lightweight, free, and fully compatible alternative to Microsoft Office for all your daily spreadsheet needs.
- 1. Download WPS Office: Visit the official WPS website and click 'Download WPS Office Free' to download the installer.
- 2. Install the Software: Run the setup file and follow the on-screen instructions for a quick, lightweight installation.
- 3. Open Your Files: Launch WPS Spreadsheets and open any existing Excel workbooks to start editing immediately.

Frequently Asked Questions
Why do I get a CORS error when fetching a OneDrive file?
OneDrive sharing URLs do not include the CORS (Cross-Origin Resource Sharing) headers required by web browsers. Browsers block these requests by default to prevent unauthorized scripts from accessing external data.
Can I convert a OneDrive share link to a direct download link?
Yes, you can often modify the URL by replacing '?web=1' with '?download=1'. However, fetching this direct download link via browser-based JavaScript will still typically result in a CORS error, requiring a backend proxy or Graph API.
What is the best JavaScript library to parse fetched Excel data?
Once you successfully fetch the Excel file as an ArrayBuffer or Blob (using the Microsoft Graph API or a backend proxy), libraries like SheetJS (xlsx) or ExcelJS are highly recommended for extracting and manipulating the spreadsheet data.




