I'm trying to change the reference cell in the DATE function in an if loop in Apps Script. The cell I'm referencing is formatted in the following way: "2020-01-31 14:26:58." Here is what I got so far:
var s = SpreadsheetApp.getActive().getSheetByName("Prep");
var data = s.getRange("I2:I").getValues();
var data_len = data.length;
for(var i=0; i<data_len; i++) {
if(data[i][0].length !== 0) {
s.getRange(i+2,4).activate();
s.getCurrentCell().setFormula('=TEXT(DATE(2020,MID(I2,6,2),1),"mmmm")');
} else {
}
}
This works fine if all of the data is from the same month. However, I'm trying to get I2 to update to I3, I4...IX as I go through the loop so that if the data references more than one month, it will be changed accordingly. I've tried Googling solutions and looking at videos and posts but I can't figure it out.
Markdown Example (Bolded text is what I want the output to show):
Link | Type | Year | Month | First Name | Last Name | Gender | Age | Application Submitted |
---|---|---|---|---|---|---|---|---|
- | Applicant | 2020 | January | John | Doe | M | 40 | 2020-01-31 14:26:58 |
- | Applicant | 2020 | February | Jill | Bradley | F | 45 | 2020-02-15 11:50:06 |
- | Applicant | 2020 | March | Jane | Alice | F | 41 | 2020-03-31 10:36:04 |
You could use the value of i
to set the value dynamically:
s.getCurrentCell().setFormula('=TEXT(DATE(2020,MID(I'+(i+2)+',6,2),1),"mmmm")');
Other option would be to set an array of formulas and use only once setFormulas
And a third option is not to use a script but an array formula in D1 (first, delete all formulas in that column:
=VSTACK("Header",MAP(I2:I,LAMBDA(each, IF(each="","",TEXT(DATE(2020,MID(each,6,2),1),"mmmm"))))