主要内容

Read and Write Data from Your Google Account

R2026b

You can read and write data from your Google® account, such as Google Sheets™ spreadsheets. For example, read a Google Sheets spreadsheet as a table.

t = readtable("https://docs.google.com/spreadsheets/d/abc123")

To do this, you must authorize MATLAB® to access your Google account. Acquire your credentials (OAuth Client ID and Client Secret) from your Google account and provide them in an instance of MATLAB on desktop.

Get Your Credentials from Google Cloud Console

Complete these steps in the Google Cloud Console to acquire your project Client ID and Client Secret. Google manages API access for Google Workspace services, such as Google Sheets, through the Cloud Console.

Sign in to Google Cloud Console and Create a Project

Sign in to the Google Cloud Console and create a project to manage your credentials and API access. Each project acts as a container for your OAuth configuration and enabled APIs.

  1. Go to the Google Cloud Console: https://console.cloud.google.com

  2. Sign in with your Google account.

  3. Use Select a project to create a new project.

Configure the OAuth consent screen to define your application details and audience. This determines who can access data from your Google account.

  1. Using the left-hand navigation menu, navigate to APIs & Services > OAuth consent screen.

  2. Click Overview > Get started for new projects.

  3. Fill in required app details:

    • App name

    • User support email

    • Developer contact email

  4. Choose Audience:

    • Internal — Google Workspace only

    • External — If you do not have a Google Workspace account

  5. Finish the OAuth configuration screen and return to Overview.

Create Credentials JSON File

Create an OAuth client ID for a desktop application and save the credentials as a JSON file. You provide this file to MATLAB to let it read and write data from your Google account.

  1. Using the left-hand navigation menu, navigate to APIs & Services > Credentials.

  2. Click Create Credentials.

  3. Click OAuth client ID.

  4. Choose Application type: Desktop app.

  5. Enter a descriptive name and click Create.

  6. After you click Create, your Client ID and Client Secret are shown on the screen. You can download these as a JSON file or copy them as text.

    Note

    Save these in a location that is secure for your workflow. You will not be able to download this JSON file or see your Client Secret after this window closes. If you cannot access your Client Secret you will need to add a new one.

Enable Required APIs

Enable the APIs that your application needs to access. For example, to read and write Google Sheets data, enable the Google Sheets API.

  1. Using the left-hand navigation menu, navigate to APIs & Services > Enabled APIs & services.

  2. Click Enable APIs and services.

  3. Search for Google Sheets API. Select Google Sheets API from the results.

  4. Click Enable.

Provide Your Credentials to MATLAB

Use the Connections Panel in MATLAB to provide your credentials and complete browser-based authorization. After this process, MATLAB can access data associated with your authorized Google account.

  1. Open an instance of MATLAB.

  2. Open the Connections panel. On the Home tab, in the Environment section, click the Connections button.

  3. In the Available Connections section, select Google Sheets.

  4. Provide your JSON authentication file. Browse to the location where you saved the credentials JSON file and select it.

  5. Select the authorization scope: Read or Read & Write.

  6. MATLAB requests to open your browser to complete authorization.

  7. Select your Google account.

  8. Click Continue on the verification screen.

  9. Click Continue on the application access screen.

After this step, you reach the Authentication Successful screen and you can return to MATLAB. You can now read and write data from your Google account.

Tips

  • Going over Google's quota limit results in an error. To learn more, see Google's Usage Limits.

  • Google Sheets spreadsheets have a limit of 10 million cells across all sheets. A write operation can fail if the total cell count across all sheets in the spreadsheet exceeds this limit.

  • You cannot programmatically create a new spreadsheet in a Google Workspace Shared drive. To write to a Shared drive, create the spreadsheet manually in the Shared drive first.

  • Writing wide tables to Google Sheets can be slow because columns are auto-fitted after writing. To improve performance, set AutoFitWidth to false (the default) in functions such as writetable and writetimetable.

See Also

| | | |

Topics