Reading & Writing Cell Values

Spreadsheets can have multiple sheets, with each sheet having any number of rows or columns. A cell is a location at the intersection of a particular row and column, and may contain a data value. The Google Sheets API provides the spreadsheets.values collection to enable the simple reading and writing of values.

This page describes the basics of using the spreadsheets.values collection. If you need to update formatting or other properties in a sheet, you will need to use the spreadsheets collection, which is described in Updating Spreadsheets.

Methods

The spreadsheets.values collection provides the following methods for reading and writing values, each with a specific task in mind:

Range Access Reading Writing
Single range spreadsheets.values.get spreadsheets.values.update
Multiple ranges spreadsheets.values.batchGet spreadsheets.values.batchUpdate
Appending spreadsheets.values.append

In general, it is a good idea to combine multiple reads or updates with the batchGet and batchUpdate methods (respectively), as this will improve efficiency.

You can find examples of each of these methods in the Basic Reading and Basic Writing samples pages.

Reading

To read data from a sheet, you will need the spreadsheet ID and the range(s) A1 notation. For more information about spreadsheet IDs and A1 notation, see Key Concepts in the Google Sheets API. The format of the output is controlled by three optional parameters:

Format Parameter Default Value
majorDimension ROWS
valueRenderOption FORMATTED_VALUE
dateTimeRenderOption SERIAL_NUMBER

Note that dateTimeRenderOption is only used if the valueRenderOption is not FORMATTED_VALUE.

The singular and batch get methods are described below. For examples of basic read operations, see the Basic Reading recipes page.

Reading a single range

To read a single range of data out of a spreadsheet, use a spreadsheets.values.get request:

Protocol

GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheet_id/values/range

Substitute spreadsheet_id with your spreadsheet's ID, and range with the range to read, in A1 notation.

Java

ValueRange result = service.spreadsheets().values().get(spreadsheetId, range).execute();
int numRows = result.getValues() != null ? result.getValues().size() : 0;
System.out.printf("%d rows retrieved.", numRows);

PHP

$result = $service->spreadsheets_values->get($spreadsheetId, $range);

Python

result = service.spreadsheets().values().get(
    spreadsheetId=spreadsheet_id, range=range_name).execute()

The response to this request is returned as a ValueRange object.

Reading multiples ranges

To read multiple discontinuous ranges, use a spreadsheets.values.batchGet, which lets you specify any number of ranges to retrieve:

Protocol

GET https://sheets.googleapis.com/v4/spreadsheets/spreadsheet_id/values:batchGet?
            ranges=range1&ranges=range2

Java

List<String> ranges = Arrays.asList(
        //Range names ...
        );
BatchGetValuesResponse result = service.spreadsheets().values().batchGet(spreadsheetId)
        .setRanges(ranges).execute();
System.out.printf("%d ranges retrieved.", result.getValueRanges().size());

PHP

$ranges = array(
  // Range names ...
);
$params = array(
  'ranges' => $ranges
);
$result = $service->spreadsheets_values->batchGet($spreadsheetId, $params);

Python

range_names = [
  # Range names ...
]
result = service.spreadsheets().values().batchGet(
    spreadsheetId=spreadsheet_id, ranges=range_names).execute()

The response to this request is a BatchGetValueResponse, object which contains the spreadsheet ID and a list of ValueRange objects.

Writing

To write to a sheet, you will need the spreadsheet ID, the range(s) in A1 notation, and the data you wish to write arranged in an appropriate request body object. For more information about spreadsheet IDs and A1 notation, see Key Concepts in the Google Sheets API.

Updates require a valid ValueInputOption parameter (for singular updates, this is a required query parameter; for batch updates, this parameter is required in the request body). The ValueInputOption controls whether input strings are parsed or not, as described in the following table:

ValueInputOption Description
RAW The input is not parsed and is simply inserted as a string, so the input "=1+2" places the string "=1+2" in the cell, not a formula. (Non-string values like booleans or numbers are always handled as RAW.)
USER_ENTERED The input is parsed exactly as if it were entered into the Google Sheets UI, so "Mar 1 2016" becomes a date, and "=1+2" becomes a formula. Formats may also be inferred, so "$100.15" becomes a number with currency formatting.

The singular and batch update methods are described below. For examples of basic write operations, see the Basic Writing recipes page.

Writing to a single range

To write data to a single range, use a spreadsheets.values.update request:

Protocol

PUT https://sheets.googleapis.com/v4/spreadsheets/spreadsheet_id/values/range?
            valueInputOption=value_input_option
{
    "values": [
        [
            // Cell values ...
        ],
        // Additional rows ...
    ]
}

Substitute spreadsheet_id with your spreadsheet's ID, and range with the range to write, in A1 notation.

Java

List<List<Object>> values = Arrays.asList(
        Arrays.asList(
                // Cell values ...
                )
        // Additional rows ...
        );
ValueRange body = new ValueRange()
        .setValues(values);
UpdateValuesResponse result =
        service.spreadsheets().values().update(spreadsheetId, range, body)
        .setValueInputOption(valueInputOption)
        .execute();
System.out.printf("%d cells updated.", result.getUpdatedCells());

PHP

$values = array(
    array(
        // Cell values ...
    ),
    // Additional rows ...
);
$body = new Google_Service_Sheets_ValueRange(array(
  'values' => $values
));
$params = array(
  'valueInputOption' => $valueInputOption
);
$result = $service->spreadsheets_values->update($spreadsheetId, $range,
    $body, $params);

Python

