【问题标题】:Google sheets move focus from search box with keyboard shortcut谷歌表格使用键盘快捷键将焦点从搜索框移开
【发布时间】:2021-05-16 08:02:35
【问题描述】:

如何使用键盘快捷键将焦点从搜索框移回当前(选定)单元格?预定义的“移动焦点”快捷方式似乎都不起作用。 CTRL+ALT+SHIFT+M 将焦点移出搜索框,但谁知道在哪里...

执行此操作的宏因“找不到范围”而失败...

spreadsheet.getRange(spreadsheet.getActiveCell()).activate();

TIA。

【问题讨论】:

  • 如果您使用内部电子表格工具“编辑 > 查找和替换”Ctrl+Shift+H,您可以点击Esc 关闭此工具(您不能保持打开状态并同时激活一个单元格,因为据我所知)。如果您使用标准浏览器搜索Ctrl+F,则取决于浏览器。

标签: google-apps-script google-sheets keyboard-shortcuts


【解决方案1】:

您可以按 Esc 关闭搜索框并将最后找到的单元格设为活动单元格。这适用于快速查找 Control+FEdit > Find and replace Control+Shift+H

要实现自己的搜索功能,您可以使用onEdit(e) simple trigger,如下所示。

该函数使用您选择的单元格,例如A1,作为搜索框。选择查看>冻结以确保搜索框始终可见。然后在搜索框中输入搜索字符串并按 Tab 以激活活动工作表中的匹配单元格。您也可以使用regular expression 作为搜索字符串。

/**
* Simple trigger that runs each time the user hand edits the spreadsheet.
*
* @param {Object} e The onEdit() event object.
*/
function onEdit(e) {
  if (!e) {
    throw new Error('Please do not run the script in the script editor window. It runs automatically when you hand edit the spreadsheet.');
  }
  quickFind_(e);
}

/**
* Finds cells that match the regular expression entered in a magic cell.
*
* @param {Object} e The onEdit() event object.
*/
function quickFind_(e) {
  const sheets = /^(Sheet1|Sheet2|Sheet3)$/i; // use /./i to make the function work in all sheets
  const magicCell = 'A1';
  const sheet = e.range.getSheet();
  if (!e.value
    || e.range.getA1Notation() !== magicCell
    || !sheet.getName().match(sheets)) {
    return;
  }
  let searchFor;
  try {
    searchFor = new RegExp(e.value, 'i');
  } catch (error) {
    searchFor = e.value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
  }
  const magicCellR1C1 = 'R' + e.range.rowStart + 'C' + e.range.columnStart;
  const data = sheet.getDataRange().getDisplayValues();
  const matches = [];
  data
    .forEach((row, rowIndex) => row
      .forEach((value, columnIndex) => {
        if (value.match(searchFor)) {
          const cellR1C1 = 'R' + (rowIndex + 1) + 'C' + (columnIndex + 1);
          if (cellR1C1 !== magicCellR1C1) {
            matches.push(cellR1C1);
          }
        }
      }));
  if (matches.length) {
    sheet.getRangeList(matches).activate();
  }
}

【讨论】:

  • 有趣的是 Esc 只是从搜索框中移除焦点,但不会将其返回到活动单元格。但是,我意识到 Esc Esc 会将焦点返回到活动单元格。但我真正想要的是关注搜索框刚刚找到的单元格(这可能不是最后一个活动单元格),但保持搜索框可见,以便将来搜索相同的字符串。 (抱歉,不清楚如何在 Markdown 中将 'Esc' 格式化为 kbd 字符;另外,感谢@Yuri)
  • 我认为 Google 表格的内置功能无法做到这一点。编辑答案以包含 onEdit(e) 函数来执行您的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
相关资源
最近更新 更多