Conditional logic in Apps Script
Conditional logic is used to make your program do something only if a condition is met.
IF statement
The IF statement will first check if a condition that you specify is met. If the condition is met, the code within curly braces ({ and }) will be executed. If the condition is not met, the code within the curly braces will NOT be executed.
The code within the curly braces is called the IF block. This is the code block that will get executed if the condition is met.
The condition to be checked must be an expression that evaluates to some value. If it evaluates to TRUE, the condition is deemed to be met.
if (some condition is met) {
//do something
}
Here is an example:
if (testScore >= 40 ) {
//student passed the test
return true;
}
Type conversion
The condition to be checked must evaluate to a value. However, this value does not have to be a boolean. Apps Script will convert the value to a boolean before checking to see if it is true.
For example, consider the following code.
if (10) {
//do something
}
The code within the curly braces will be executed since Apps Script will convert the value 10 to true.
The rules that govern conversion to boolean are as follows
The number 0 and an empty string ("") are converted to false.
The values null, NaN and undefined are converted to false.
All other values are converted to true.
A note on null and NaN
A null value is similar to undefined. It represents something that does not exist.
NaN stands for "not a number". This value is usually the result of Apps Script encountering something else when it is expecting a number. For example, trying to divide a number by a string will result in NaN.
Logger.log(3 / "this is a string"); //outputs NaN
IF - ELSE statement
The IF - ELSE statement has two blocks of code. The IF block and the ELSE block. The IF block is executed if the condition is met. The ELSE block is executed if the condition is NOT met.
if (some condition is met) {
//IF block
//do something
} else {
//ELSE block
//do something else
}
Here is an example:
if (testScore >= 40 ) {
//student passed the test
return true;
} else {
//student failed the test
return false;
}
IF - ELSE - IF ladder
The IF - ELSE ladder can be used to chain together a series of conditional statements. Let's say you want to assign grades based on test scores. The grading key is as follows:
A: score >= 85
B: score >= 70 and score < 85
C: score >= 55 and score < 70
D: score >= 40 and score < 55
F: score < 40
Here is how you might implement a function that accepts the test score as input and outputs the grade.
function scoreToGrade(score) {
if (score >= 85) {
return "A";
} else if (score >= 70) {
return "B";
} else if (score >= 55) {
return "C";
} else if (score >= 40) {
return "D";
} else {
return "F";
}
}
The above function first checks if the score is greater than or equal to 85. If yes, it returns "A". If not, it checks if the score is greater than or equal to 70. If yes, it returns "B". If not, it checks if the score is greater than or equal to 55. If yes, it returns "C". If not, it checks if the score is greater than or equal to 40. If yes, it returns "D". If not, there is nothing left to check, it returns "F".
Master Google Sheets Automation
I'd appreciate any feedback you can give me regarding this post.
Was it useful? Are there any errors or was something confusing? Would you like me to write a post about a related topic? Any other feedback is also welcome. Thank you so much!