My Profile Photo

Ionut Gavrilut


DevOps Enthusiast | Linux System Administrator | Certified Jenkins Engineer


Python gspread to update Google Sheets

I used this Python library (https://github.com/burnash/gspread) to update spreadsheets in Google Drive, from Jenkins. Developers like this kind of “notifications”, more than email notifications, because it cannot spam.

Using OAuth2 for Authorization

I logged in to Google API using OAuth2 credentials. I needed to add:

scope = ['https://spreadsheets.google.com/feeds']

Otherwise there was a Insufficient Permission error.

OAuth Credentials

OAuth credentials can be generated in several different ways using the oauth2client library provided by Google. I used Signed Credentials for this script.

Using Signed Credentials

  • Go to Google Developers Console and create a new project
  • Under “API & auth”, in the API enable “Drive API”.
  • Go to “Credentials” and choose “New Credentials > Service Account Key”. Now it will start a download of a Json file. This is the file you need to run this script. I renamed this Json file to API.json

Now get the script from here

Set your Spreadsheet

sheet = gc.open_by_key("you_can_find_this_key_in_url")
worksheet = sheet.sheet1

Spreadsheet key can be found in its URL. An URL pattern looks like this: https://docs.google.com/spreadsheets/d/{spreadsheet_key}/

Make sure you have Python installed. This script run with 3 arguments: row, column, message.

python updateSheet.py 10 3 "Text Message"

This will show “Text Message” in C:10 (C is 3) cell.