Home > OS >  If e.value = null or blank then leave blank
If e.value = null or blank then leave blank

Time:03-05

 var timestamp = e.values[0];
  var recipientName = e.values[1];
  var firstLineOfAddress  = e.values [2];
  var secondLineOfAddress = e.values[3];
  var thirdLineOfAddress = e.values[4];
  var postcode = e.values [5];
  var recipientEmail = e.values[6];
  var todaysDate = e.values[7];
  var invoicenNumber = e.values[8];
  var dueDate = e.values[9];
  var item1Description  = e.values [10];
  var item1Qty = e.values [11];
  var item1UnitPrice  = e.values [12];
  var item1Amount = e.values[13];


  var templateFile = DriveApp.getFileById("1VjJI3VUNSJDQuv8NsgSfSugIfi3c_ev4cGpbk5_LQ3I");
  var templateFolder = DriveApp.getFolderById("1MsmTVhosVz0S4Nquz2qMr-SZtYZYyV9S");

  var copy = templateFile.makeCopy(recipientName, templateFolder);
  var doc = DocumentApp.openById(copy.getId());

  var body = doc.getBody();

  body.replaceText("{{RECIPIENT NAME}}", recipientName);
  body.replaceText("{{FIRST LINE OF ADDRESS}}", firstLineOfAddress);
  body.replaceText("{{SECOND LINE OF ADDRESS}}", secondLineOfAddress);
  body.replaceText("{{THIRD  LINE OF ADDRESS}}", thirdLineOfAddress);
  body.replaceText("{{Postcode}}",postcode);
  body.replaceText("{{EMAIL}}",recipientEmail);
  body.replaceText("{{DATE}}",todaysDate);
  body.replaceText("{{REF}}", invoicenNumber);
  body.replaceText("{{DUEDATE}}", dueDate);
  body.replaceText("{{desc1}}", item1Description);
  body.replaceText("{{qty1}}", item1Qty);
  body.replaceText("{{unitprice1}}", item1UnitPrice);
  body.replaceText("{{amount1}}", item1Amount)



  doc.saveAndClose();

}

Im still learning googleappscript/java so apologies for sounding dumb. Im using a form to produce a invoice however I would like the variable of e.values[10] to be able to be cleared from the doc it produces if left blank.

So if e.value is blank then it body.replace text should replace it with nothing. If e.value is filled in then body.replace text should fill in with the appropriate response.

Any ideas how I can do this?

CodePudding user response:

Each submission in form always contains timestamp. So when a user leave a question in blank, the value of it in e.values is just empty. It will produce something like this: [3/4/2022 5:50:53, , , , , , , , , , , , ].

Empty values works in body.replaceText() and not produce error. Hence you don't need to change anything in your code.

In case you have null value in your e.values, which can be done by editing the content of e.values, just append a ||'' next to the replacement string.

Your code should look like this:

body.replaceText("{{RECIPIENT NAME}}", recipientName||'');
body.replaceText("{{FIRST LINE OF ADDRESS}}", firstLineOfAddress||'');
body.replaceText("{{SECOND LINE OF ADDRESS}}", secondLineOfAddress||'');
body.replaceText("{{THIRD  LINE OF ADDRESS}}", thirdLineOfAddress||'');
body.replaceText("{{Postcode}}",postcode||'');
body.replaceText("{{EMAIL}}",recipientEmail||'');
body.replaceText("{{DATE}}",todaysDate||'');
body.replaceText("{{REF}}", invoicenNumber||'');
body.replaceText("{{DUEDATE}}", dueDate||'');
body.replaceText("{{desc1}}", item1Description||'');
body.replaceText("{{qty1}}", item1Qty||'');
body.replaceText("{{unitprice1}}", item1UnitPrice||'');
body.replaceText("{{amount1}}", item1Amount||'') 
  • Related