user1828299
user1828299

Reputation: 11

Merge Form Submission to document - lost newbie

Seeking guidance on the following:

User submits Formstack form -> Form entry populates a row on Google Spreadsheet -> Trigger the execution of a document merge for that submission (now a ROW in the sheet) with the column header defining the variables -> Email merged document to User via submitted email address.

My document requires the ability to conditionally handle many of the variables (if/then conditional text based on form response). I've played with an App called Ultradoc which seems great for the variable handling in the Google Document, supports conditions, etc.

However, the problem is it doesn't know how to select one ROW of data, rather it's designed to run a merge for everything in each column.

One idea might be to run a script which takes that new ROW and somehow makes it look like a two row sheet (header+submitted row)?? Somehow hides the other rows? This seems terribly kludgy. What's the right way to approach something like this?

Thanks in advance

Upvotes: 1

Views: 260

Answers (1)

bigelowr
bigelowr

Reputation: 431

You actually don't need any third party apps for this. Create the form as a Google Form, create the script and make it run on Form submit.

To get the values that were entered into the form, pass the form submission as a parameter to the function: function processForm(e){

To get the data in the form, access the e.values array. It's zero indexed, starting with the top item on the form.

Store the submitted data into variables

var name = e.values[0];
var email = e.values[1];
// and so on...

Perform any validation or document handling.

Create a copy of your template document

var copyId   = DocsList.getFileById("templateDocID")
            .makeCopy(docName+' from '+name) //or whatever you wanted to call the resulting document
            .getId();
var copyDoc = DocumentApp.openById(copyId);
var copyBody = copyDoc.getActiveSection();

Replace the text in the template

copyBody.replaceText("NAME", name);
copyBody.replaceText("EMAIL", email);
// and so on...

Send the document to the user (for my application, I sent as pdf, but you can send it how ever you like)

copyDoc.saveAndClose();
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
MailApp.sendEmail(email, copyDoc.getName(), "Here's your document", {attachments:pdf});
DocsList.getFileById(copyId).setTrashed(true);

Upvotes: 2

Related Questions