Skip to Content

ONEDRIVE_CSV

Overview

This function retrieves a CSV file from the user’s Microsoft OneDrive App Folder using the Microsoft Graph API and a global variable graphToken for authentication, which is set if the user is signed in. It returns the CSV contents as a 2D list, suitable for use in Excel. It can be adapted to also load a CSV file into a pandas DataFrame to load very large CSV files from your OneDrive App Folder that would otherwise be too large to open directly in Excel, or if you prefer to not use Power Query or other manual steps to import the data.

Usage

To use the function in Excel:

=ONEDRIVE_CSV(file_path)
  • file_path (string, required): The path to the CSV file in your OneDrive App Folder (e.g., /Apps/Boardflare Python for Excel/your-file.csv).

The function returns the contents of the CSV file as a 2D list (table) suitable for Excel.

Examples

Example 1: Load a CSV file

In Excel:

=ONEDRIVE_CSV("/Apps/Boardflare Python for Excel/data.csv")

Expected output:

NameValue
Alice10
Bob20

This means the CSV file is loaded as a 2D array in Excel, ready for further analysis.

Python Code

import requests import csv import io def onedrive_csv(file_path): """ Retrieves a CSV file from the user's Microsoft OneDrive App Folder using the Microsoft Graph API and a global variable `graphToken` for authentication. Returns the CSV contents as a 2D list, suitable for use in Excel. Args: file_path: The path to the CSV file in the user's OneDrive App Folder (e.g., '/Apps/Boardflare Python for Excel/your-file.csv'). Returns: 2D list representing the CSV file's contents, or an error message as a string. This example function is provided as-is without any representation of accuracy. """ token = globals().get("graphToken", None) if not token: return "Microsoft Graph token is not set. Please click login button next to OneDrive in Functions tab." if not file_path or not isinstance(file_path, str): return "A valid file_path string must be provided." url = f"https://graph.microsoft.com/v1.0/me/drive/root:{file_path}:/content" headers = { "Authorization": f"Bearer {token}", "Accept": "text/csv" } try: response = requests.get(url, headers=headers) if response.status_code != 200: return f"Failed to retrieve file: {response.status_code} {response.text}" csv_content = response.content.decode('utf-8') reader = csv.reader(io.StringIO(csv_content)) table = [row for row in reader] return table except Exception as e: return f"Error: {str(e)}"

Live Notebook

Edit this function in a live notebook.

Live Demo

Last updated on