Sending marks of students has become a difficult task. The teacher often has a situation, where they need to create a document with their marks and details. In this blog post, we will cover how to create a PDF from the responses of Google Form using Google Docs Script. I will be covering a fictitious example of the following: So let’s get started. The form that I will be using is a Math paper that has to be given to the students. (If you would prefer working with a copy of the Google Form, click
here) Let us assume that all form responses have been received in the Google Sheet associated with the form. As you can see the form contains the following: More tips on Google Apps Script here Step 2: Write the Automation ScriptWhile you are in the sheet, launch the Apps Script Editor. To do that:
This brings up the Script Editor as shown below: We have reached the Script Editor. Let’s Code using driveapp.getfolderbyid and other functions.
I have to create a PDF file for each student. This will contain all the details and the marks secured by the students. For this I have created a template of the PDF file that will have the following details in it:
This is the template that I have created in Google Docs. So for each student, the code will put in the details and create a PDF. Now let me explain the different parts of the Google Docs script format we have used.
This is a function that will create PDF for multiple responses that are there in the Google Sheet. The parameter e contains all the new values received in the Google Form. How is this function invoked? It is done via what is called a Trigger. This trigger will create the PDF as soon as a new response comes into the Google Form. To create a trigger, you have to follow these steps:
2. After clicking Current Project triggers you will see something like this. 3. Now we have to add a trigger, to create a PDF as soon a new response comes into the Google Sheet. So to add a trigger click on Add Trigger. After clicking Add Trigger you will get the following options: Here you have to choose the following:
4. After doing this, let us save the trigger by clicking on save. This will help the person who has to create the PDF in the following ways:
Now let’s move on to the other parts of the code.
Here I have done two things here :
My template has been saved in a Google Doc. I need to access it and then replace the variables in the doc with the data from the sheet. In the doc I have created variables for each of the details that have to be saved in the PDF. Here I have accessed the body of the Google Doc, where the sdata from the Google Sheet is going to substitute the values.
Here I have used the replaceText function. So this will search for the variable in the Google Doc and replace it with the assigned data. This variable ( {score} ) is in Google Doc and the other one is from the Google Sheet, so it will replace it. For each row it will take the value and substitute the value in the Google Doc. So for each of the values I have created a variable in the Google Doc such as the (1) Marks, (2) Name, (3) Roll number, (4) Email Address . After putting all the values, we have to save and close the Google Doc. For this I have used the OpenDoc.saveAndClose(); function.
Here I have set the name of the PDF that has to be saved. I have assigned the name and roll number of the student as the name of the PDF file. After the PDF file has been created, I am going to print a confirmation message that will tell us that the PDF file has been created. Our code is complete. Since we have added the trigger we do not have to run the program. As soon as the responses come in, it will automatically create a PDF. To check the PDF, go to the assigned folder and there you will see the PDF file. The script on successful execution will send out an email to each recipient. A sample PDF is shown below: If the user needs something more configurable, maintainable or sophisticated, they can use the Form Publisher add-on How to Export Google Doc to PDF: The SummaryWe have seen how you can automate the process of converting Google Sheets to PDFs for multiple people. This can be used by teachers to create report cards for students. It will reduce the work of the teachers and also increase the speed of the process. This can be used by people who take workshops, who have to give certificates to the students. Instead of sitting for hours trying to make PDFs for the students, they can automate the process. This will both save their time and also increase their efficiency. How do I save a Google Form as a PDF from an email?How to save a Google Form as a PDF. Create your form.. Click the three dots in the upper right corner and select Print.. Select Save as PDF from the Destination dropdown.. Print or share through email or online (optional). How do I automatically create PDFs with Google Form responses?First you open your Google Form, then you connect either a Google Slides or Google Doc template, then you add question tags to your template and then click 'Save as PDF', and voila!, you've automated Google Form response into PDFs!
Can I export a Google Form as a PDF?Create PDF Files from Google Forms
Open your Google Form, launch the Email Notifications addon and either create a new email notification or choose the Manage Form Settings menu if you would like to add PDF capabilities to an existing rule. Check the option that says "Generate PDF Files" and save the rule.
|