This is a 15-20 minute talk introducing developers to the (new as of Feb 2019) Google Docs API. The example demonstrated is a "mail merge" app, and whose data comes from a Google Sheet. Bonus: attendees get an intro to using the Sheets API too.
1. 2018 | Confidential and Proprietary
Introducing the (new)
Google Docs API
Wesley Chun (@wescpy)
Developer Advocate, Google Cloud
G Suite Dev Show
goo.gl/JpBQ40
About the speaker
● Developer Advocate, Google Cloud
● Mission: enable current and future developers to be successful using
Google Cloud and other Google developer tools, APIs, and platforms
● Videos: host of the G Suite Dev Show on YouTube
● Blogs: developers.googleblog.com & gsuite-developers.googleblog.com
● Twitters: @wescpy, @GoogleDevs, @GSuiteDevs
● Background
● Software engineer & architect for 20+ years
● One of the original Yahoo!Mail engineers
● Author of bestselling "Core Python" books (corepython.com)
● Teacher and technical instructor since 1983
● Fellow of the Python Software Foundation
3. Google APIs client
libraries; demos in
Others:
developers.google.com/
api-client-library
Python 2 or 3:
pip{3,} install -U
google-api-python-client
Authorized API access
OAuth boilerplate
goo.gl/KMfbeK
4. OAuth2 or
API key
HTTP-based REST APIs 1
HTTP
2
Google APIs request-response workflow
● Application makes request
● Request received by service
● Process data, return response
● Results sent to application
(typical client-server model)
G Suite: Google Docs
Docs API gives you access to read &
write documents programmatically so
you can autogenerate them with data
integrated from various sources
developers.google.com/docs
6. Mail merge
Mail merge (template global search & replace)
requests = [
# (global) search-and-replace text
{'replaceAllText': {
'containsText': {'text': '{{TITLE}}'},
'replaceText': 'Hello World!',
}},
]
DOCS.documents().batchUpdate(body={'requests': requests},
documentId=DOC_ID, fields='').execute()
Mail merge
bit.ly/2W1xhNa
(video TBD)
7. G Suite: Google Sheets
Sheets API gives you programmatic
access to spreadsheets; has ability
to perform almost any action you
can do from the web interface
developers.google.com/sheets
Try our Node.js customized reporting tool codelab:
g.co/codelabs/sheets
Why use the Sheets API?
data visualization
customized reports
Sheets as a data source
8. Migrate SQL data to a Sheet
# read SQL data then create new spreadsheet & add rows into it
FIELDS = ('ID', 'Customer Name', 'Product Code',
'Units Ordered', 'Unit Price', 'Status')
cxn = sqlite3.connect('db.sqlite')
cur = cxn.cursor()
rows = cur.execute('SELECT * FROM orders').fetchall()
cxn.close()
rows.insert(0, FIELDS)
DATA = {'properties': {'title': 'Customer orders'}}
SHEET_ID = SHEETS.spreadsheets().create(body=DATA,
fields='spreadsheetId').execute().get('spreadsheetId')
SHEETS.spreadsheets().values().update(spreadsheetId=SHEET_ID, range='A1',
body={'values': rows}, valueInputOption='RAW').execute()
Migrate SQL data
to Sheets
goo.gl/N1RPwC
Thank you!
Wesley Chun
@wescpy