[英]Scroll through the spreadsheet to today's date using google app scripts
My google spreadsheet consists of columns:我的谷歌电子表格由列组成:
I need a script that when you open the document will automatically scroll through the table to today's date.我需要一个脚本,当您打开文档时,它会自动在表格中滚动到今天的日期。
Go to Today: Go 到今天:
function gotoTodaysDate() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheets()[0];//most left sheet
const shsr=2;//data start row
const shsc=1;//date column
const vs=sh.getRange(shsr,shsc,sh.getLastRow()-shsr+1,1).getValues();
const dates=vs.map(function(r){return r[0];});//flattened
const td=Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "dd.MM.yyyy");//current date string
const idx=dates.indexOf(td);
sh.getRange(idx+shsr,1).activate();
}
Installable onOpen Trigger:可安装的 onOpen 触发器:
function createOnOpenTrigger() {
const ss=SpreadsheetApp.getActive();
if(!isTrigger('gotoTodaysDate')) {
ScriptApp.newTrigger('gotoTodaysDate').forSpreadsheet(ss.getId()).onOpen().create();
}
}
Helper Function: Keeps you from creating more that one trigger for a given function助手 Function:防止您为给定的 function 创建多个触发器
function isTrigger(funcName){
var r=false;
if(funcName){
var allTriggers=ScriptApp.getProjectTriggers();
for (let i=0;i<allTriggers.length;i++){
if(funcName==allTriggers[i].getHandlerFunction()){
r=true;
break;
}
}
}
return r;
}
You may not need to have a script do that.您可能不需要脚本来执行此操作。
If your dates are in reverse order (ie, oldest up top to newest at the bottom), and you're just trying to get to the most recent date at the bottom, you can click in cell A1 and hit the Ctrl key along with the down-arrow key (ie, Ctrl-▼) on your keyboard, and you'll be taken there.如果您的日期是相反的顺序(即,从上到下最旧的日期),并且您只是想在底部找到最近的日期,您可以单击单元格 A1 并按 Ctrl 键以及键盘上的向下箭头键(即 Ctrl-▼),您将被带到那里。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.