'How do I modify this code to include multiple templates?

Currently this script uses my template for customer contracts and replaces "keys" with values from active row, creates a pdf and emails it. I have 3 different styles of templates that I want to use.

In column b of my spreadsheet (column a being timestamp) I want to choose between template 1,2 or 3. Then when I run the script it should get the template based on column b; replace the text, and create pdf and email just as it is now.

The only difference in the templates is the style and logo. All "keys" and variables are the same. I want to have the script decide between 3 templates based on the value in column b in the active row and then proceed as it does now. Thanks

var docConTemplate = "1uQnP_Z7TqPgMrk9SFzc";
var docConName = "Customer Contract";

function sendContract() {
//Get information from form and set as variables 

var ActiveSheet = SpreadsheetApp.getActiveSheet();
var ActiveRow = ActiveSheet.getActiveRange().getRow();
var cus_nam = ActiveSheet.getRange("B"+ActiveRow).getValue();
var cus_add = ActiveSheet.getRange("E"+ActiveRow).getValue();
var tod_dat = Utilities.formatDate(new Date(), "GMT-4", "EEEE MMMM dd, 
yyyy");
var cus_pho = ActiveSheet.getRange("D"+ActiveRow).getValue();
var cus_ema = ActiveSheet.getRange("C"+ActiveRow).getValue();
var num_bus = ActiveSheet.getRange("J"+ActiveRow).getValue();
var date_bb = ActiveSheet.getRange("F"+ActiveRow).getValue();
var dep_dat = Utilities.formatDate(date_bb, "GMT-4", "EEEE MMMM dd, 
yyyy 'at' hh:mm a");
var dep_add = ActiveSheet.getRange("G"+ActiveRow).getValue();
var des_des = ActiveSheet.getRange("H"+ActiveRow).getValue();
var date_cc = ActiveSheet.getRange("I"+ActiveRow).getValue(); 
var ret_dat = Utilities.formatDate(date_cc, "GMT-4", "EEEE MMMM dd, 
yyyy 'at' hh:mm a");
var cus_not = ActiveSheet.getRange("L"+ActiveRow).getValue();
var price = ActiveSheet.getRange("K"+ActiveRow).getValue();

// Get document template, copy it as a new temp doc, and save the 
Doc’s id
var copyId = DriveApp.getFileById(docConTemplate)
.makeCopy(docConName+' for '+dep_dat)
.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('keyCusNam', cus_nam);
copyBody.replaceText('keyCusAdd', cus_add);
copyBody.replaceText('keyDat', tod_dat);
copyBody.replaceText('keyPho', cus_pho);
copyBody.replaceText('keyEma', cus_ema);
copyBody.replaceText('keyDepDat', dep_dat);
copyBody.replaceText('keyDepAdd', dep_add);
copyBody.replaceText('keyDesAdd', des_des);
copyBody.replaceText('keyRetDat', ret_dat);
copyBody.replaceText('keyCusNot', cus_not);
copyBody.replaceText('keyPri', price);
copyBody.replaceText('keyNumBus', num_bus);  

// Save and close the temporary document
copyDoc.saveAndClose();

// Convert temporary document to PDF
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");

// Attach PDF and send the email
var subject = "Contract";
var body = "Thank you for your business! Here is the contract for  " + 
dep_dat + ".";
  MailApp.sendEmail(cus_ema, subject, body, {htmlBody: body, 
    attachments: pdf});

// Delete temp file
DriveApp.getFileById(copyId).setTrashed(true);
}


Solution 1:[1]

I replaced this:

var docConTemplate = "1uQnXrMLUP_Tq9SFzc";
var docConName = "Customer Contract";

with this:

var docInvTemplate;
var sheet = SpreadsheetApp.getActiveSheet();
var row = sheet.getActiveRange().getRowIndex();
var company = sheet.getRange("B"+row).getValue();
if (company == "company1") {docInvTemplate = 
"1eRABNlFxW0KafpX6x1qOXwxHzYZ1ernQVm9kg79E";}
else if (company == "ompany2") {docInvTemplate = 
"1uKXirM26fHkp7qc9YB4WTBHjzZqurAADEE8NAY";}
else if (company == "company3") {docInvTemplate = 
"1CMNxBbo6qt3CsuGwTofAxMdWPH_2oE3ADHYc";}
var docInvName = "Customer Invoice";

I found the answer myself, based on this code:

var initialcontact;
var row = sheet.getActiveRange().getRowIndex();
var urgency = sheet.getRange(row, 
getColIndexByName("Urgency")).getValue();
if (urgency = "Critical") {initialcontact = "1 hour";}
else if (urgency = "High") {initialcontact = "4 hours";}
else if (urgency = "Medium") {initialcontact = "1 day";}
else if (urgency = "Low") {initialcontact = "3 days";}

that I found here:

Google Apps Script Conditional If/Else If Statement

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Cœur