Use Google Apps Scripts to:
-
Create a form linked to a sheet and send an email as
- access data from a google form
- access a google doc template
- make a copy of the template
- edit the copy
- send it as a pdf attachment by email.
-
Create a Dependent Dropdown Cell in Google Sheets
- Access data from a second Google Sheet
- Loop through a collection of data
- Create a dropdown cell that is dependent on data from another Dropdown
-
Create a folder in Google Drive
-
Create a google sheet in the drive.
-
Under the tools option in the Google sheet, create a form for the sheet.
-
Click on the link or click on tools and navigate to 'edit form'.
-
Select 'Settings' from the dropdown menu from creating the form. Choose 'Collect email addresses'.
-
Create questions for the form.
-
In the form creation, click on responses.
-
Look for the green flag with the plus, click this. This will take you to the google sheet with which the form is associated. If there is no sheet linked to the form, create a new google sheet. This will populate the sheet with headers that correspond with your question values.
-
Navigate to the google sheet linked to the form.
-
Click on 'Tools' in the google sheet.
-
Click on script editor. Enter the script editor page.
-
Change the name of the function to
function onFormSubmit(e){}
. This will change the function displayed in the menu from 'myfunction' to 'onFormSubmit.' This is very important. Changing the name of the function toonFormSubmit
gives the script authorization to use the functionopenById
which is necessary to edit the pdf. -
Click on Edit in the menu. Click on 'Current Project's triggers' near the bottom of the dropdown menu. A window will pop up.
-
Click on 'No triggers set up. Click here to add one now.'
-
Create new trigger with these options in the three drop down menus:
- Run 'onFormSubmit'
- Events 'from spreadsheet'
- 'on form submit'
-
Save the trigger.
-
Accept permissions for the script. This allows the script to view the spreadsheet and run code for it.
-
Choose an account for the permissions(your account). Allow for permissions.
-
Copy and paste the code from workStatusFormScript.js. Get ready enter some specific values and variables for your script.
This code describes the following
- getting the template for the pdf by ID
- accessing form values
- copying the template
- editing text in the template with values from the form
- converting the template file to a pdf
- sending the pdf by email
-
Get a template for your pdf.
-
Get the id number for the template. The template described below is a To Do List template. Find your own or create one from indtructions (here)[https://www.wikihow.com/Create-a-Template-in-Google-Docs] Every google template has a URL. The id number is found in a google docs url. https://docs.google.com/document/d/1FQAJ8v0oWv0EM7uHXp18bKPYaClwPVmnUqG2DwO9GzY/edit
- id = 1FQAJ8v0oWv0EM7uHXp18bKPYaClwPVmnUqG2DwO9GzY
-
Create a variable called documentId and set it equal to the template id.
var docTemplate = "1FQAJ8v0oWv0EM7uHXp18bKPYaClwPVmnUqG2DwO9GzY"
- Create a variable called docName and set it equal to the name of your template.
var docName = "name-of-your-template"
- Within the function onFormSubmit(), create the variable email and set it equal to the email address of the person or personsyou would like to email the pdf to.
var email = "bob@greatemail.com, melinda@greateremail.com"
- Access the values from the form using
e.values
. This returns an array of values. The value at the 0 position of the array,e.values[0]
, is the timestamp. When creating the form, if you have chosen to 'Collect email addresses,' the email address of the respondent will be at the 1 position,e.values[1]
. All other values are listed by the order of how the questions were created.
var date = e.values[0];
var respondent = e.values[1];
var firstQuestion = e.values[2];
var secondQuestion = e.values[3];
var thirdQuestion = e.values[4];
- Create a variable called
copyId
and set it equal toDriveApp.getFileById(docTemplate)
. Notice that the method getFileById() takes the variabledocTemplate
which we assigned in step 3.
var copyId = DriveApp.getFileById(docTemplate)
- Chain the method .makeCopy() the the previous code and pass the variable
docName
to .makeCopy().
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName)
- To customize the name of the copy of the template, concatenate a string to docName(add a word in quotes).
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName + "copy")
- Chain .getId() to the functions in step 9. copyId is now equal to the ID of the copy of the template we just made.
var copyId = DriveApp.getFileById(docTemplate).makeCopy(docName + "copy").getId();
- Create a variable called
copyDoc
and set it equal toDocumentApp.openById(copyId)
. This creates an actual copy of the template in the same file in which the original file exists.
var copyDoc = var copyDoc = DocumentApp.openById(copyId);
- Create a variable called
copyBody
and set it equal tocopyDoc.getActiveSection()
.copyBody
is now an object, and the text of this object can now be edited.
var copyBody = copyDoc.getActiveSection();
- Chain the method replaceText() to copyBody.
copyBody.replaceText()
Replace text takes two arguments. The first argument is the text to be replaced and second argument is the text that is replacing the original text. We identify the text in our template by snake case starting with the word 'key', i.e., 'keyDate' in the template will be replaced with our variable date
.
- Pass the key, 'keyDate', from the template to the method replaceText() and then pass variable from the form,
date
.
copyBody.replaceText('keyDate', date);
-
Replace all other text in a similar manner. Choose the key from the template, pass it as the first argument to replaceText, choose the value from the form and pass it as the second argument.
-
Call saveAndClose() on copyDoc to finish editing.
copyDoc.saveAndClose();
- Create a variable called
pdf
and set it equal toDriveApp.getFileById(copyId)
. Notice that we pass the varcopyId
to the method getFileById().
var pdf = DriveApp.getFileById(copyId)
- Chain the method getAs("application/pdf") to the code in step 17.
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
We now have a pdf of the edited template.
- Create a variable called
subject
and set it equal to a string. This will be the subject line for the email. You may also concatenate any variables from the form by adding them to the string, i.e.
var subject = 'A form was sent on this date and time' + date;
- Create a variable called
body
and set it equal to a string.
var body = 'This is the body of the email which may contain a variable like the Respondent: " + respondent;
- Pass the variables
subject
andbody
to the MailApp.sendEmail().
MailApp.sendEmail(subject, body)
sendEmail() takes an option to send an attachment. Let's pass in that option.
- Pass in the option
{attachments: pdf}
to sendEmail().
MailApp.sendEmail(subject, body, {attachments: pdf});
- Get the copied and edited template by passing its ID,
copyId
, to DriveApp.getFileById().
DriveApp.getFileById(copyId)
- Chain the method setTrashed() to the code in line 23 and pass the boolean
true
to the function.
DriveApp.getFileById(copyId).setTrashed(true);
You can now fill out the form, create a pdf and send it as an email. Enjoy!
-
Select a range of cells.
-
Right click to create validation of values for the dropdown selection of each cell in the range of cells.
-
Choose a range of data from a spread sheet, i.e.,
'Your-sheet'!A1:A10
-
Identify a collection of data in a second spreadsheet associated with the first dropdown menu.
-
Create a collection of arrays with associated data.
-
Loop through the array of arrays and identify where values meet a condition dependent on the first dropdown cell value.
-
Create a collection from all values that meet the previous condition.
-
Use this collection to create a rule for a validation.
-
Create the validation