Welcome folks today in this blog post we will be showing how basically you can fill out the google docs template with dynamic data using the google forms in browser using google apps script. All the full source code of the application is shown below.
Get Started
In order to get started you need to go to google drive
and make a folder inside that we will be storing all the google docs files which will be generated by the google forms as shown below
And now we need to create a google form using the menu of google drive as shown below
Now if you open the spreadsheet
file you will find the below columns where we will be storing all the user submitted information as shown below
Now we will be editing the exisiting sheet using the app script
as shown below in the menu
Now we need to create the google docs
template file as shown below
And now we need to edit the code.gs
file and copy paste the below code
code.gs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
function autoFillGoogleDocFromForm(e) { var timestamp = e.values[0]; var name = e.values[1]; var age = e.values[2]; var country = e.values[3]; var file = DriveApp.getFileById('##docfileid##'); var folder = DriveApp.getFolderById('##folderid##') var copy = file.makeCopy(age + ',' + name, folder); var doc = DocumentApp.openById(copy.getId()); var body = doc.getBody(); body.replaceText('{{name}}', name); body.replaceText('{{age}}', age); body.replaceText('{{country}}', country); doc.saveAndClose(); } |
As you can see in the above javascript code we are first of all getting the google form values and inside it we are getting the name
age and country. And then we are getting the reference of the driver folder
id and the google docs file id. You need to copy paste the id of the drive folder and the docs file. And then we are replacing the values of the name,age and country inside the template. This data is coming from the google forms.
And now we need to add the trigger inside the google apps script as shown below. This trigger is added because when we submit the form we will execute the above function as shown below
Now if we submit the google form and submit the information that information will be inserted inside the sheets
and the google doc will be generated automatically as shown below