Reading and Writing spreadsheet¶
A cell is located at interception between a row and a column and may contain a value. To interact with those cells, google sheets API provides the spreadsheet.values collection with it, those with the credentials can reading and writing.
I will focus here on the "basic" usage of the spreadsheet.values collection and ignoring any formatting properties for the sheets.
Methods¶
The following methods are from the spreadsheet.values collection
Range Access | Reading | Writing |
---|---|---|
Single Range | spreadsheets.values.get | spreadsheets.values.update |
Multiple Range | spreadsheets.values.batchGet | spreadsheet.values.batchUpdate |
Appending | spreadsheet.values.append |
Google Provide additional examples for Basic reading and Basic Writing in the sample pages
Basic reading samples
Basic Reading | Sheets API | Google Developers
Basic Writing samples
Basic Writing | Sheets API | Google Developers
Reading¶
What do I need to read the spreadsheet?
- The spreadsheet ID
- The range in A1 notation
The output of this action will be control by 3 parameters:
majorDimension
( default value:ROW
)valueRenderOption
(Default value:FORMATTED_VALUE
)dateTimeRenderOption
(Default value:SERIAL_NUMBER
)
ValueRange
¶
Represent the data within the range of the spreadsheet, the data I'm reading.
Its JSON representation is:
Where:
range
typestring
: the range of values in A1 notation. This range will be the one I will be reading.majorDimension
typeenum
: This is more difficult to understand, but in few words, depending on the value the output will follow a format, let say the value ismajorDimension=ROWS
the data is A1=1,B1=2,A2=3,B2=4, therange=A1:B2
the return will be[ [1,2] [3,4] ]
but if I modifymajorDimension=COLUMNS
the output will be[ [1,3] [2,4] ]
.values
typearray
: the data to be read, this fallow a format of an array of arrays, the outer array represent the data, each inner array amajordimension
and each item inside a cell value.
more info:
REST Resource: spreadsheets.values | Sheets API | Google Developers
valueRenderOption
¶
It will affect how the values are rendered in the output. The possible values of this parameter are:
FORMATTED_VALUE
UNFORMATTED_VALUE
FORMULA
More info:
ValueRenderOption | Sheets API | Google Developers
dateTimeRenderOption
¶
It is used only if valueRenderOption=FORMATTED_VALUE
. It affects how dates are rendered in the output. the options are:
SERIAL_NUMBER
FORMATTED_STRING
more info:
DateTimeRenderOption | Sheets API | Google Developers
BatchGetValueResponse
¶
It is the response when the method batchGet()
is used. It is the method to read non-consecutive ranges or several ranges from a spreadsheet. As well as valueRange
is has a JSON representation
Where:
spreadsheetId
: It is the ID of the spreadsheet I am readingvalueRanges
: is an object that containsvalueRange
in the same order as the request values.
more info:
Method: spreadsheets.values.batchGet | Sheets API | Google Developers
Examples¶
I have the spreadsheet
Where:
spreadsheetId
is known and will look similar to10q7CW3zqzb2B3hHluXPC4Q8J
- The sheet that contains the data is
first_sheet
- And the range I'm going to read in A1 notation will be
first_sheet!A1:C4
Reading single range¶
Assuming that I already have the resources and tokens (full implementation below), the reading function will look like this:
Be aware that values
in the second statement are the item in the ValueRange
JSON representation I mentioned earlier.
I didn't provide any information about the majorDimension
so the default value ROW
is applied. the result is
Reading multiple ranges¶
Using the same spreadsheet, for this example, I will change the range to two non-consecutive ranges. I will need to make some changes to the function. I replace the get()
method for batchGet()
.
The response will be a BatchGetValueResponse
that is an object that contains a list of ValueRange
and the spreadsheet_ID
and the response will be:
Writing¶
Similar to "reading". I will need few elements to be able to read on the spreadsheet, these elements are:
spreadsheetID
.- The range in A1 notation.
- The data is arranged in the appropriate format in the body of the request.
for the update, I will need to use the parameter ValueInputOption
( for a batch update this value is in the body of the request, for a single update, it will be part of the query parameters)
valueInputOption | Description |
---|---|
RAW | It will place a string without any parse, if the input is =1+2 the value place in the cell will be that =1+2 |
USER_ENTERED | The input will be parse following a similar pattern that googles sheet UI, example, =1+2 will be rendered as a result of the operation 3 |
Examples¶
To write data as it was to read, I have two types, the single range writing and the writing for multiple ranges.
Writing a single range¶
For this, I will follow the requested [spreadsheet.values.update](https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets.values/update)
I started with a simple empty sheet
I'm going to write in the first and second row, and the code looks like
from the code above:
- I create a list of values the outer array represents all ranges of the spreadsheet, the inner arrays are the rows since I didn't specify the
majorDimention
, and each item in the array is a value of the cell. - I define the body of the request, in this case, it is a
valueRange
just the value item is mandatory if I specify therange
it must match the one provided in theupdate
function below. Here I can change themajorDimension
- I have the
update
function, here I will pass thespreadsheeId
, therange
, thevalueInputOption
because it is a single range update, otherwise, it will be in the body. Finally, I will pass thebody
containing the values.
Writing a multiple ranges¶
In this case, the request use is spreadsheets.values.batchUpdate
The request must be BatchUpdateValuesRequest
object, this object will contain the ValueInputOption
and a list of ValueRange
, an example:
From the code above:
- I define two ranges, ode that will be the first row
first_sheet!A1:C1
and the second will be the third rowfirst_sheet!A3:C3
. - I define two variables that will handle the values,
values1
, andvalues2
- I create the
data
, Be aware here I have twovalueRange
. - I create the
body, the
valueInputOption` will be in the body in the request instead of the query parameter, like in the example with a single range. - I use the function
batchUpdate
to execute the writing.
Appending¶
Like writing append will use a specific type of request in this case spreadsheets.values.append
.
For the most part, the append is similar to writing a single range with the difference in the function use, rather than use update()
I use append()
.
from the code above:
- Define the range where I will start to append ( be aware that if that is used already the new information will be written in the next row)
- Define the values, the outer array defines the data, inner array represents the rows (I haven't defined a
majorDimension
so the default isROW
). - I create the
body
. - I use the function
append()
that will include the query parametervalueInputOption
Full scripts¶
Reading¶
Writing¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
|