You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* add Docs API Mail Merge Python sample
* pylint & eval fixes
* lint fixes
* final(?) lint fixes
* PR fixes and code cleanup
* more cleanup & fixed brokenness from last commit
* unit test fixes
* README missing one more reference to Sheets API
* oops
* added Docs API video to README
* updated app to spuport >1 row of data; README cleanup
* update unit tests to support new functionality; README cleanup too
* README tweaks
* pylint fixes
Copy file name to clipboardExpand all lines: docs/mail-merge/README.md
+40-23Lines changed: 40 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,19 +3,15 @@
3
3
## Prerequisites
4
4
5
5
- Access to the internet and a web browser
6
-
- A Google account (G Suite accounts may require administrator approval)
6
+
- A Google or Gmail account (G Suite accounts may require administrator approval)
7
7
- Basic Python skills (2.x or 3.x)
8
8
- Google APIs project with the [Drive](https://developers.google.com/drive/), [Sheets](https://developers.google.com/sheets/), and [Docs](https://developers.google.com/docs/) APIs enabled
9
9
10
10
## Description
11
11
12
-
Before getting started, create a new project (or reuse an existing one) at <https://console.developers.google.com> with Google Drive, Sheets, and Docs APIs enabled. (See the videos listed below if you're new to Google APIs.) Then install the Google APIs Client Library for Python if you haven't already: `pip{,3} install -U google-api-python-client`
12
+
Create a new project (or reuse an existing one) from [your developer console](https://console.developers.google.com) with the three Google APIs above enabled. (See the videos listed at the bottom if you're new to Google APIs.) Ensure you have the Google APIs Client Library for Python installed; the fastest way of doing this is to use `pip install -U google-api-python-client` (or with `pip3` if you have both Python 2 and 3 on your computer).
13
13
14
-
This sample app requires you to [create a new Google Docs file](https://docs.google.com). Choose the template you wish to use, but we suggest using Letter/Spearmint to keep things simple. Replace the contact information in the Doc with template variables that we can merge with desired data.
15
-
16
-
Here is a [sample letter template](https://drive.google.com/open?id=1Rr7eKm3tvUgRKRoOYVSMF69IVAHIOPS72-l0CzsPmfM) to get an idea of what we mean: 
17
-
18
-
In the document, the variable names used by the sample app are:
14
+
This sample app requires you to [create a new Google Docs file](https://docs.google.com) to serve as the letter template with variable placeholders. Choose the template you wish to use, but we recommend *Letter/Spearmint* to keep things simple. Replace the contact information in the Doc with template variables that we can merge with desired data. Here are the variables we're using:
19
15
20
16
### General
21
17
@@ -36,15 +32,27 @@ In the document, the variable names used by the sample app are:
36
32
*`{{TO_COMPANY}}` — recipient's organization
37
33
*`{{TO_ADDRESS}}` — recipient's address
38
34
39
-
After you've templatized the Google Doc, be sure to grab its file ID — in your browser, look at the address bar, and extract the long alphanumeric string that makes up the Drive file ID from the URL: `https://docs.google.com/document/d/<DRIVE_FILE_ID>/edit`.
35
+
Here's one [sample letter template](https://drive.google.com/open?id=1Xycxuuv7OhEQUuzbt_Mw0TPMq02MseSD1vZdBJ3nLjk) to get an idea of what we mean:
36
+
37
+

40
38
41
-
Replace `YOUR_TMPL_DOC_FILE_ID` in the `docs_mail_merge.py` source file with this file ID as the string value (in quotes) for the `DOCS_FILE_ID` variable. Run the sample app, accept the OAuth2 permissions requested, and when the script has completed, you should have a new mail-merged Google Doc named `Merged form letter` in your Google Drive folder!
39
+
Once you're done, grab your template's file ID — go to the URL in the address bar of your browser and extract the long alphanumeric string representing the Drive file ID: `https://docs.google.com/document/d/DRIVE_FILE_ID/edit`. Replace `YOUR_TMPL_DOC_FILE_ID` with this ID as the `DOCS_FILE_ID` string variable (keep in quotes).
42
40
43
41
## Data source
44
42
45
-
The application currently supports two different sources of data, plain text and Google Sheets. By default, the sample uses plain text via the `TARGET_TEXT` variable. A better option is to use a Google Sheet. Enable the API for your project in the developers console, and change the `source` variable at the bottom to `'sheets'`. Be sure you create a Sheet structured like the one below. Here is one [example Sheet](https://docs.google.com/spreadsheets/d/1vaoqPYGL1cJvkogV36nu3AKQ5rUacXj9TV-zqTvXuMU/edit) you can model yours with. Ensure you then set the `SHEETS_FILE_ID` variable to the file ID of your Google Sheet.
43
+
Next, decide on the data source. This sample app currently supports plain text and Google Sheets. By default, the `TARGET_TEXT` variable is set to `'text'` but change to `'sheets'` once you have a Google Sheet with the data. The code supports a Sheet structured like this:
44
+
45
+

46
+
47
+
Here is [one example Sheet](https://drive.google.com/open?id=18yqXLEMx6l__VAIN-Zo52pL18F3rXn0_-K6gZ-vwPcc) you can model yours with. Ensure you then set the `SHEETS_FILE_ID` variable to its file ID (get it the same way as your Google Doc).
48
+
49
+
## Application code
46
50
47
-

51
+
The application script (`docs_mail_merge.py`) supplies a data structure (`merge`) with the sender info, date, body of the letter, and placeholders for values that will be replaced by data from the selected data source. The data is fetched and merged into form letters in a loop, displaying links to each of the merged letters. One function is used to fetch the data, supported by private function "shims" for each data source. The other pair of functions: a private function to copy the template, and one more for merging the form data into a copy of the template.
52
+
53
+
If you run the sample app as written (with all real variables and data documents) and accept the OAuth2 permissions. You'll see one line of output per letter merged. Those letters, named `Merged form letter`, will also be found in your Google Drive. If you run the app with our data featured here, your merged letter should look like this:
0 commit comments