Typescript 将同一脚本代码循环到两个特定选项卡,打开最近编辑的选项卡

Typescript 将同一脚本代码循环到两个特定选项卡,打开最近编辑的选项卡,typescript,google-apps-script,google-sheets,google-sheets-formula,google-sheets-query,Typescript,Google Apps Script,Google Sheets,Google Sheets Formula,Google Sheets Query,我对脚本并没有真正的了解,我只是花了一段时间研究了一些我需要的特定代码。但我想我自己做不到,我需要帮助 我尝试了不同的代码,可能适合我的需要,最终找到了这张纸条[t代码/s: function onOpen() { // version 1.1, written by --Hyde 5 March 2020 // - add sheetName, dateColumn // - use value.getTime to check that dateColumn cont

我对脚本并没有真正的了解,我只是花了一段时间研究了一些我需要的特定代码。但我想我自己做不到,我需要帮助

我尝试了不同的代码,可能适合我的需要,最终找到了这张纸条[t代码/s:

    function onOpen() {
  // version 1.1, written by --Hyde 5 March 2020
  //  - add sheetName, dateColumn
  //  - use value.getTime to check that dateColumn contains dates
  //  - find today instead of tomorrow
  //  - use descriptive variable names
  //  - see https://support.google.com/docs/thread/32097728?msgid=32396362
  // version 1.0, written by AD:AM 15 May 2019
  //  - initial version
  //  - see https://support.google.com/docs/thread/6074864?msgid=6156095

  ////////////////////////////////
  // [START modifiable parameters]
  var sheetName = 'sheetname';
  var dateColumn = 2; // A = 1, B = 2, etc.
  // [END modifiable parameters]
  ////////////////////////////////

  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
  var values = sheet.getDataRange().getValues();
  var now = new Date();
  var today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
  for (var i = 0, length = values.length; i < length; i++) {
    var value = values[i][dateColumn - 1];
    if (value.getTime && value.getTime() >= today) {
      sheet.getRange(i + 1, dateColumn).activate();
      break;
    }
  }
}
函数onOpen(){ //版本1.1,作者——海德,2020年3月5日 //-添加sheetName、dateColumn //-使用value.getTime检查dateColumn是否包含日期 //-找到今天而不是明天 //-使用描述性变量名 //-见https://support.google.com/docs/thread/32097728?msgid=32396362 //版本1.0,由AD编写:2019年5月15日上午 //-初始版本 //-见https://support.google.com/docs/thread/6074864?msgid=6156095 //////////////////////////////// //[启动可修改参数] var sheetName='sheetName'; var dateColumn=2;//A=1,B=2,等等。 //[结束可修改的参数] //////////////////////////////// var sheet=SpreadsheetApp.getActive().getSheetByName(sheetName); var values=sheet.getDataRange().getValues(); var now=新日期(); var today=新日期(now.getFullYear()、now.getMonth()、now.getDate()).getTime(); 对于(变量i=0,长度=values.length;i=今天){ getRange(i+1,dateColumn).activate(); 打破 } } } 我尝试复制该代码并将(工作表名称)重命名为我需要的两个选项卡,以实现该功能,尽管它只能在其中一个选项卡上工作。我需要它在两个选项卡上都工作。此脚本打开到当前日期,我希望它能在两个选项卡上工作,因为这两个选项卡具有不同的用户分配

这是一份我的工作表供你参考

请在这一点上帮助我,因为我真的不太懂如何阅读代码,我只是研究


谢谢

每个文档不能使用多个
onOpen()
触发器。由于代码中的逻辑已经用于一个工作表,您可以通过以下方式更改获取
工作表
变量的方式:

  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
为此:

  var sheet = SpreadsheetApp.getActiveSheet();
该方法将检索当前正在打开或编辑的工作表

在编辑工作表时,除了使用
onOpen()
触发器外,还需要使用触发器来激活单元格。我测试了此代码并成功运行:

function onOpen() {
  activateLastDateCell();
}

function onEdit() {
  activateLastDateCell();
}

function activateLastDateCell() {
  // version 1.1, written by --Hyde 5 March 2020
  //  - add sheetName, dateColumn
  //  - use value.getTime to check that dateColumn contains dates
  //  - find today instead of tomorrow
  //  - use descriptive variable names
  //  - see https://support.google.com/docs/thread/32097728?msgid=32396362
  // version 1.0, written by AD:AM 15 May 2019
  //  - initial version
  //  - see https://support.google.com/docs/thread/6074864?msgid=6156095

  ////////////////////////////////
  // [START modifiable parameters]
  //var sheetName = '(Ian) Hulog - Hulog 2020';
  var dateColumn = 2; // A = 1, B = 2, etc.
  // [END modifiable parameters]
  ////////////////////////////////

  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getDataRange().getValues();
  var now = new Date();
  var today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
  for (var i = 0, length = values.length; i < length; i++) {
    var value = values[i][dateColumn - 1];
    if (value.getTime && value.getTime() >= today) {
      sheet.getRange(i + 1, dateColumn).activate();
      break;
    }
  }
}
函数onOpen(){ 激活弹力细胞(); } 函数onEdit(){ 激活弹力细胞(); } 功能激活的ElastDateCell(){ //版本1.1,作者——海德,2020年3月5日 //-添加sheetName、dateColumn //-使用value.getTime检查dateColumn是否包含日期 //-找到今天而不是明天 //-使用描述性变量名 //-见https://support.google.com/docs/thread/32097728?msgid=32396362 //版本1.0,由AD编写:2019年5月15日上午 //-初始版本 //-见https://support.google.com/docs/thread/6074864?msgid=6156095 //////////////////////////////// //[启动可修改参数] //var sheetName='(Ian)Hulog-Hulog 2020'; var dateColumn=2;//A=1,B=2,等等。 //[结束可修改的参数] //////////////////////////////// var sheet=SpreadsheetApp.getActiveSheet(); var values=sheet.getDataRange().getValues(); var now=新日期(); var today=新日期(now.getFullYear()、now.getMonth()、now.getDate()).getTime(); 对于(变量i=0,长度=values.length;i=今天){ getRange(i+1,dateColumn).activate(); 打破 } } }
每个文档不能使用多个
onOpen()
触发器。由于代码中的逻辑已经适用于一个工作表,您可以通过以下方式更改获取
工作表
变量的方式:

  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
为此:

  var sheet = SpreadsheetApp.getActiveSheet();
该方法将检索当前正在打开或编辑的工作表

在编辑工作表时,除了使用
onOpen()
触发器外,还需要使用触发器来激活单元格。我测试了此代码并成功运行:

function onOpen() {
  activateLastDateCell();
}

function onEdit() {
  activateLastDateCell();
}

function activateLastDateCell() {
  // version 1.1, written by --Hyde 5 March 2020
  //  - add sheetName, dateColumn
  //  - use value.getTime to check that dateColumn contains dates
  //  - find today instead of tomorrow
  //  - use descriptive variable names
  //  - see https://support.google.com/docs/thread/32097728?msgid=32396362
  // version 1.0, written by AD:AM 15 May 2019
  //  - initial version
  //  - see https://support.google.com/docs/thread/6074864?msgid=6156095

  ////////////////////////////////
  // [START modifiable parameters]
  //var sheetName = '(Ian) Hulog - Hulog 2020';
  var dateColumn = 2; // A = 1, B = 2, etc.
  // [END modifiable parameters]
  ////////////////////////////////

  var sheet = SpreadsheetApp.getActiveSheet();
  var values = sheet.getDataRange().getValues();
  var now = new Date();
  var today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
  for (var i = 0, length = values.length; i < length; i++) {
    var value = values[i][dateColumn - 1];
    if (value.getTime && value.getTime() >= today) {
      sheet.getRange(i + 1, dateColumn).activate();
      break;
    }
  }
}
函数onOpen(){ 激活弹力细胞(); } 函数onEdit(){ 激活弹力细胞(); } 功能激活的ElastDateCell(){ //版本1.1,作者——海德,2020年3月5日 //-添加sheetName、dateColumn //-使用value.getTime检查dateColumn是否包含日期 //-找到今天而不是明天 //-使用描述性变量名 //-见https://support.google.com/docs/thread/32097728?msgid=32396362 //版本1.0,由AD编写:2019年5月15日上午 //-初始版本 //-见https://support.google.com/docs/thread/6074864?msgid=6156095 //////////////////////////////// //[启动可修改参数] //var sheetName='(Ian)Hulog-Hulog 2020'; var dateColumn=2;//A=1,B=2,等等。 //[结束可修改的参数] //////////////////////////////// var sheet=SpreadsheetApp.getActiveSheet(); var values=sheet.getDataRange().getValues(); var now=新日期(); var today=新日期(now.getFullYear()、now.getMonth()、now.getDate()).getTime(); 对于(变量i=0,长度=values.length;i=今天){ getRange(i+1,dateColumn).activate(); 打破 } } }
正如Andres所指出的,激活最近编辑的工作表需要脚本监控编辑事件。但是,不应从OneEdit(e)函数运行“按日期跳转到单元格”函数,因为这会导致每次编辑电子表格时选择跳转

请参阅以获取一个解决方案,该解决方案有一个单独的
saveLastEditedCellLocationPerSheet(()
函数,可从OneEdit(e)函数运行