Export to Google Docs from AppSheet

AppSheet already provides the ability to automatically generate a PDF document based on a template, but sometimes you many need to generate a Google Doc with a link that can be accessed later. This is an example of how to generate a Google Doc based on a template and return a Google Drive link to the app.

Here we have a sales automation app for tracking customer leads assigned to sales reps. Let's say the sales rep needs a one-pager that gives a quick overview of one of their leads. We can do that from an AppSheet app.

Export Example #1: "Export Sales Lead" action

export_lead.gif

Export Example #2: "Export Table" action

export_table.gif

Instructions to Replicate:

Step 1: Copy the Apps Script file located here

alafontant_0-1695723451075.png

Step 2: Add the Apps Script automation to your app. You will need to authorize the project after you select the file.

alafontant_1-1695723451835.png

Step 3: Select the right function from dropdown list. Select the exportDocs function to paste specific values of text or images into a document. Select the exportDocsWithTable if you need to iterate through a list of records and paste the data into a table in a document.

alafontant_0-1702938877601.png

 

exportDocs
templateUrl The link to your template sheet. For example:
"https://docs.google.com/document/d/1PgIRV5hEi-PU2zHceaCXcKOub1cM9mChWu5WeFKlQag/edit"
docTitle The new name for the spreadsheet after the export. For example:
"Customer Report : " & [company_name]
userEmails The recipients email addresses. You can have multiple as long as they are comma-separated.
"user1@test.com", "user2@test.com"
content The data that should be included in the export. The list is structured in pairs. The first item is the key label that is included in your document template. The second item is the data that you want to paste into the document. The script functions by finding the key and replacing it with the item directly following it in the list.

Note that image urls are supported, but not file paths to Google Drive. The key name for an image must have "image" somewhere in the key name to be recognized by the script (e.g., "Company Image" or "Profile-image"). The image must also be contained within a table in the document template to ensure appropriate sizing.

For example:

LIST(

TEXT("Company Name"),
     TEXT(LOOKUP([reference_id], "customers", "customer_id", "company_name")),

TEXT("Full Name"),
      TEXT(LOOKUP([reference_id], "customers", "customer_id", "full_name")),

TEXT("Company Image"),
       TEXT(LOOKUP([reference_id], "customers", "customer_id", "image"))

)

 

exportDocsWithTable
templateUrl

The link to your template sheet. For example:
"https://docs.google.com/document/d/1E2vI-24w0N2B83dR6Oa1H-QjXhqaiaBG7XXkDM3_wss/edit"

docTitle The new name for the spreadsheet after the export. For example:
"Detailed Report for " & [region] & " Region"
userEmails The recipients email addresses. You can have multiple as long as they are comma-separated.
"user1@test.com", "user2@test.com"
tableKey The key used to identify the table where data should be appended. This key must be in the first row and first column of your table so that the script can identify the correct table. It should not contain any brackets. For example:
"Leads Table"
columns

This contains the list of columns that you want to paste into your table. The columns must be listed in the same order that you want them displayed in the table.

For example:

LIST(

SELECT(customers[full_name], [reference_id] = [region].[rep_id]),

SELECT(customers[company_name], [reference_id] = [region].[rep_id]),

SELECT(customers[email], [reference_id] = [region].[rep_id]),

SELECT(customers[num_employees], [reference_id] = [region].[rep_id]),

SELECT(customers[Formatted Revenue], [reference_id] = [region].[rep_id])

)

content The data that should be included in the export. The list is structured in pairs. The first item is the key label that is included in your document template. The second item is the data that you want to paste into the document. The script functions by finding the key and replacing it with the item directly following it in the list.

Note that image urls are supported, but not file paths to Google Drive. The key name for an image must have "image" somewhere in the key name to be recognized by the script (e.g., "Company Image" or "Profile-image"). The image must also be contained within a table in the document template to ensure appropriate sizing.

For example:

