This document explains how to create and manage spreadsheets in Google Sheets using the Google Sheets API.
Create a spreadsheet
To create a file in Sheets, use the
create method on the
spreadsheets resource
with no parameters.
When you create the file, the method returns a spreadsheets resource. The
returned resource contains a spreadsheetId, properties, a list of sheets,
and a spreadsheetUrl.
The following code sample shows how to create a blank spreadsheet with a specified title.
Apps Script
Java
JavaScript
Node.js
PHP
Python
Ruby
Organize spreadsheets in Google Drive folders
By default, the created spreadsheet is saved to the user's root folder on Google Drive.
If you want to save a spreadsheet to a specified Drive folder, use these methods:
After the spreadsheet file is created, move it to a specific folder using the
updatemethod on thefilesresource of Google Drive API. For more information, see Move files between folders.Add a blank spreadsheet to a folder using the
createmethod on thefilesresource of Drive API, specifyingapplication/vnd.google-apps.spreadsheetas themimeType. For more information on creating files, see Create a file in a specific folder. For more information on MIME types, see Google Workspace and Google Drive supported MIME types.
For either alternative, you'll need to add the appropriate Drive API scopes to authorize the call.
If your app is using a Service Account, the Service Account owns the created spreadsheet. This file then resides in the Service Account's dedicated Drive storage. Files don't appear within other Drive storage accounts unless explicitly shared. For more information, see File ownership.
To move or create a file within a shared drive folder, refer to Implement shared drive support.
To learn more about cell and row limits in Google Sheets, see Files you can store in Google Drive.
Get a spreadsheet
To get a spreadsheet, use the
get method on the
spreadsheets resource with the spreadsheetId path parameter.
The method returns the file as an instance of a spreadsheets resource. By
default, data within the spreadsheet isn't returned. The returned resource
contains the structure and metadata of the spreadsheet, including the
spreadsheet properties (such as title, locale, and time zone) and some detailed
sheet information (such as formatting and protected ranges).
To include data within a spreadsheets resource, use these two methods:
Specify a field mask listing your selected fields by setting the
fieldssystem parameter.Set the boolean
includeGridDataquery parameter totrue. If a field mask is set, theincludeGridDataparameter is ignored.
When you're working with large spreadsheets, we recommend that you query for
only the specific spreadsheet fields that you need. The get method returns all
the data associated with the spreadsheet, so general queries for large
spreadsheets can be slow. For example, to read the number 100 from a cell,
spreadsheets.get returns the cell value plus metadata (such as font name,
size, etc.) resulting in large JSON payloads that are slow to parse. In
comparison, a similar call to values.get returns only the specific cell value
resulting in a much lighter, faster response.
For more information on the spreadsheets.values resource, including
spreadsheets.values.get
and
spreadsheets.values.batchGet,
see the following documents:
List spreadsheets
The Sheets API doesn't offer a method to list spreadsheets for authenticated users.
To retrieve a list of spreadsheets, you can use Drive API's
list method on the
files resource, specifying
application/vnd.google-apps.spreadsheet as the
mimeType:
HTTP
GET https://www.googleapis.com/drive/v3/files?q=mimeType='application/vnd.google-apps.spreadsheet'
cURL
curl -X GET "https://www.googleapis.com/drive/v3/files?q=mimeType='application/vnd.google-apps.spreadsheet'" \
-H "Authorization: Bearer ACCESS_TOKEN" \
-H "Accept: application/json"
Replace ACCESS_TOKEN with the access token that grants access to the API.
Using the files.list method to list a user's spreadsheets requires a
restricted Drive API
scope.
Related topics
Here are a few next steps you might try:
To update a spreadsheet, see Update spreadsheets.
To work with smart chips, see Smart chips.
To work with developer metadata, see Read, write, and search metadata.