This document describes the basics of using the
spreadsheets.batchUpdate
method on the
spreadsheets resource.
Aside from the value data contained in its cells, a spreadsheet includes many other types of data, such as:
- Dimensions
- Cell formats and borders
- Named ranges
- Protected ranges
- Conditional formatting
These are some of the many kinds of data that control the appearance and
operation of a spreadsheet. The spreadsheets.batchUpdate method lets you
update any of these spreadsheet details. Changes are grouped in a batch so that
if one request is unsuccessful, none of the other (potentially dependent)
changes are written.
If you need to read and write cell value data, you can also use the
spreadsheets.values
resource as described in Read and write cell
values.
Operation types
The particular operations supported by the spreadsheets.batchUpdate method can
be grouped into the following broad operation types:
| Category | Description |
|---|---|
| Add (and Duplicate) | Add new objects (sometimes based on old ones, as in the Duplicate requests). |
| Update (and Set) | Update certain properties of an object, usually leaving the old properties alone (whereas a Set request overwrites the prior data). |
| Delete | Remove objects. |
These categories are used in the next section to describe the behavior of specific operations.
Batch update operations
The spreadsheets.batchUpdate method works by taking one or more
Request
objects, each one specifying a single kind of request to perform. The following
table lists the types of batch update requests, grouped by resource object and
operation type:
Data manipulation requests
There are also some additional requests that mimic user actions for manipulating data:
| Request | Description |
|---|---|
AutoFillRequest |
Automatically fills in more data based on existing data. |
CopyPasteRequest |
Copies data from one area and pastes it to another. |
CutPasteRequest |
Cuts data from one area and pastes it to another. |
DeleteDuplicatesRequest |
Removes rows containing duplicate values in specified columns of a cell range. |
FindReplaceRequest |
Finds and replaces occurrences of some text with other text. |
PasteDataRequest |
Pastes data (HTML or delimited) into a sheet. |
RandomizeRangeRequest |
Randomizes the order of the rows in a range. |
SortRangeRequest |
Sorts data in a range. |
TextToColumnsRequest |
Converts a column of text into many columns of text. |
TrimWhitespaceRequest |
Trims cells of whitespace (such as spaces, tabs, or new lines). |
To learn more about cell and row limits in Google Sheets, see Files you can store in Google Drive.
Use field masks to update specific fields
Many update requests require a FieldMask. A field mask is a comma-delimited list of fields used to indicate which fields in an object to update, while leaving all other fields unchanged. Using a field mask prevents accidental overwrites of fields that aren't specified in the request.
For more information about field masks, see Update with a field mask.
The following code sample shows how to use the
UpdateSpreadsheetPropertiesRequest
to update only the title of a spreadsheet:
Request
POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheetId:batchUpdate
Request body
{
"requests": [{
"updateSpreadsheetProperties": {
"properties": {"title": "TITLE"},
"fields": "title"
}
}]
}Replace TITLE with the new title of the spreadsheet.
Batch update responses
When updating a spreadsheet, some kinds of requests might return responses. These are returned in an array, with each response occupying the same index as the corresponding request. Some requests don't have responses and for those the response is empty.
Typically, "add" requests have responses that return information such as the ID
of the added object. For the list of supported responses, see
Responses.
Code sample: Batch update spreadsheet
The following code sample shows how to perform these actions:
- Update the spreadsheet's title using the
titlevariable. - Find and replace cell values in the spreadsheet using the
findandreplacementvariables.