How Do You Make A Google Form Populate A Template In Word
I take used a similar solution for creating documents from a template, just information technology was a spreadsheet script and used a sail as a template. Later on, I switch to a Google Md template, as it was easier for my coworkers to alter the content.
Since nobody hither was interested in the spreadsheet, I decided to readapt the Script to work from Google Form and directly create the documents from a Google Doctor template. Likewise, the script tin can accept actress code to convert the doc to a PDF and electronic mail information technology.
So, In this tutorial, nosotros are going to create a Google Course that volition apply a script to create a document from a template, save it in your drive, and, if needed, convert and ship it as PDF to an email accost.
Catch the responses in Google Form Script with trigger
The main difference from the spreadsheet script is this office. In Google Spreadsheet you tin can use "namedValues" object or "values" array to grab the responses, or y'all can but become the range from the Course response sheet.
In Google Form Script, when using an installable trigger "On form submit", google passes the event object as a FormResponse object. So, to read the responses you demand to treat them equally FormResponse object and get them past using "getItemResponses()" method. In brusque, here is a simple Google script that reads the Course responses sent past the "On form submit" trigger:
office onSubmit(e){ var items = e.response.getItemResponses(); for (i in items){ Logger.log("getItem().getTitle()=%s, getResponse()=%southward", items[i].getItem().getTitle(), items[i].getResponse()); } } Read more about Google Form events here.
Afterwards nosotros take hold of the response, the next step is to open up the template doc, create a copy of it and search for the placeholders and supervene upon them with values from the Google Form.
So, the first step is to:
Create a Google physician template
Create a new Google Doc in your Drive and design your template. For the places where y'all need to insert the information from the form, soon placeholders, use curly brackets with a short text, something like so: {Name}. Your Doc should wait similar this:
The script will look for the text {date} and replace it with the date from the Google Form, the same for the {Proper name}. For my instance I use just this two placeholder, but y'all can use as much as you lot demand.
Here is the link to my template physician: Template Doctor.
Find
Y'all should create the certificate in a new folder because the script creates the documents in the aforementioned folder as the template document.
Create a Google Form
In that location are no special requirements on how to build your class, just I recommend using only these types of questions: short answer, paragraph, multiple option, drop-down, date, and time. These types of questions only render one answer that tin easily be inserted into the doc. Get more info here: Google Forms Question Types.
And so, if you simply wanna test this instance, create a Form and add the questions: Name (short answer question), Appointment (date question), and Email (brusk answer question). You tin likewise create a re-create of my form by post-obit this link: Create a re-create of "Populate template physician" Form.
Side by side, open the script editor from the three-dot menu and insert the script.
Using the Script to Populate the Template
Now, that yous have your Form and Template gear up, you can add together the script. Equally I mentioned earlier, open up the script editor and paste this script:
office onFormSubmit(due east) { //open the template document past ID //you tin find the ID in the link of the certificate var templateDoc = DriveApp.getFileById('12EZQW2k757k4ZVPy1VyH_y-Wgx4zPF7g7zUCl-PEsH0'); //create a re-create of the template, we don't wanna mess up the template doc var newTempFile = templateDoc.makeCopy(); //open the new template document for editing var openDoc = DocumentApp.openById(newTempFile.getId()); var torso = openDoc.getBody(); //get the responses triggered by On Grade Submit var items = e.response.getItemResponses(); //detect the text in the template doc and replace it with the Course response //items[0].getResponse() is the first response in the Form body.replaceText('{Proper noun}', items[0].getResponse()); body.replaceText('{date}', items[1].getResponse()); //Salvage and Close the open document and set the name openDoc.saveAndClose(); newTempFile.setName(items[0].getResponse() + '-' + items[1].getResponse()) //If you don't need the option to ship electronic mail with PDF, //simply delete this section of the script, leave the last curly bracket // convert the new certificate to PDF and postal service information technology var theBlob = newTempFile.getBlob().getAs('application/pdf'); var electronic mail = items[2].getResponse(); var subject = 'Your new documnet'; var body = 'Hello ' + items[0].getResponse() + ', <br/>Hither is your new document.'; GmailApp.sendEmail(email, subject, body, { htmlBody: trunk, attachments: [{ fileName: items[0].getResponse() + ".pdf", content: theBlob.getBytes(), mimeType: "application/pdf" }] }); } Note
If you lot run the script from the script editor it will generate an error. The script is intended to run by using a trigger.
This short script volition open the template doc and make a re-create of it, we don't wanna change the original template. Side by side, it will go the responses from the Form and using the "replaceText" method, it will supplant our placeholders with the text from the Form. Likewise, it volition set a new name for the certificate, in my example, by using the Name and the Engagement.
If the order of the question in your Grade is different and so mine, information technology will ready a wrong text for the placeholder. So, y'all take to change the "items" number to match your lodge. In the side by side image you can see that, for example, the Name question is the First question so I use 0 to become the answer: items[0].getResponse()
What to Change
The first affair you need to alter is the ID of your template certificate, located on line 4. Get your ID from the link of your template document and insert it here.
The next affair you lot demand to change is the proper noun of the placeholders and the number in the "items" response to refer to your form/template. In my example, I have just two placeholders to change: {Name} and {date}, you can encounter the statements located on line 17 and eighteen. Unremarkably, you lot tin add more statements like this if you have more form questions/placeholders to change.
Script has an option to send an email with the PDF created from the document, if you don't demand this pick just delete everything from line 28 to 39. If you want to send emails, just check/change the "items" number from the Form responses to reflect your Form. For example, if you collect the e-mail address in the first question in your form, you need to change the "items" number to 0 on line 29, similar this:
var electronic mail = items[0].getResponse(); Create a Trigger: On Grade Submit
In guild for the script to run correctly, information technology needs to be activated through a trigger. So, to add together a trigger, click on the "Electric current projects triggers" icon, from the toolbar.
Next, from the Projects triggers window, add a new trigger:
From the New trigger dialog box, select the trigger "On Form Submit" and click on Salve.
After saving the trigger, follow the on-screen teaching to review and accept the permission. This procedure tin can get confusing, so if y'all have any problems, cheque this guide: Google Script Authorization: Review and Accept the Permission Guide.
Now, yous can go to your Course and submit a response to test information technology. If everything is fine, yous can check your Drive folder to encounter if the Document is created, and also your e-mail for the PDF.
Bonus: Save the PDF file in your Drive
Some of you have requested to include a code in the script, then you can relieve the PDF in your Drive, not just postal service information technology. I don't recommend this, peculiarly if you are on a free Google account, since the PDF counts towards the quota limit. Anyway, it's just a few lines of lawmaking and so hither it is. Insert this code at the cease of the script, just earlier the last closing bracket (paste information technology on line forty)
// salve the PDF to a Folder by using the folder ID var binder = DriveApp.getFolderById('14nUcGSmThsGvAGcxcWdpH11BoNcc0lUb'); var savePDF = folder.createFile(theBlob); savePDF.setName(items[0].getResponse() + '-' + items[1].getResponse() + '.pdf'); Since you need to be more organized, it's amend to save the PDF in a unlike binder. This is why I have included the first line of code, so you lot can add together the Folder ID where yous want the PDFs saved. And so, create a Binder in your drive, probably a sub-folder to the original binder, and go the Folder ID from the link. Paste it to supervene upon mine, and now you tin can transport in a Form response. Check the Folder for the PDF to see if everything is OK.
If you have any problems implementing this solution, check the "My executions" sections in script.google.com and postal service the fault you get in the comment section.
How Do You Make A Google Form Populate A Template In Word,
Source: https://howtogapps.com/google-form-script-to-autofill-and-email-a-doc-template/
Posted by: goddardpaptur.blogspot.com

0 Response to "How Do You Make A Google Form Populate A Template In Word"
Post a Comment