简体   繁体   English

使用谷歌应用脚本在电子表格中滚动到今天的日期

[英]Scroll through the spreadsheet to today's date using google app scripts

My google spreadsheet consists of columns:我的谷歌电子表格由列组成:

  • A: Date一个约会
    1. 16.07.2020 16.07.2020
    2. 17.07.2020 17.07.2020
    3. 18.07.2020 18.07.2020
  • B, C, D: Other information B、C、D:其他信息

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.

相关问题 使用带有完整日期名称的谷歌应用脚本自动滚动电子表格以显示“今天的日期” - Auto Scroll through the spreadsheet to display "today's date" using google app scripts with the full date name 如何在包含今天日期的单行中打开 Google Sheets 电子表格 - How to open Google Sheets spreadsheet to cell in a single row that contains today's date 在今天的日期应用脚本之前删除电子表格中的日期 - delete date in spreadsheet prior today's date apps script JSON日期是今天的日期? - JSON Date coming through as Today's date? 在Google脚本中使用JavaScript将信息传输到电子表格,但电子表格显示未定义 - Using JavaScript in Google Scripts to transfer information to spreadsheet, but spreadsheet shows undefined Google App Script/Javascript - 无法使用 Google App Script 循环到 Google 电子表格中的列末尾 - Google App Script/Javascript - Not able to loop through up to the end of Column in Google Spreadsheet using Google App Script Google脚本会根据日期将范围从一个电子表格复制到另一个电子表格 - Google scripts copy range from one spreadsheet to another based on date Google电子表格脚本 - Google-Spreadsheet Scripts Google 应用程序脚本 - 遍历电子表格中的行 - Google app script - looping through the rows in a spreadsheet Google App Scripts 在电子表格中查找文本并返回位置索引 - Google App Scripts find text in spreadsheet and return location index
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM