Google App脚本 - 更改为不同的pdf模板(来自2个单独的Google doc的不同密钥)

时间:2018-04-04 05:52:53

标签: javascript google-apps-script google-sheets google-form gas

如果Google表格中的人选择的材料(机械印章)可以在Google表格中找到,我想要做的是生成标准的pdf文档(引用)。

如果所选材料不可用,它将生成不同的pdf(非标准材料的不同模板)。

但问题是,即使用户从Google表单中选择了非材料,它仍会生成原始pdf。

数据位于电子表格中(表格名称D标准价格) 第1列是机械密封类型,第2列是价格。

这是代码。

function onFormSubmit(e) {

var docTemplate = "insert google doc key here for template 1";    
//standard mech seal


var docName = "RFQ Mechanical Seal Quotation";
var email_address = "insert email address here";
var Time = e.values [0];
var Boffice = e.values [1];
var SalesCon = e.values [2];
var BRefNum = e.values [3];
var ToriSealQNo = e.values [4];
var EndUser = e.values [5];
var ProNo = e.values [6];
var PumpModel = e.values [7];
var EnqType = e.values [8];
var MechSealType = e.values [9];
var ReqQtty = e.values [10];
var Comment = e.values [11];
var Attach = e.values [12];
var EmailAdd = e.values [13];

var today = Utilities.formatDate(new Date(), "JST", "dd/MM/yyyy");

//to find standard price, define variable
var   result  = ""; //結果を入れる変数。
const colFrom = 1; //to search in column number Mech seal type
const colTo   = 2; //result column number D standard Price

//sheet object
var ss     = SpreadsheetApp.getActiveSpreadsheet();
var sheet  = ss.getSheetByName("D Standard Price");

//セルの内容を2次元配列に格納 - becomes 2D array 
var values = sheet.getSheetValues(1, colFrom, sheet.getMaxRows(), colTo);

//一行ずつマッチするキーワードを確認する(最初にヒットした値を格納)
 values.forEach(function(row){if(!(result) && row[colFrom - 1] == MechSealType) result = row[colTo - 1]});


//If no result then go to non mech seal template
 if(!(result)) result = "Non-Standard Mechanical Seal. Your enquiry is now being processed"
 if(!(result)) var docTemplate = "insert google doc key template 2";

var copyId = DriveApp.getFileById(docTemplate)
.makeCopy(docName+' for '+ ProNo)
.getId();

var copyDoc = DocumentApp.openById(copyId);

var copyBody = copyDoc.getActiveSection();

// This code reads through Google Doc (Template) and replaces each 
keyword with the variable (responses) that the user filled out.

copyBody.replaceText('keyTime', Time);
copyBody.replaceText('keyBoffice', Boffice);
copyBody.replaceText('keySalesCon', SalesCon);
copyBody.replaceText('keyBRefNum', BRefNum);
copyBody.replaceText('keyToriSealQNo', ToriSealQNo);
copyBody.replaceText('keyEndUser', EndUser);
copyBody.replaceText('keyProNo', ProNo);
copyBody.replaceText('keyPumpModel', PumpModel);
copyBody.replaceText('keyEnqType', EnqType);
copyBody.replaceText('keyMechSealType', MechSealType);
copyBody.replaceText('keyReqQtty', ReqQtty);
copyBody.replaceText('keyComment', Comment);
copyBody.replaceText('keyAttach', Attach);
copyBody.replaceText('keyEmailAdd', EmailAdd);
copyBody.replaceText('keyDPrice', result);
copyBody.replaceText('keyToday', today);  

copyDoc.saveAndClose();
var pdf = DriveApp.getFileById(copyId).getAs("application/pdf");
var subject = "MSRFQ :: Mechanical Seal Enquiry Received";
var body = "Dear " + SalesCon + ", thank you for submitting an enquiry.  
Attached, is a copy of the mechanical seal quotation.";
MailApp.sendEmail(EmailAdd, subject, body, {htmlBody: body, cc: email_address, attachments: pdf});
DriveApp.getFileById(copyId).setTrashed(true);

}

谢谢。

1 个答案:

答案 0 :(得分:0)

您的错误出现在这个双if语句中。在第一个语句中,如果result是空字符串,则为false或0,然后result = "Non-Standard Mechanical Seal. Your enquiry is now being processed"

当你到达第二个if result时,它现在将评估为true,因为它包含一个长度大于0的字符串。

 if(!(result)) result = "Non-Standard Mechanical Seal. Your enquiry is now being processed"
 if(!(result)) var docTemplate = "insert google doc key template 2";

要解决此问题,请将两个赋值放在同一个if语句中。

 if(!(result)){
   result = "Non-Standard Mechanical Seal. Your enquiry is now being processed";
   docTemplate = "insert google doc key template 2";
 }