Search code examples
javascriptgoogle-apps-scriptgoogle-sheetsgoogle-forms

Google App Script - Change to different pdf template (different key from 2 separate google doc)


What I am trying to do is to generate a standard pdf document (quotation) if the material(mechanical seal) chosen by the person from Google Form is available in Google sheets.

If the material chosen is not available, it will generate a different pdf (different template for non standard material).

But the problem is it will still generate the original pdf even if a non material is selected by the user from Google Form.

The data is located in the spreadsheet (sheet name D standard Price) column 1 is the mechanical seal type and column 2 is the price.

Here's the code.

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

}

Thank you.


Solution

  • Your error is in this double if statement. In the first statement if result is an empty String, false or 0 then result = "Non-Standard Mechanical Seal. Your enquiry is now being processed"

    When you get to your second if result will now evaluate to true since it contains a String with a length greater than 0.

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

    To fix this put both assignments inside the same if statement.

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