If e.value = null or blank then leave blank - javascript

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?

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

How to partially change a file name in google apps script?

function autoFillGoogleDocFromForm(e) {
var timestamp = e.values[0];
var auditorFirmName = e.values[1];
var auditorAddressLine1 = e.values [2];
var auditorAddressLine2 = e.values[3];
var auditorAddressLine3 = e.values[4];
var auditorAddressLine4 = e.values [5];
var dateOfLetterIssue = e.values[6];
const template = "template";
var templateFile = DriveApp.getFileById("");
var templateFolder = DriveApp.getFolderById("");
var copy = templateFile.makeCopy(auditorFirmName + ${template}, templateFolder);
var doc = DocumentApp.openById(copy.getId());
var body = doc.getBody();
body.replaceText("{{Insert Auditor Firm name}}", auditorFirmName);
body.replaceText("{{Address Line 1}}", auditorAddressLine1);
body.replaceText("{{Address Line 2}}", auditorAddressLine2);
body.replaceText("{{Address Line 3}}", auditorAddressLine3);
body.replaceText("{{Address Line 4}}",auditorAddressLine4);
body.replaceText("{{Date}}",dateOfLetterIssue);
doc.saveAndClose();
}
Currently the line var copy = templateFile.makeCopy(auditorFirmName, templateFolder); is creating a copy of the original doc with the file name of the auditor firm as I have requested. But what I would like to do is create a file name with the auditor firm name and the wording template for example. Or a fixed piece of text that is not a variable. E.g currently the file will save as John Smith Auditors where I want it to save as John Smith Auditors Template. Where John Smith stays as a variable but the word Template would always remain fixed. Is this possible?
How about changing this line var copy = templateFile.makeCopy(auditorFirmName, templateFolder); to this var copy = templateFile.makeCopy(auditorFirmName + template, templateFolder); and add a const template="whatever words you want to add"; earlier in the file near the top. You could just add + "Template" if you like but typically your needs tend to change so the use of a variable may be useful but it's up to you. If I'm not understanding you question I apologize.

Date doesnt work - tag value in setFullYear

var d = new Date();
var gunler= ["Pazar","Pazartesi","Salı","Çarşamba","Perşembe","Cuma","Cumartesi"];
document.getElementById("gun").innerHTML = gunler[d.getDay()];
var ic = document.getElementsByClassName("i");
var dis = document.getElementsByClassName("d");
var ll = document.getElementById("seciligun");
var ll2 = ll.innerHTML;
alert(ll2);
d.setFullYear(ll2);
var a=0;
while(a<ic.length){
if(gunler[d.getDay()]=="Cumartesi"||gunler[d.getDay()]=="Pazar"){
ic[a].innerHTML="";
}
else{
dis[a].innerHTML="";
}
a=a+1;
}
I using these codes in my js file , i taking date in my div tag that id = seciligun. When i use alert it seems like this : 2020-08-15 but when i say alert(d) , browser saying "invalid date". If i write like this d.setFullYear(2020-08-15) everything works correctly but i need use tag's value that id = seciligun.
You need to extract the year from ll2.
var y = ll2.split('-')[0];
d.setFullYear(y);

What is the significance of the if statement in this code?

I am currently learning JavaScript through some tutorials and this example came out in the tutorial.
I'm just confused why must the if statement be there.
I tried erasing the if statement and it still worked. Can someone help me please.
var timesVisited=0;
var dateVisited = 'Never';
if(localStorage.myLastVisit){
var visit = JSON.parse(localStorage.myLastVisit);
timesVisited = visit.numVisits;
dateVisited = visit.dateVisits;
}
$("#dateVisit").html(dateVisited);
timesVisited++;
$("#numVisit").html(timesVisited);
var myVisits = {};
myVisits.numVisits = timesVisited;
var d = new Date();
var hours = d.getHours();
var minutes = d.getMinutes();
myVisits.dateVisits = hours + ':' + minutes;
localStorage.myLastVisit = JSON.stringify(myVisits)
I tried erasing the if statement and it still worked.
Only if myLastVisit is already in localStorage. If it isn't there (which is what the if is testing for), without the if you'll get an error from JSON.parse because you'll pass undefined into it, which will get converted to a string with the charactersundefined in it because JSON.parse requires a string, which will then fail because that's not valid JSON. The if is there so that if the setting isn't present, the default values assigned to timesVisited and dateVisited are used.
Works if the setting is there:
var timesVisited=0;
var dateVisited = 'Never';
var visit = JSON.parse(`{"numVisits": 2, "dateVisits": "2020-03-27"}`);
timesVisited = visit.numVisits;
dateVisited = visit.dateVisits;
console.log(timesVisited); // 2
console.log(dateVisited); // "2020-03-27"
Fails if it isn't:
var timesVisited=0;
var dateVisited = 'Never';
var visit = JSON.parse(undefined); // ERROR
timesVisited = visit.numVisits;
dateVisited = visit.dateVisits;
console.log(timesVisited);
console.log(dateVisited);

get value of string before and after / in javascript

suppose i have 10 localstorage like blow:
localStorage.setItem("item01","this is a value/100");
localStorage.setItem("item02","this new is a value/110");
localStorage.setItem("item03","this is a good value/120");
localStorage.setItem("item04","this is a nice value/130");
I need a java script code to check if the key of for example item01 is not 0 then put the data of item01 before / to xyz and the data after / to rfv variable.
I would suppose you split the data-string along the character "/". Something like:
var lsData = localStorage.getItem("item01");
var dataArray = lsData.split("/");
if(dataArray.length === 2){
var xyz = dataArray[0];
var rfv = dataArray[1];
}
else{
... error-code (could not split exactly)...
}
This is an answer to your question "get value of string before and after / in javascript".
Like I stated in the comments you can split a string into an array of substrings using a delimiter.
jsfiddle
var str= "this is a value/100";
var temp=str.split("/");
var xyz = temp[0]; //->this is a value
var rfv = temp[1]; //->100
alert('xyz = '+xyz+'\nrfv = '+rfv);
this should do it...
I'm assuming you know how to get the localStorage value, so I'm just posting an example.
var a = "this is a value/100"
var b = /^(.*?)\/(\w+)/.exec(a);
var text = b[1]
var value = b[2]

Having trouble converting user input into a graphic bar

The code seems to work fine when inputting numbers 1-9 but anything above doesn't work, what could be the issue? Here is the code:
var varkString = prompt('Enter your VARK scores - [visual|aural|read|kinesthetic]','9|3|11|10');
var subStrings = varkString.split('|');
var visual = varkString[0];
var aural = varkString[1];
var read = varkString[2];
var kinesthetic = varkString[3];
var varkBar = 30*visual
document.writeln('<img src="bar_blue.png" width='+varkBar+' height="25"/>');{
}
Edit: Solved
You are parsing first character when you are getting visual, second on aural and third on read.
I belive that you want to use subStrings
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
when you are slpiting the string varkString the array will automatically constructed and assigned to subStrings.so use it like this:
var subStrings = varkString.split('|');
var visual = subStrings[0];
var aural = subStrings[1];
var read = subStrings[2];
var kinesthetic = subStrings[3];

Categories