Apps Script Course: Simple Operations

When you assign a value to a variable, remember that the variable on the left of the = receives the value of what is on the right of the =.


Value Assignment

Observe this code, determine what values will be returned by console.log, then check in the Apps Script editor as on the previous page:

let value1 = 10;
let value2 = 20;

value1 = value2;
value2 = value1;

console.log(value1);
console.log(value2);

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

The returned values of the 2 variables are 20 and 20:

let value1 = 10; // value1 = 10
let value2 = 20; // value2 = 20

value1 = value2; // value1 = value2 (which is 20), so = 20
value2 = value1; // value2 = value1 (which is 20), so = 20

console.log(value1); // Displays: 20
console.log(value2); // Displays: 20
Text preceded by // is a comment and is ignored during code execution. Comments serve to provide more information to the code and to make them more readable. You can also comment out a line of code so it's ignored if you don't want to delete it right away.

Observe this new code, determine what value will be returned by console.log, then check in the editor:

let value = 10;

value = value / 2;

console.log(value);

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

The returned value is 5:

let value = 10; // value = 10

value = value / 2; // value = value (which is 10) divided by 2, so 5

console.log(value); // Displays: 5

Operators

The operators you can use for your calculations are:

+Addition
-Subtraction
*Multiplication
/Division
**Exponent
%Remainder (modulo)

For example:

let result;
let value1 = 10;
let value2 = 2;

result = value1 + value2; // 10 + 2 = 12
result = value1 - value2; // 10 - 2 = 8
result = value1 * value2; // 10 * 2 = 20
result = value1 / value2; // 10 / 2 = 5
result = value1 ** value2; // 10 ** 2 = 100
result = value1 % value2; // 10 % 2 = 0 (the remainder of dividing 10 by 2 = 0)

Operation on the Same Variable

For example, to increase the value of a variable by 1 (as in the case of a counter), you must assign the variable its own value + 1:

let value;

value = value + 1;

To avoid repeating value, you can add the operator just before the =:

let value;

value += 1;
This simplification is possible with all the operators listed above.

And in the particular case of the operations + 1 and - 1 you can simply write ++ or --:

let value;

value++;

These 3 lines are therefore equivalent (it's up to you to choose the version you prefer):

value = value + 1;
value += 1;
value++;

Concatenating Text

To assemble multiple strings into one, simply use the + operator:

let text1 = 'Sheets';
let text2 = 'Pratique';

text1 = text1 + '-' + text2;

console.log(text1); // Displays: Sheets-Pratique

For text, it is also possible to use the assignment operator +=:

let text1 = 'Sheets';
let text2 = 'Pratique';

text1 += '-' + text2;

console.log(text1); // Displays: Sheets-Pratique

So be careful when performing additions, because if the numbers are in text format, you might have some surprises...

Observe this piece of code and try to guess what values will be returned by console.log, then check in the editor:

console.log('5' + '4');
console.log('5' + 4);
console.log(5 + '4');
console.log(5 + 4);

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

All operations with a number in text format were concatenated and not added:

console.log('5' + '4'); // Displays: 54
console.log('5' + 4); // Displays: 54
console.log(5 + '4'); // Displays: 54
console.log(5 + 4); // Displays: 9

When a number is in text format, you can convert it to numeric format with Number (or * 1):

let number1 = '10'; // Text
let number2 = Number('10'); // Number
let number3 = '10' * 1; // Number

console.log(number1 + 5); // Displays: 105
console.log(number2 + 5); // Displays: 15
console.log(number3 + 5); // Displays: 15

Minimum / Maximum

To find the largest value, use Math.max (the equivalent of the Google Sheets function MAX) and enter the different values as arguments:

let salesJanuary = 45;
let salesFebruary = 37;
let salesMarch = 42;

let maxSales = Math.max(salesJanuary, salesFebruary, salesMarch);

console.log(maxSales); // Displays: 45

And for the smallest value, use Math.min (the equivalent of the Google Sheets function MIN):

let salesJanuary = 45;
let salesFebruary = 37;
let salesMarch = 42;

let minSales = Math.min(salesJanuary, salesFebruary, salesMarch);

console.log(minSales); // Displays: 37

Const

So far, variables have been declared with the let instruction, intended to create variables whose assigned value can change later.

There is also the const instruction which allows you to declare a constant.

Like a variable declared with let, a constant can be assigned all sorts of values, however, the assigned value can no longer be changed.

In the following code, you can see that only the value assigned to total changes, and for the others, a declaration as a constant suits very well:

let total = 0;

const salesJanuary = 45;
total += salesJanuary;

const salesFebruary = 37;
total += salesFebruary;

const salesMarch = 42;
total += salesMarch;

console.log(total); // Displays: 124

Var

There is a third instruction, var, which allows declaring a variable like let (although with differences in scope that are not useful to detail at this stage of the course).

The var instruction is an old instruction (which was the only possible declaration at the time when let and const did not exist).

There is not really any interest in using it here, so it will not be used in this course, but know that you will sometimes see it in certain publications and documentation because it is still widely used.