Go to the cell that contains today's date in a Sheet with Google Apps Script

I need to go to specific cell that contains today's date, but .getRangeByName() or .getA1Notation() doesn't work. This is my script:

item1: function() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var date = Utilities.formatDate(new Date(), 'GMT-5', 'dd/MM/yyyy');
  var cell = sheet.getRangeByName(date).getA1Notation();
  sheet.setActiveRange(cell);
},

Upvotes: 0

Views: 2063

Answers (1)

Tanaike
Tanaike

Reputation: 201513

I believe your goal as follows.

  • You want to select the cells by searching the value of var date = Utilities.formatDate(new Date(), 'GMT-5', 'dd/MM/yyyy'); in the active sheet using Google Apps Script.

For this, how about this answer?

Modification point:

  • Unfortunately, getRangeByName cannot be used for searching values. In this case, I would like to propose to use TextFinder.

Modified script:

function myFunction() {
  var ss = SpreadsheetApp.getActive();
  var sheet = SpreadsheetApp.getActiveSheet();
  var date = Utilities.formatDate(new Date(), 'GMT-5', 'dd/MM/yyyy');

  // I modified below script.
  var ranges = sheet.createTextFinder(date).findAll().map(r => r.getA1Notation());
  sheet.getRangeList(ranges).activate();
}

References:

Upvotes: 1

Related Questions