16 Years of Sheets!

Google Sheets was released over 16 years ago in March 2006 and is one of the core applications comprising Google Workspace today. While Google Sheets does not offer all the bells-and-whistles available with Microsoft Excel, it is still a very powerful spreadsheet application. With Google Workspace enjoying greater adoption as a result of the pandemic-fueled work-at-home trend, it is likely that more and more people are using or will be using Google Sheets.

Like any spreadsheet application, Google Sheets allows users to organize, edit, analyze, and visualize data. Given that Google Sheets has been a web-based application from its inception, its collaborative features are arguably its greatest strength where multiple users can view and edit a given spreadsheet simultaneously. Additional strengths and weaknesses (as compared to Microsoft Excel) include:

Strengths

Weaknesses

The Google Sheets API provides a programmatic way to interact with a Google Sheets spreadsheet and supports a wide variety of languages including Python, Java, and Node.js. An application developer can read data from and write data to a Google Sheets spreadsheet via the API, as well as complete more complex interactions such as creating new worksheets, formatting data, and creating charts. To a certain extent, the Google Sheets API provides developers with a set of methods that can be used to manipulate a given Google Sheets spreadsheet in a nearly unlimited way.

In this tutorial you will learn how to set up the underlying dependencies needed to use the Google Sheets API with a Python application. In particular, you will create a new Google Cloud Platform project, enable the Google Sheets API, create credentials to securely access the API, install the required Python libraries to interact with the API from a Python application, and build a simple test application.

Prerequisites

To complete this tutorial, you will need:

Step 1 - Create a New Google Cloud Platform Project

Google Cloud Platform projects allow developers to work with Google Workspace APIs, including the Google Sheets API. Keep in mind that Google sets a quota on the number of projects a given user can create. If you are using the Google Cloud Platform for the first time, then this obviously shouldn't be a problem. However, if you need a higher project quota in the future, you can always request one from Google.

Step 2 - Enable the Google Sheets API

Now that you have created a new Google Cloud Platform project, you need to enable the Google Sheets API for the project.

Step 3 - Create a Service Account

A Python application that wants to interact with the Google Sheets API must be authenticated and authorized to access the resources that the API supports. Google provides different options for the authentication and authorization mechanisms depending on the needs of the developer. In this step, we will choose to configure a new Service Account for authentication and authorization purposes.

Step 3a - Create a New Service Account

Step 3b - Create a New Key for the Service Account

Step 4 - Install Required Google Libraries for Python

A Python application needs to import certain Google libraries for Python to successfully authenticate with and interact with the Google Sheets API.

Step 4a - Install the Google API Python Client Library

The Google Client Libraries provide access to Google Cloud APIs for a variety of languages. In this tutorial, we will install the Google API Python Client Library.

pip install google-cloud

Step 4b - Install the Google Authentication Libraries

The Google Authentication libraries are used for authentication with Google Workspace APIs, and in this particular case, the Google Sheets API.

pip install google-auth

pip install google-auth-oauthlib

The required Python libraries have now been installed.

Step 5 - Configuring a Test Application in Python

With everything setup in Steps 1 - 4, you can now start programmatically interacting with Google Sheets spreadsheets via the Google Sheets API. This steps builds a simple test application to retrieve the title of a Google Sheets spreadsheet.

Step 5a - Create a New Google Sheets Spreadsheet

Step 5b - Share the New Google Sheets Spreadsheet With Your Service Account

Step 5c - Record the URL Identifier for the New Google Sheets Spreadsheet

https://docs.google.com/spreadsheets/d/8VaaiCuZ2q09IVndzU54s1RtxQreAxgFNaUPf9su5hK0/edit#gid=0

Step 5d - Configure the Test Application in Python

from google.oauth2 import service_account
from googleapiclient.discovery import build

spreadsheet_id = ENTER_YOUR_SPREADSHEET_ID FROM_STEP_5c_HERE_WITH_QUOTES
# For example:
# spreadsheet_id = "8VaaiCuZ2q09IVndzU54s1RtxQreAxgFNaUPf9su5hK0"

credentials = service_account.Credentials.from_service_account_file("key.json", scopes=["https://www.googleapis.com/auth/spreadsheets"])
service = build("sheets", "v4", credentials=credentials)

request = service.spreadsheets().get(spreadsheetId=spreadsheet_id, ranges=[], includeGridData=False)
sheet_props = request.execute()

print(sheet_props["properties"]["title"])

# Output:
# My New Google Sheets Spreadsheet

Conclusion

In this tutorial, you created a new Google Cloud Platform project, enabled the Google Sheets API for that project, created a new service account to authenticate with the Google Sheets API, installed all required Google libraries for Python, and built a simple test application. As mentioned in the introduction, the possibilities are almost endless when programmatically interfacing with Google Sheets. See Google Sheets for Developers documentation for more information.