-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAge.js
27 lines (23 loc) · 1.15 KB
/
Age.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/***************************************************************************************
* The function below sets an arrayformula in F2 of 'Student Database' that fills down *
* column F. *
* *
* The formula calculates the student's age based on the date of birth in column E. *
* *
* A trigger is set to run this function on edit. *
***************************************************************************************/
function insertFormula() {
let sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Student Database");
let targetCell = 'F2';
let formula = '=arrayformula(DATEDIF(E2:E,TODAY(),"Y"))';
let cell = sheet.getRange(targetCell);
let currentValue = cell.getValue();
if (isEmptyValue(currentValue)) {
cell.setFormula(formula);
} else {
Logger.log("Good");
}
}
function isEmptyValue(value) {
return value === "" || value === null;
}