How to dynamically update reference cell in an if loop in Google Apps Script setFormula function - Stack Overflow

I'm trying to change the reference cell in the DATE function in an if loop in Apps Script. The cel

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

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
Share Improve this question edited Mar 6 at 16:06 rpgbear95 asked Mar 6 at 15:49 rpgbear95rpgbear95 354 bronze badges 0
Add a comment  | 

2 Answers 2

Reset to default 2

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"))))

Dynamically Convert Dates to Month Names

Here is an alternative way using Google Apps Script to dynamically retrieve the month name from date entries and set the values in a specified range of a Google Sheet.

I slightly modified your code and instead of setting up a formula, this code will directly input the values.

Modified Code:

function updateMonthColumn() {
  var ss = SpreadsheetApp.getActive().getSheetByName("Prep");
  var data = ss.getRange("I2:I").getValues();

  for (var i = 0; i < data.length; i++) {
    if (data[i][0].length !== 0) { 
      var dateString = data[i][0]; // Get the date string from column I
      var date = new Date(dateString);
      var monthName = date.toLocaleString('default', { month: 'long' }); // Get the month name from Date 

      ss.getRange(i + 2, 4).setValue(monthName);  // Set the month name in column D
    }
  }
}

You can also change 'long' to 'short' if you prefer the month name to be shorter.

Sample Output:

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

References:

  • Date.prototype.toLocaleString()

  • new Date()

  • setValues()

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744964747a4603608.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信