I have modified the "employee of the week award" and cannot get it to pull accurate data to my document upon from submission.
My goal is to:
1) Fill out the form with the required details
2) Have the script gather those details and insert them into my docs template in the proper key holders
3) Create a 'new' copy of the doc in a pdf with title change
4) Email would be great but not necessary as long as it saves the new document
I can get it to create a new document and run the script but it does not seem to be pulling the correct (or any) data from the form or spreadsheet. It keeps showing 'undefined' where it should be showing the values I inputed into the form. Please help me see my errors. I have literally spent hours trying to figure it out and re-write code. Thanks in advance!
Here are links to my document and spreadsheet, form, and script I am using.
https://docs.google.com/document/d/1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk/edit
https://docs.google.com/spreadsheet/ccc?key=0AkKN7xCpxU54dENoZ0RoSnF1QXhQNnAyX3ZiNmVwRGc#gid=0
Here is my current script:
function sendDocument() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = sheet.getLastRow(); // First row of data to process
var numRows = 1; // Number of rows to process // Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1,2) // Fetch values for each row in the Range.
var data = dataRange.getValues();
for (i in data) {
var row = data[i];
var ID = row[1]; // First column
var facility_name = row[2]; // Second column
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById("1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk")
.makeCopy("Copy of Mail Merge Doc template"+' for '+facility_name)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys/tags,
copyBody.replaceText('keyFacilityName', facility_name);
copyBody.replaceText('keyID', ID);
var todaysDate = Utilities.formatDate(new Date(), "GMT", "MM/dd/yyyy");
copyBody.replaceText('keyTodaysDate', todaysDate);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF by using the getAs blob conversion
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Delete temp file
DocsList.getFileById(copyId).setTrashed(false);
}}
I have also tried the following two options at the beginning of my script to 'pull' the data from the form to create the 'new' doc with no success...
// Global variables
docTemplate = “1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk”;
docName = “Copy of Mail Merge Doc template”;
function sendDocument() {
// Full name and email address values come from the spreadsheet form
var ID = from-spreadsheet-form
var facility_name = from-spreadsheet-form
...continue script as above...
AND
// Global variables
docTemplate = “1A34uyNyMzp3o8XBzmIGqqfvCH6ocpQWN4HaZ5f4AiDk”;
docName = “Copy of Mail Merge Doc template”;
function onFormSubmit(e) {
// Full name and email address values come from the spreadsheet form
var ID = e.values[1];
var facility_name = e.values[2];
...continue script as above...
Please help if possible.