0

I used this script to default the cell to today's date - How to make Google Sheets jump to today's row when opened?

function onOpen() {
 var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getActiveSheet();
 var range = sheet.getRange("A:A");
 var values = range.getValues();  
 var day = 24*3600*1000;  
 var today = parseInt((new Date().setHours(0,0,0,0))/day);  
 var ssdate; 
 for (var i=0; i<values.length; i++) {
   try {
     ssdate = values[i][0].getTime()/day;
   }
   catch(e) {
   }
   if (ssdate && Math.floor(ssdate) == today) {
     sheet.setActiveRange(range.offset(i,0,1,1));
     break;
   }    
 }
}

The formula works and it's great. Unfortunately the view still defaults to the top of the sheet but with the selected cell being the today's date way down the spreadsheet.

I have to press an arrow key and the view will change.

Can I somehow alter the default view to the selected cell? As I'm writing this it may be as easy as scripting the down key after running the initial code.

I'm no coder though so the help would be nice.

Thanks!

  • Unable to reproduce the behavior described. Firefox 66. setActiveRange selects the range, but also scrolls to where it's just visible. The only exception was if I already have that cell selected, scroll away, and run the script again. Perhaps give it a starting point with say sheet.setActiveRange(sheet.getRange(1,1)); before your for loop? – Joel Reid Apr 25 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.