Skip to content
Advertisement

How to automate sheet to activate/move to cell with today’s date

I’m trying to implement this script to find the current date on all viewable sheets (Not Hidden) and move to the active date range (today) on whichever tab it’s on upon open.

I found this thread but it’s now locked, https://productforums.google.com/forum/#!topic/docs/NiPjCPUOx3M

The script they used to achieve this is listed in the code section:

This code doesn’t seem to work for my sheet as my date’s are entered as calendar dates and thus are registered as a numerical value.

Could anyone help me with this?

My Sheet: https://docs.google.com/spreadsheets/d/1W3KMwE2dEqe0dkoggKJ5LRT3qHEgwD71izVKx8gJn08/edit?usp=sharing

The date ranges for each sheet are listed in: C4:I4 C51:I51 C98:I98 C145:I145

function onOpen() {

var sheets, sheet, d, now, today, flag, r;

sheets = SpreadsheetApp.getActive().getSheets();
now = new Date(),
today = new Date(now.getYear(), now.getMonth() + 1, now.getDate(), 0, 0, 0, 0)
    .getTime();
for (var i = 0, len = sheets.length; i < len; i += 1) {
    v = sheets[i].getRange('A:A')
        .getValues()
    for (var j = 0, l = v.length; j < l; j += 1) {
        r = v[j][0];
        if (r && isDate_(r)) {
            d = new Date(r.getYear(), r.getMonth() + 1, r.getDate(), 0, 0, 0, 0)
                .getTime();
            if (d == today) {
                sheets[i].setActiveRange(sheets[i].getRange(j + 1, 6));
                break;
            }
        }
    }
}
}

function isDate_(sDate) {

var tryDate = new Date(sDate);
return (tryDate && !isNaN(tryDate));
}

For the sheet to find the cell listed with today’s date and activate/move to it automatically.

Advertisement

Answer

Finding Today with RangeList

function findToday() {
  var shts=SpreadsheetApp.getActive().getSheets();
  var now=new Date();
  var today=new Date(now.getYear(), now.getMonth(), now.getDate()).valueOf();
  for(var s=0;s<shts.length;s++) {
    var sh=shts[s];
    if(sh.getLastRow() && sh.getLastColumn()) { 
      //var rgA=sh.getRangeList(['C4:I4','C6:I6','C8:I8','C10:I10']).getRanges();//test ranges
      var rgA=sh.getRangeList(['C4:I4','C51:I51','C98:I98','C145:I145']).getRanges();
      for(var r=0;r<rgA.length;r++){
        var rg=rgA[r];
        var vA=rg.getValues();
        for(var i=0;i<vA.length;i++) {
          for(var j=0;j<vA[i].length;j++){
            if(vA[i][j]){
              var row=rg.getRow();
              var col=rg.getColumn();
              var t=new Date(vA[i][j]);
              var d=new Date(t.getYear(), t.getMonth(), t.getDate()).valueOf();
              if (d==today) {
                var name=sh.getName();
                sh.setActiveRange(sh.getRange(sh.getLastRow()+1,1));
                SpreadsheetApp.flush();
                sh.setActiveRange(sh.getRange(row + i, col + j));
                return;
              }
            }
          }
        }
      }
    }
  }
}

In your onOpen function which may have to be installable, just call findToday();

function onOpen() {
  findToday();
} 
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement