This code works great! Once the form is submitted, it auto-populates a template I have created, then send it to my email. This works great unless the user leaves empty fields on the form. If that's the case, the order of the auto-populate is wrong... Is there anyway to automatically include a text (for example "not specified") if the user decides to leave a field blank? If the user enters a text, great! if not, then I would want it to show "not specified".
Please help!
// Requisition Form
// Get template from Google Docs and name it
var docTemplate = "the tamplate I created";
var docName = "Requisition Form";
// When Form Gets submitted
function onFormSubmit(e) {
//Get information from form and set as variables
var email_address = "myemail";
var business_entity = e.values[2];
var rotation = e.values[3];
var category = e.values[4];
var city = e.values[5];
var state = e.values[6];
var environment = e.values[7];
var date = e.values[8];
var recurring = e.values[9];
var supervisor = e.values[10];
var phone = e.values[11];
var email = e.values[12];
var background = e.values[13];
if (e.values[13]=null) {
e.values[13]='not specified';} //this does not work!
var skills = e.values[14];
if (e.values[14]=null) {
e.values[14]='not specified';} //this does not work!
var development = e.values[15];
var knowledge = e.values[16];
var responsibilities = e.values[17];
// Get document template, copy it as a new temp doc, and save the Doc’s id
var copyId = DocsList.getFileById(docTemplate)
.makeCopy(docName+' for '+supervisor)
.getId();
// Open the temporary document
var copyDoc = DocumentApp.openById(copyId);
// Get the document’s body section
var copyBody = copyDoc.getActiveSection();
// Replace place holder keys, in our google doc template
copyBody.replaceText('keyBU', business_entity);
copyBody.replaceText('keyRotation', rotation);
copyBody.replaceText('keyCategory', category);
copyBody.replaceText('keyCity', city);
copyBody.replaceText('keyState', state);
copyBody.replaceText('keyEnvironment', environment);
copyBody.replaceText('keyDate', date);
copyBody.replaceText('keyRecurring', recurring);
copyBody.replaceText('keySupervisor', supervisor);
copyBody.replaceText('keyPhone', phone);
copyBody.replaceText('keyEmail', email);
copyBody.replaceText('keyBackground', background);
copyBody.replaceText('keySkills', skills);
copyBody.replaceText('keyDevelopment', development);
copyBody.replaceText('keyKnowledge', knowledge);
copyBody.replaceText('keyResponsibilities', responsibilities);
// Save and close the temporary document
copyDoc.saveAndClose();
// Convert temporary document to PDF
var pdf = DocsList.getFileById(copyId).getAs("application/pdf");
// Attach PDF and send the email
var subject = "Requisition Form";
var body = "Here is a Requisition Form from " + supervisor + "";
MailApp.sendEmail(email_address, subject, body, {htmlBody: body, attachments: pdf});
// Delete temp file
DocsList.getFileById(copyId).setTrashed(true);
}
Get the responses using named values instead of indexes... there won't be any possible confusion even if some fields are left empty.
See documentation here.
You can retrieve the item names with the FormApp method getTitle() or type the names directly in your script since you seem to know exactly the question titles.
Example :
EDIT 2 :
(first edit was very similar but I noticed that the empty field detection was not well written. I used a compact IF form in the code below to make code simpler.)
Below is a full example that uses
namedValues
instead ofindexed values
.Btw, I added a test function that simulates a form submission with 2 empty fields.(original idea borrowed from this post (Mogsdad):How can I test a trigger function in GAS?)
Had a similar problem. Should be possible to replace lastResponses with e