mercredi 18 novembre 2020

Trying to use if and Else if to select a template based on a google sheet cell response

I'm new to google scripts and I've created a google form that when an input in one of the cells changes a different template is used and populated then emailed. I created a similar more google form with only one template and would appreciate any help as its currently not working. Here's the code that I have so far:

function myFunction(e) {
var timestamp = e.values[0];
var applicationDate = e.values[1];
var customerName = e.values[2];
var occupation = e.values[3];
var teamMember = e.values[4];
var tmCell = e.values[5];
var address = e.values[6];
var city = e.values[7];
var postcode = e.values[8];
var tmEmail = e.values[9];


if(occupation = 'option1'){var file = DriveApp.getFileById('template ID removed')};
else if( occupation = 'option 2'){var file = DriveApp.getFileById('template ID removed')}; 
else if( occupation = 'option 3'){var file = DriveApp.getFileById('template ID removed')};
else if( occupation = 'option 4'){var file = DriveApp.getFileById('template ID removed')};
else if( occupation = 'option 5'){var file = DriveApp.getFileById('template ID removed')};
else ( occupation = 'option 6'){var file = DriveApp.getFileById('template ID removed')};


var folder = DriveApp.getFolderById('folder ID removed')
var copy = file.makeCopy('Client Recommendation'+ ','+ ' ' + customerName + ','+ ' ' + applicationDate, folder); 

var doc = DocumentApp.openById(copy.getId()); 

var body = doc.getBody(); 

body.replaceText('', applicationDate); 
body.replaceText('', customerName);  
body.replaceText('', occupation); 
body.replaceText('', teamMember);
body.replaceText('', tmCell);
body.replaceText('', address);
body.replaceText('', city);
body.replaceText('', postcode);
body.replaceText('', tmEmail);

doc.saveAndClose(); 

var pdffolder = DriveApp.getFolderById("drive ID removed");
var pdfFILE = DriveApp.getFileById(doc.getId()).getAs('application/pdf');
pdfFILE.setName(doc.getName() + ".pdf");
var theFolder = pdffolder;
var theFile = DriveApp.createFile(pdfFILE);
theFolder.addFile(theFile);

var pdfEMAIL = DriveApp.getFileById(doc.getId()).getAs('application/pdf').getBytes();
var message = "Here's the client recommendations for " + customerName  + " " +".\nMany Thanks!\nGo Oilers!";
var emailTo = tmEmail; // Team Members email
var subject = "Client Recommendations, " + customerName + ", " + applicationDate;
var attach = {fileName:"Client Recommendations " + customerName +'.pdf',content:pdfEMAIL, mimeType:'application/pdf'};
MailApp.sendEmail(emailTo, subject, message, {attachments:[attach]}); //

}

Aucun commentaire:

Enregistrer un commentaire