LIST(

TEXT("sales_rep"),
   TEXT(LOOKUP([reference_id], "reps", "rep_id", "sales_rep")),

TEXT("email"),
   TEXT(LOOKUP([reference_id], "reps", "rep_id", "email")),

TEXT("region"),
   TEXT(LOOKUP([reference_id], "reps", "rep_id", "region"))

)


Step 4
: Create your Docs template. You template will vary depending on the functions you need.

exportDocs: The template must include the key between brackets. For example, if you wanted to include the number of employees in the document template, you could name a key {Employees} as displayed below. There is no restriction on what you can name your key or the number of key-value pairs that can be included in the document.

Reminder that images must be contained within a table to ensure that it is sized appropriately and must contain "image" somewhere in the key name to function correctly.

exportDocsWithTable: Use this function if you need to iterate through a list of records that need to be pasted into a table. Start by naming the table in your template with a table key - this allows the script to identify the correct table. This key can be named anything as long as it resides in the first column and first row of the table. You will need to provide this "tableKey" as one of the parameters in the automation. Then add the column headers for the data that you plan to paste to your document template. These columns headers should be in the same order as the list you add to the "columns" parameters in the automation. Make sure to add one blank row after your column headers! This will ensure that the table is formatted the way that you prefer. 

โ€ƒ

Template for "Export Sales Lead" Template for "Export Table"
Template for Export DocsTemplate for Export Docs Screenshot 2023-12-18 at 10.52.07โ€ฏAM.png

 


Please note, this is merely one example of how to use Apps Script to export to Google Docs. Alternative implementations are certainly possible.

Quick Links
Example App (remember to authorize the apps script project in the automation tab if you copy this template)
Apps Script File
Docs Template

12 13 3,083
13 REPLIES 13

Could you show an example of a list of values? Similar to how Start: templates work.

Thanks

Do you mean an example of a list of values of variable size in a table, like below? Or something else?

Customer Name Revenue Employees
Cymbal Inc. $1,200,000 115
ABC Corp. $891,875 74
Acme Org. $2,550,000 89
... ... ...

Yes, a way to iterate through a list of records

Haha, I knew someone was going to ask for this, but I got busy! Let me post the Google Slides export first and then I'll come back and add support for that use case.

Hello @alafontant, any update on iterating on list of records?

Would be great to hear your ideas, and happy to help.

Apologies it took me so long to get back to this! I have updated the post to include support for tables within a document. Let me know if you have any questions.

Thank you very much @alafontant 

I believe with your this sharing of GAS for populating multiple records in the Google Docs template based on a table key, almost all reports having parent record and associated child records can be created outside the traditional approach of using AppSheet templates.

If required an addition of some GAS for converting the Google Docs filled with data can be converted to a PDF.

Added bonus, I believe is with this GAS approach you shared, the much required  and asked for option of having headers and footers and page numbers for the report pages will also be more conveniently possible. This is so because , I believe we can use the default page number and header footer settings of the Google docs itself.

Will test your shared app and GAS more and revert. Just as a quick question if I may ask, in case the data populated table in the Google doc extends one page because of many records to populate, will the table headers repeat in the next G Doc page?

Thanks for the feedback! I'm glad this series of tips has been helpful.

Repeating table headers on each page can be accomplished by selecting "Pin header up to this row" next to the table in the Google Docs template. I have not turned it on in this Docs template, but you can in your version.

alafontant_2-1703092133042.png

 

Thank you @alafontant. Yes, now got it. Sinnce none of the Google Docs native  settings gets disturbed when we use it as a template with GAS approach, the "pin header..." setting will work to repeat the headers.

 Is it possible to display a ticked check box in the document? 

Hi Alafontant,

If it were possible, I would like to ask you a question since I do not understand the error I have.

The file is created correctly, but when I try to display it I get this error.
Any idea what it could be?
Thank you for this post

fail_page-0001.jpgfail_page-0002.jpg

Hello. Can help me?

My script is returning a error: 

Exception: Invalid argument: replacement at exportDocs(Code:49:12)

Its create the document but not replace the terms on {}. 

I have two questions:

Instead of emailing, can I choose a file path in Drive to save the exported docs?

For "content," can I just put name of table?

Top Labels in this Space