OneDrive File Issues

How to Access an Excel File on OneDrive Using JavaScript

Emma BrownEmma Brown Nov 3, 2026 872 views

Question details

The user needs to fetch and read an Excel file hosted on OneDrive via JavaScript, but the requests are being blocked.

How to Access an Excel File on OneDrive Using JavaScript
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.
Before you start

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.

Solution 1Recommended

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).

1
Register your application

Go to the Azure portal and register your web application to obtain a Client ID and configure your redirect URIs.

2
Implement authentication

Use MSAL.js (Microsoft Authentication Library for JavaScript) in your frontend code to prompt the user for login and obtain an access token.

3
Fetch the file content

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'.

4
Process the data

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.

Use the Microsoft Graph API for Programmatic Access
API Permissions Required: Ensure your Azure AD app has the 'Files.Read' or 'Files.ReadWrite' delegated permissions granted.
Free Microsoft Office alternative

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. 1. Download WPS Office: Visit the official WPS website and click 'Download WPS Office Free' to download the installer.
  2. 2. Install the Software: Run the setup file and follow the on-screen instructions for a quick, lightweight installation.
  3. 3. Open Your Files: Launch WPS Spreadsheets and open any existing Excel workbooks to start editing immediately.
Fully compatible with Microsoft Excel formats (.xlsx, .xls, .csv).Completely free for everyday spreadsheet viewing, editing, and formatting.Lightweight installation with an intuitive, familiar tabbed interface.Built-in cloud sync for easy file management without complex developer setups.
microsoft office alternative - wps office

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.