如何从单元格中提取链接,现在在单元格中的链接不再显示为超链接?

10

"插入链接"现在不再生成 =HYPERLINK(’’,’’)公式。

以前,如果您将一个带有值“X”的单元格链接起来,它会被转换为公式=HYPERLINK(*link*,’X’)

两天前,“插入链接”发生了变化。

现在单元格的内容保持不变,只是有下划线。

使用脚本,我该如何从单元格中提取链接,既然它的值和公式都不包含此信息?

我搜索了文档,但我能找到的唯一与链接相关的方法是setShowHyperlink(showHyperlink)

1个回答

13
我可以确认您的情况。在这种情况下,似乎可以从RichTextValue对象中检索超链接。也就是说,我认为规范已经更改,使得使用RichTextValue将超链接给予文本。
因此,作为示例情况,它假设如下:
- 在单元格"A1"中放置了一个文本X。 - 此单元格手动链接到URL,例如https://www.google.com
在这种情况下,该单元格没有=HYPERLINK("https://www.google.com","X")。从这种情况检索URL的示例脚本如下:
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var url = sheet.getRange("A1").getRichTextValue().getLinkUrl();
console.log(url);
  • 在这种情况下,URL与URL中的整个文本相关联。因此可以使用上述脚本。

注意:

  • In the current stage, the multiple hyperlinks can be added to the texts in one cell. For example, when 2 URLs are put to the text in a cell, you can use the following sample script. In this sample, a text of url1, url2 is put to a cell "A1", and url1 and url2 are linked with each link.

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    var RichTextValue = SpreadsheetApp.newRichTextValue()
      .setText("url1, url2")
      .setLinkUrl(0, 4, "https://url1/")
      .setLinkUrl(6, 10, "https://url2/")
      .build();
    sheet.getRange("A1").setRichTextValue(RichTextValue);
    
  • When the multiple URLs are retrieved from the text in a cell, you can use the following sample script.

    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
    var range = sheet.getRange("A1");
    var RichTextValue = range.getRichTextValue().getRuns();
    var res = RichTextValue.reduce((ar, e) => {
      var url = e.getLinkUrl();
      if (url) ar.push(url);
      return ar;
    }, []);
    console.log(res);
    

参考资料:

更新于2020年6月13日:

截至2020年6月12日的更新,官方文档中新增了获取链接网址()设置链接网址(linkUrl)的文档。


1
你在哪里找到了 getLinkUrl() 函数? - TheMaster
7
@TheMaster 我之前使用脚本编辑器的自动补全功能来调用 getLinkUrl() 方法。但是现在,我发现当我查找 getLinkUrl() 方法的官方文档时,发现它没有被列出来。这让我很惊讶。似乎官方文档还没有更新。但我相信官方文档会在不久的将来更新。 - Tanaike
1
@TheMaster 我可以确认,根据2020年6月12日的更新,getLinkUrl()setLinkUrl(linkUrl)的文档已经添加到官方文档中。 - Tanaike
@Tanaike var url = e.getLinkUrl(); 如果有URL,ar.push(url);我们能够获取URL,但是否有办法找出应用链接的文本以及这些URL? - Sanat
@Sanat 你认为 e 是什么? - TheMaster
这是一个子字符串,谢谢,我已经得到了文本 :) - Sanat

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接