values = [
    [
        # Cell values ...
    ],
    # Additional rows ...
]
body = {
  'values': values
}
result = service.spreadsheets().values().update(
    spreadsheetId=spreadsheet_id, range=range_name,
    valueInputOption=value_input_option, body=body).execute()

The body of the update request must be a ValueRange object, though the only required field is values. If range is specified, it must match the range in the URL. In the ValueRange, you can optionally specify its majorDimension. By default, ROWS is used. If COLUMNS is specified, each inner array is written to a column instead of a row.

When updating, values with no data are skipped. To clear data, use an empty string ("").

Writing multiple ranges

If you want to write multiple discontinuous ranges, you can use a spreadsheets.values.batchUpdate request:

Protocol

POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheet_id/values:batchUpdate
{
    "valueInputOption": "value_input_option",
    "data": [
        {
            "range": "range",
            "values" : [
                [
                    // Cell values ...
                ],
                // Additional rows ...
            ]
        },
        // Additional ranges to update ...
    ]
}

Java

List<List<Object>> values = Arrays.asList(
        Arrays.asList(
                // Cell values ...
                )
        // Additional rows ...
        );
List<ValueRange> data = new ArrayList<ValueRange>();
data.add(new ValueRange()
        .setRange(range)
        .setValues(values));
// Additional ranges to update ...

BatchUpdateValuesRequest body = new BatchUpdateValuesRequest()
        .setValueInputOption(valueInputOption)
        .setData(data);
BatchUpdateValuesResponse result =
        service.spreadsheets().values().batchUpdate(spreadsheetId, body).execute();
System.out.printf("%d cells updated.", result.getTotalUpdatedCells());

PHP

$values = array(
    array(
        // Cell values ...
    ),
    // Additional rows ...
);
$data = array();
$data[] = new Google_Service_Sheets_ValueRange(array(
  'range' => $range,
  'values' => $values
));
// Additional ranges to update ...

$body = new Google_Service_Sheets_BatchUpdateValuesRequest(array(
  'valueInputOption' => $valueInputOption,
  'data' => $data
));
$result = $service->spreadsheets_values->batchUpdate($spreadsheetId, $body);

Python

values = [
    [
        # Cell values
    ],
    # Additional rows
]
data = [
    {
        'range': range_name,
        'values': values
    },
    # Additional ranges to update ...
]
body = {
  'valueInputOption': value_input_option,
  'data': data
}
result = service.spreadsheets().values().batchUpdate(
    spreadsheetId=spreadsheet_id, body=body).execute()

The body of the batchUpdate request must be a BatchUpdateValuesRequest object, which contains a ValueInputOption and a list of ValueRange objects (one for each written range). Each ValueRange object specifies its own range, majorDimension, and the data to input.

Appending values

To append data after a table of data in a sheet, use a spreadsheets.values.append request:

Protocol

POST https://sheets.googleapis.com/v4/spreadsheets/spreadsheet_id/values/range:append?
            valueInputOption=valueInputOption
{
    "values": [
        [
            // Cell values ...
        ],
        // Additional rows ...
    ]
}

Substitute spreadsheet_id with your spreadsheet's ID, and range with the range to detect an existing table, in A1 notation.

Java

List<List<Object>> values = Arrays.asList(
        Arrays.asList(
                // Cell values ...
                )
        // Additional rows ...
        );
ValueRange body = new ValueRange()
        .setValues(values);
AppendValuesResponse result =
        service.spreadsheets().values().append(spreadsheetId, range, body)
        .setValueInputOption(valueInputOption)
        .execute();
System.out.printf("%d cells appended.", result.getUpdates().getUpdatedCells());

PHP

$values = array(
    array(
        // Cell values ...
    ),
    // Additional rows ...
);
$body = new Google_Service_Sheets_ValueRange(array(
  'values' => $values
));
$params = array(
  'valueInputOption' => $valueInputOption
);
$result = $service->spreadsheets_values->append($spreadsheetId, $range,
    $body, $params);

Python

values = [
    [
        # Cell values ...
    ],
    # Additional rows ...
]
body = {
  'values': values
}
result = service.spreadsheets().values().append(
    spreadsheetId=spreadsheet_id, range=range_name,
    valueInputOption=value_input_option, body=body).execute()

The body of the update request must be a ValueRange object, though the only required field is values. If range is specified, it must match the range in the URL. In the ValueRange, you can optionally specify its majorDimension. By default, ROWS is used. If COLUMNS is specified, each inner array is written to a column instead of a row.

The input range is used to search for existing data and find a "table" within that range. Values are appended to the next row of the table, starting with the first column of the table. For example, consider a sheet Sheet1 that looks like:

A B C D E
1 x y z
2 x y z
3
4 x y
5 y z
6 x y z
7

There are two tables in the sheet: A1:C2, and B4:D6. Appended values would begin at B7 for all the following range inputs:

  • Sheet1, because it will examine all the data in the sheet, determine that the table at B4:D6 is the last table.
  • B4 or C5:D5, because they're both in the B4:D6 table.
  • B2:D4, because the last table in the range is the B4:D6 table (despite it also containing the A1:C2 table).
  • A3:G10, because the last table in the range is the B4:D6 table (despite starting before and ending after it).

The following range inputs would not start writing at B7:

  • A1 would start writing at A3, because that's in the A1:C2 table.
  • E4 would start writing at E4, because it's not in any table. (A4 would also start writing at A4 for the same reasons.)

Additionally, you can choose if you want to overwrite existing data after a table or insert new rows for the new data. By default, the input overwrites data after the table. To write the new data into new rows, specify insertDataOption=INSERT_ROWS.

Send feedback about...