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

Loop in the same script code to two specific tabs that opens up the one with the recent edit

我真的不了解脚本,我只是研究了一段时间以获得我需要的特定代码。但是我觉得我自己做不到,我需要帮助。

我尝试了可能适合我需要的不同代码,终于找到了这个 scrip[t code/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;
    }
  }
}

我尝试复制该代码并将(sheet 名称)重命名为我需要具有该功能的 2 个选项卡,尽管它只能使用一个或另一个。我需要它为他们两个工作。此脚本打开到当前日期,我希望它在两个选项卡上都有效,因为这两个选项卡分配了不同的用户。

Here 是我的 sheet 的副本,供您参考。

请大家帮帮我,看代码我实在是看不懂,只是研究。

谢谢

每个文档不能使用多个 onOpen() 触发器。由于代码中的逻辑已经适用于一个 sheet,因此您可以更改获取 sheet 变量的方式,从此:

  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);

为此:

  var sheet = SpreadsheetApp.getActiveSheet();

getActiveSheet() 方法将检索当前正在打开或编辑的 sheet。

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

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;
    }
  }
}

正如 Andres 所说,激活最近编辑的 sheet 需要脚本监视编辑事件。但是,“按日期跳转到单元格”功能不应 运行 来自 onEdit(e) 功能,因为这会导致每次编辑传播sheet 时选择跳转。

请参阅 https://support.google.com/docs/thread/32097728?msgid=33126935 以了解从 onEdit(e) 函数中将 saveLastEditedCellLocationPerSheet_() 函数与 运行 分开的解决方案。这是基本位的副本:

/**
* Saves the location of the edited cell on each sheet,
* and the name of the sheet that was last edited.
*
* @param {Object} e The onEdit() event object.
*/
function saveLastEditedCellLocationPerSheet_(e) {
  if (!e.range || JSON.stringify(e.range) === '{}') {
    return;
  }
  var sheetName = e.range.getSheet().getName();
  var propKeyNames = getPropKeyNames_();
  var propertiesToUpdate = {};
  propertiesToUpdate[propKeyNames.lastEditedSheet] = sheetName;
  propertiesToUpdate[propKeyNames.lastEditedCellOnSheetPrefix + sheetName] = e.range.getA1Notation();
  var propStore = PropertiesService.getUserProperties();
  propStore.setProperties(propertiesToUpdate);
}

/**
* Gets the property key names to use to access the properties.
*
* @return {Object} The property key names to use to access the properties.
*/
function getPropKeyNames_() {
  return {
    lastEditedSheet: 'lastEditedSheet',
    lastEditedCellOnSheetPrefix: 'lastEditedCellOnSheet_',
    lastEditedCellOnSheetRegex: /^lastEditedCellOnSheet_/,
  };
}

干杯--海德