I'm trying to set a Google Sheet document open to the cell in row 2 with the current date using Script Editor. The dates are arranged chronologically, and daily, from A2:IK2. They're formatted mm/dd.
I found one answer on this website for a similar question, however their dates were listed in a single column whereas my dates are in a single row. The following code was posted by Marshmallow here: https://webapps.stackexchange.com/questions/78927/how-to-make-google-sheet-jump-to-todays-row-when-opened ).
Marshmallow's answer:
function onOpen() {
var menu = [{name: "Jump to today's date", functionName: "jumpToDate"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
jumpToDate();
}
function jumpToDate() {
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;
}
}
}
I modified it to:
function onOpen() {
var menu = [{name: "Jump to today's date", functionName: "jumpToDate"}];
SpreadsheetApp.getActiveSpreadsheet().addMenu("Custom", menu);
jumpToDate();
}
function jumpToDate() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("A2:IK2"); //given that it is in row 2
var values = range.getValues();
var day = 24*3600*1000;
var today = ((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(0,i,1,1)); //to offset columns not rows
break;
}
}
}
I thought that I had accounted for the transposition from rows to columns (range "A:A" to "A2:IK2" and changing the offset where indicated; however, I am not seeing the code have any effect. Let alone opening to today's date.
What am I missing here? Any suggestions?
Two more places where transposition requires changes are: values[0][i]
instead of values[i][0]
, and values[0].length
instead of values.length
.
To address RobG's remark, I also changed the comparison logic, replacing division and flooring with straightforward inequalities: the current time must be between the beginning and end of the date stated in the spreadsheet.
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange("2:2"); // get all 2nd row
var values = range.getValues();
var day = 24*3600*1000;
var now = Date.now();
var ssdate;
for (var i = 0; i < values[0].length; i++) {
try {
ssdate = values[0][i].getTime();
}
catch(e) {
}
if (ssdate && ssdate <= now && now < ssdate + day) {
sheet.setActiveRange(range.offset(0,i,1,1));
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.