如何修改此代码以包含多个模板?

时间:2017-04-20 17:03:09

标签: google-apps-script

目前,此脚本使用我的模板进行客户合同并替换"密钥"使用活动行中的值,创建pdf并通过电子邮件发送。我有3种不同风格的模板供我使用。

在我的电子表格的列b中(列a是时间戳)我想在模板1,2或3之间进行选择。然后当我运行脚本时,它应该根据列b得到模板;替换文本,并像现在一样创建pdf和电子邮件。

模板的唯一区别是样式和徽标。所有"键"和变量是一样的。我希望脚本根据活动行中b列的值决定3个模板,然后按现在的步骤进行操作。感谢

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);
}

1 个答案:

答案 0 :(得分:0)

我取而代之:

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

用这个:

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";

我根据这段代码自己找到了答案:

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";}

我在这里找到了:

Google Apps Script Conditional If/Else If Statement