【问题标题】:Google sheets. Go to range depending on what a user has typed in a search box谷歌表。根据用户在搜索框中输入的内容转到范围
【发布时间】:2021-11-12 21:28:03
【问题描述】:

我知道有一个选项可以通过键入 go to range 或按 CTRL + F 转到帮助中的特定行,但恐怕我的用户赢了不要打扰 go to 或 CTRL+F 搜索。是否有脚本或函数可以根据用户在用作搜索框的单元格中键入的内容来执行与“转到特定行”(即跳转列表)相同的操作?

hlookup、vlookup、filter 或 search 都不行,因为表格向右移动。另外,用户需要在找到该行时输入数据。上述命令只能返回值,不允许数据输入。

就脚本而言,我什至找不到从哪里开始的提示。仅在此google sheet 中使用过滤器。用户基本上在 C1 中输入搜索名称或姓氏,理想情况下应该跳转到一行。由于这是一个演示,因此只有 3 个条目,但原始条目会充满数据。

This thread 可以解决问题。现在尝试使用下面的脚本突出显示它。问题是,当另一个搜索条件在搜索框中时,它会在查找时突出显示并留下颜色。我试图删除突出显示。有什么想法吗?

if (matches.length) {
    sheet.getRangeList(matches).activate();
    sheet.getRangeList(matches).setBackground("yellow");
  }
   else if(matches != magicCell) {
    sheet.clearConditionalFormatRules();
    sheet.getRangeList(matches).setBackground("white");
   }

【问题讨论】:

  • 显示你尝试过的和没用的
  • 脚本方面什么都没有。因为我什至找不到关于从哪里开始的提示。我只玩了过滤器命令,我在最初的帖子中添加了它。
  • 试试quickFind_ 脚本。它似乎完全符合您的要求。
  • 谢谢。这行得通。现在尝试使用 if (matches.length) { sheet.getRangeList(matches).activate(); 突出显示它sheet.getRangeList(matches).setBackground("yellow"); } else if(matches != magicCell) { sheet.clearConditionalFormatRules(); sheet.getRangeList(matches).setBackground("white");问题是,当另一个搜索条件在搜索框中时,它会在查找时突出显示并留下颜色。我试图删除突出显示。有什么想法吗?
  • @LiepājasLiedagavidusskola 你能写下这个问题的答案,这样它就不会没有答案了吗?

标签: google-apps-script google-sheets search goto


【解决方案1】:

带有突出显示的搜索框。 将代码中下面的 C1 更改为您的搜索框单元格。将脚本设置为在触发器中运行 OnEdit。将偏移量更改为需要排除的任何行数。修复标题以方便使用。

一旦找到,搜索框中的所有实例都设置为背景:黄色(更改为您需要的任何颜色)。清除搜索框后,黄色突出显示消失。请注意,执行以下脚本需要 1 到 4 秒。它比CTRL + F慢,但它比CTRL + F更好地突出显示。它允许不熟悉CTRL + F的用户轻松搜索。另请注意,如果列表包含您的搜索查询的多个实例,它将落在最后一个查找中。您必须手动滚动才能查看所有突出显示的单元格。因此,它的功能不如 CTRL + F。

附言。如果有任何 Google 员工阅读此内容。我希望 Google 包含一个新的搜索框功能。菜单 -> 插入 -> 单元格中或单元格上方的搜索框。

过滤不适合我的情况,因为我的用户具有基本的表知识水平。如果他们设置了过滤器,他们可能不知道如何将其关闭。

* 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 = /./i; // use /./i to make the function work in all sheets
  const magicCell = 'C1';
  const sheet = e.range.getSheet();
   if (e.range.getA1Notation() !== magicCell
    || !sheet.getName().match(sheets)) {
    return;
  }
  if (!e.value || !e.value.replace(/ +/g, '')) { clear_backgrounds(); 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) {
    clear_backgrounds();
    sheet.getRangeList(matches).activate();
    sheet.getRangeList(matches).setBackground("yellow");
  }
  
 }

function clear_backgrounds() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getDataRange().offset(2,0); // offset to keep color of first 2 rows
  var bg = range.getValues();
  for (row in bg) for (col in bg[row]) bg[row][col] = null;
  range.setBackgrounds(bg);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2018-11-02
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    相关资源
    最近更新 更多