Matlab to Google Sheets (matlab2sheets)

This function (and run-once helper function) exports data to a Google spreadsheet
1.8K Downloads
Updated 21 Sep 2017

Use this together with GetGoogleSpreadsheet from the File Exchange and you can read/write to Google Sheets from MATLAB. Inspired by an older set of functions (Matlab to Google Spreadsheets) that no longer works with Google's new v4 API.
--
status = mat2sheets(spreadsheetID, sheetID, sheetpos, d)
% This function takes values from an array or cell array and places them in
% a Google spreadsheet. It requires the one-time use of RunOnce (see below)
% SYNTAX: status = mat2sheets(spreadsheetID, sheetID, pos, d)
%
% ARGUMENTS:
% spreadsheetID: (string), identifier from URL of your Google Sheet
% sheetID: (string), another identier from URL
% pos: 1x2 array with indices for [sheetrow, sheetcolumn]
% to start pasting data
% d: array or cell array of data to paste into Sheet. If
% [], pos indicates row or range of rows [start stop]
% to delete
%
% RETURNS: status (0=failed, 1=success)
%
% EXAMPLES:
% For sheet with the following URL:
% https://docs.google.com/spreadsheets/d/1GPd-vBsX5VUejz5hrxE/edit#gid=552
%
% A call may look like:
% mat2sheets('1GPd-vBsX5VUejz5hrxE', '552', [2 3], [1 2 3 4 5])
%
% Would put values 1,2,3,4,5 into cells C2,D2,E2,F2,G2, respectively
%
% To delete row(s)
% mat2sheets('1GPd-vBsX5VUejz5hrxE', '552', 2, []) % delete row 2
% mat2sheets('1GPd-vBsX5VUejz5hrxE', '552', [2 10], []) % delete rows 2-10 inclusive
%
% USING RunOnce().
% Before using this code, you've got to enable the Drive/Sheets APIs via:
% https://console.developers.google.com/
% Here, you will "create credentials" via an OAuth 2.0 client ID that comes
% with a Client ID and Client Secret. These codes are the two arguments of
% RunOnce(client_id, client_secret). Run RunOnce with both of these
% codes passed as strings, and follow the instructions.
%
% The following code is inspired by, and makes use of, code
% originally published in the file exchange by Claudiu Giurumescu.
% (https://www.mathworks.com/matlabcentral/fileexchange/31221-matlab-to-google-spreadsheets)
%
% I wrote this to accomodate for latest changes in Google API, added some
% comments, and simplified it all so that it can be implemented by the
% average user (hopefully!)
%
% Additionally, I use loadjson by Qianqian Fang to read the input streams
% from Google for learning meta data about the sheets
% (https://www.mathworks.com/matlabcentral/fileexchange/33381-jsonlab--a-toolbox-to-encode-decode-json-files)
%
% andrew robert bogaard 26 sept 2016
% updated 25 july 2017 (delete rows)
% abogaard@uw.edu

Cite As

Andrew Bogaard (2024). Matlab to Google Sheets (matlab2sheets) (https://github.com/abogaard/matlab2gsheets), GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2014a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Versions that use the GitHub default branch cannot be downloaded

Version Published Release Notes
1.0.0.0

This is a beta version, not extensively tested. Please let me know what needs fixing! :)
added urlreadwrite to mat2sheets
-
removed beta release

trivial metadata updates
--
updated documentation
Added functionality to delete rows

% To delete row(s)
% mat2sheets('1GPd-vBsX5VUejz5hrxE', '552', 2, []) % delete row 2
% mat2sheets('1GPd-vBsX5VUejz5hrxE', '552', [2 10], []) % delete rows 2-10 inclusive

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.