Google Sheets API¶
The task we can perform with the RESTful API will be:
- Create spreadsheets
- Read and write spreadsheets cell values
- update spreadsheets formatting
- Manage connected sheets
Common terms¶
Some terms used in the API
Spreadsheet¶
It is the primary object, and it contains multiple sheets. It has a unique ID spreeadsheetId
made with letters, numbers, hyphens, and underscores. You can find it in the google sheet URL.
REST Resource: spreadsheets | Sheets API | Google Developers
Sheet¶
A page or tab in the spreadsheet, sheets have a unique title and a numeric sheetId
value which can be found in a google sheet URL.
Sheets API | Google Developers
Cell¶
An individual field of text or data within the sheets. Cells can be group vertically by rows and horizontally by columns.
A1 notation¶
The method refers to one or a group of cells. It uses the sheet name, the column letter, and a numeric value that indicates the row. Bellow examples taken from the google sheet documentation:
Sheet1!A1:B2
refers to the first two cells in the top two rows of Sheet1.Sheet1!A:A
refers to all the cells in the first column of Sheet1.Sheet1!1:2
refers to all the cells in the first two rows of Sheet1.Sheet1!A5:A
refers to all the cells of the first column of Sheet 1, from row 5 onward.A1:B2
refers to the first two cells in the top two rows of the first visible sheet.Sheet1
refers to all the cells in Sheet1.
R1C1 notation¶
This method is less common than A1 notation, and it is used to refer to some cells or cells about a specific point. It uses the sheet name and the starting and ending coordinates of the cell using the row and column numbers.
Sheet1!R1C1:R2C2
refers to the first two cells in the top two rows of Sheet1.R1C1:R2C2
refers to the first two cells in the top two rows of the first visible sheet.Sheet1!R[3]C[1]
refers to the cell that is three rows below and one column to the right of the current cell.
Cells | Sheets API | Google Developers
Python Quickstart¶
There will be some prerequisites. They can be fine in the official documentation.
Python Quickstart | Sheets API | Google Developers
1. Install the google client library¶
2. Configure the example¶
- The import of the packages, in this case, I'm importing from the google library. It will make easy the interaction with the google API.
SCOPES
It is the limitation on the actions. For this case, it is limited toreadonly
.SAMPLE_SPREADSHEET_ID
andSAMPLE_RANGE_NAME
these are the ID for the spreadsheet and the range where I'm going to work in.- Inside the main function will be a section where I will consult the file
credentials.json
this is the file I download from the dashboard in the OAuth 2.0 client ID section.