Is there a way to connect directly form QGIS to a Google spreadsheet?

The purpose for pulling data from a Google spreadsheet is diverse. This could be to pull coordinate values from a questionnaire or content for atlas making.


There is a way to connect the sheet via the Google sheets API as Cezar mentioned. Instructions can be found here. After connecting to the API, you have to create a python (pyQGIS) script that pulls from the google sheet and creates points for the layer, a tutorial can be found here. I created an example (with sensitive info removed) which I have attached below. This code is not yet complete. Once I clean it up I'll try to publish it as a toolbar. In the meantime...just add your sheet id and sheetname/range.

#load the required packages/libraries/etc
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from qgis.PyQt.QtCore import QVariant

# If modifying these scopes, delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/spreadsheets.readonly']

# The ID and range of a sample spreadsheet.

#adapted from quickstart
def getDataFromTable():
    """Shows basic usage of the Sheets API.
    Prints values from a sample spreadsheet.
    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server()
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('sheets', 'v4', credentials=creds)

    # Call the Sheets API
    sheet = service.spreadsheets()
    result = sheet.values().get(spreadsheetId=SAMPLE_SPREADSHEET_ID,
    values = result.get('values', [])
    return values

data = getDataFromTable()
headers = data.pop(0)

#now we can start the qgis-y stuff
vl = QgsVectorLayer("Point", "temp", "memory")
pr = vl.dataProvider()
for attrb in headers: 
  pr.addAttributes([QgsField(attrb, QVariant.String)])


for datum in data:
    f = QgsFeature()

The most basic option is to export the table as a CSV file and import it in QGIS. You there have the option to watch the file for updates.

If exporting CSVs isn't an option, you can use a desktop spreadsheet and connect it to a database, or use a database in the first place. A good example from actual practice can be found in an older answer.

You should also look at the Google Sheets API to see if you can use it in some way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.