Google 表格在过滤后会打开多个超链接! openallLinks()

问题描述 投票:0回答:1

我有一张谷歌表格,上面有多个超链接。 我可以选择多个链接,然后单击“打开选定的链接”,所有链接都会在 Chrome 中的单独选项卡中打开。 这很有效,直到我添加了一个过滤器,以便我可以看到具有类似主题的超链接,例如数学中的“位值”。

打开过滤器后,打开的超链接过多,例如,如果过滤器显示第 1、4、6 和 10 行的超链接(4 个链接),则打开 10 个链接 - 1>10(含)!!

有没有办法调整脚本来解决这个问题? 我在这里有一个工作表链接,并过滤了行以显示“位置值”

https://docs.google.com/spreadsheets/d/1P5MS3mcAJRHURW5eWspl4uxiO8XyLPyT17GCNumry5I/edit?usp=sharing

任何帮助或想法都会很棒! 我是一个剧本知识很少的老师! 亲切的问候, 马特

尝试以多种方式对工作表进行排序,还尝试单独选择链接

这是当前脚本的副本:

function openAllLinks() {
  // Get the selected range
  const selection = SpreadsheetApp.getActiveSheet().getActiveRange();

  // Filter for cells containing hyperlinks
  const withLinks = selection.getRichTextValues()
    .flatMap(row => row.flatMap(cellRichTextValue => {
      const links = cellRichTextValue.getRuns().filter(run => run.getLinkUrl());
      return links.length > 0 ? links[0].getLinkUrl() : [];
    }));

  if (withLinks.length == 0) {
    Browser.msgBox("No URLs were found.");
    return;
  }
  const opens = withLinks.map(url => `window.open('${url}', '_blank');`).join("");
  const html = HtmlService.createHtmlOutput(`<html><script>${opens};google.script.host.close();</script></html>`);
  SpreadsheetApp.getUi().showModalDialog(html, "sample");
  // ---

  // Show a confirmation message
  SpreadsheetApp.getUi().alert('Links opened successfully!');
}function myFunction() {
  
}

google-sheets google-apps-script filter hyperlink
1个回答
0
投票

建议的解决方案

您可以通过更改脚本来实现您想要的效果:

  const selection = SpreadsheetApp.getActiveSheet().getActiveRange();

  const withLinks = selection.getRichTextValues()
    .flatMap(row => row.flatMap(cellRichTextValue => {
      const links = cellRichTextValue.getRuns().filter(run => run.getLinkUrl());
      return links.length > 0 ? links[0].getLinkUrl() : [];
    }));

致:

  const ss = SpreadsheetApp.getActiveSheet();
  const selection = ss.getActiveRange();

  const withLinks = selection.getRichTextValues()
    .flatMap((row, i) => row.filter(_ => !ss.isRowHiddenByFilter(selection.getRow() + i))
      .flatMap(cellRichTextValue => {
        const links = cellRichTextValue.getRuns().filter(run => run.getLinkUrl());
        return links.length > 0 ? links[0].getLinkUrl() : [];
      }));

完整的代码将变为:

function openAllLinks() {
  const ss = SpreadsheetApp.getActiveSheet();
  const selection = ss.getActiveRange();

  const withLinks = selection.getRichTextValues()
    .flatMap((row, i) => row.filter(_ => !ss.isRowHiddenByFilter(selection.getRow() + i))
      .flatMap(cellRichTextValue => {
        const links = cellRichTextValue.getRuns().filter(run => run.getLinkUrl());
        return links.length > 0 ? links[0].getLinkUrl() : [];
      }));

  if (withLinks.length == 0) {
    Browser.msgBox("No URLs were found.");
    return;
  }
  const opens = withLinks.map(url => `window.open('${url}', '_blank');`).join("");
  const html = HtmlService.createHtmlOutput(`<html><script>${opens};google.script.host.close();</script></html>`);
  SpreadsheetApp.getUi().showModalDialog(html, "sample");

  SpreadsheetApp.getUi().alert('Links opened successfully!');
}
© www.soinside.com 2019 - 2024. All rights reserved.