2017-04-20 56 views
-1

目前,此腳本使用我的模板來處理客戶合同,並用活動行中的值替換「鍵」,創建PDF並通過電子郵件發送。我有3種不同樣式的模板,我想使用它們。如何修改此代碼以包含多個模板?

在我的電子表格(列a是時間戳)的b列中,我想要在模板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); 
} 
+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";} 

,我發現在您的網站上:

http://stackoverflow.com/questions/20404002/google-apps-script-conditional-if-else-if-statement 

請隨時糾正我的語法。

相關問題