google-apps-script - Google表格,用于在一个单元格中设置多个超链接的脚本

标签 google-apps-script google-sheets hyperlink google-sheets-api

我的目标是创建一个包含来自 Google 脚本的多个超链接的电子表格单元格。

我可以创建一个带有多个链接的单元格,选择部分单元格文本,然后使用 Ctrl + K 或菜单 insert link。我的单元格以 google 和 yahoo 为例,有两个超链接,每个搜索引擎一个。

我可以用两种不同的方式阅读这个单元格中的超链接:

 var range = SpreadsheetApp.getActiveSheet().getRange('A1');
 var richText = range.getRichTextValue();
 var allRuns = richText.getRuns();
 var firstLink = allRuns[0].getLinkUrl();
 var secondLink = allRuns[2].getLinkUrl();
 var gLink = richText.getLinkUrl(0, 5);
 var aLink = richText.getLinkUrl(11, 15);

但是没有可用的setLinkUrl方法。我在 Google sheets API 中找不到任何内容要么。

我复制了这个单元格并在 Linux 中使用 xclip 命令

xclip -selection clipboard -o  -t text/html

结果是

<meta http-equiv="content-type" content="text/html; charset=utf-8">                                                                                                                                                                                                  
<style type="text/css">                                                                                                                                                                                                                                              
<!--                                                                                                                                                                                                                                                                 
td {                                                                                                                                                                                                                                                                 
    border: 1px solid #ccc;                                                                                                                                                                                                                                          
}                                                                                                                                                                                                                                                                    

br {                                                                                                                                                                                                                                                                 
    mso-data-placement: same-cell;                                                                                                                                                                                                                                   
}                                                                                                                                                                                                                                                                    
-->                                                                                                                                                                                                                                                                  
</style>                                                                                                                                                                                                                                                             
<span style="font-size: 10pt; font-family: Arial; font-style: normal;"                                                                                                                                                                                               
    data-sheets-value="{&quot;1&quot;:2,&quot;2&quot;:&quot;google and yahoo&quot;}"                                                                                                                                                                                 
    data-sheets-userformat="{&quot;2&quot;:1049089,&quot;3&quot;:{&quot;1&quot;:0},&quot;12&quot;:0,&quot;23&quot;:1}"                                                                                                                                               
    data-sheets-textstyleruns="{&quot;1&quot;:0,&quot;2&quot;:{&quot;2&quot;:{&quot;1&quot;:2,&quot;2&quot;:1136076},&quot;9&quot;:1}}{&quot;1&quot;:6}{&quot;1&quot;:11,&quot;2&quot;:{&quot;2&quot;:{&quot;1&quot;:2,&quot;2&quot;:1136076},&quot;9&quot;:1}}"   
    data-sheets-hyperlinkruns="{&quot;1&quot;:0,&quot;2&quot;:&quot;https://www.google.com/&quot;}{&quot;1&quot;:6}{&quot;1&quot;:11,&quot;2&quot;:&quot;https://www.yahoo.com/&quot;}{&quot;1&quot;:16}"><span                                                   
    style="font-size: 10pt; font-family: Arial; font-style: normal; text-decoration: underline; -webkit-text-decoration-skip: none; text-decoration-skip-ink: none; color: #1155cc;"><a                                                                              
        class="in-cell-link" target="_blank" href="https://www.google.com/">google</a></span><span                                                                                                                                                                   
    style="font-size: 10pt; font-family: Arial; font-style: normal;">                                                                                                                                                                                                
        and </span><span                                                                                                                                                                                                                                             
    style="font-size: 10pt; font-family: Arial; font-style: normal; text-decoration: underline; -webkit-text-decoration-skip: none; text-decoration-skip-ink: none; color: #1155cc;"><a                                                                              
        class="in-cell-link" target="_blank" href="https://www.yahoo.com/">yahoo</a></span></span>                                                                                                                                                                   

我不确定这是否意味着超链接有自己的运行,独立的文本样式运行?感谢任何指点。

最佳答案

function addMultipleUrlsToCell() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet2');
  sh.getRange('A1').setValue('');
  var RichTextValue=SpreadsheetApp.newRichTextValue()
  .setText("Google,GASReference,SO")
  .setLinkUrl(0,6,"https://google.com/")
  .setLinkUrl(7,19,"https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet-app/")
  .setLinkUrl(20,22,"https://stackoverflow.com")
  .build();
  sh.getRange("A1").setRichTextValue(RichTextValue);
}

function retrieveMultipleUrls() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getSheetByName('Sheet2');
  const rg=sh.getRange(1,1);
  var rtv=rg.getRichTextValue().getRuns();
  var res=rtv.reduce(function(ar,e){
    var url=e.getLinkUrl();
    if(url)ar.push(url);
    return ar;
  },[]);
  console.log(res);
}

Reference

关于google-apps-script - Google表格,用于在一个单元格中设置多个超链接的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62075453/

相关文章:

D3.js 在节点选择上突出显示链接

php - 如何制作一个用css访问的<li>链接

google-apps-script - 如何从一个 Google Apps 脚本调用另一个函数?

filter - 如何过滤非空白行?

android - 在 Android 中从 Web View 编辑 Google 文档

google-apps-script - 使用 Google App 脚本删除一定数量的过滤器 View

xml - 谷歌表格: =importxml is not loading some results

javascript - 从 Office/Outlook 加载项粘贴超链接

search - 我可以以某种方式停靠搜索框 (ctrl +f) 或在 Google 表格的顶部菜单中创建一个新的搜索框,以便它始终可见吗?

javascript - 在 Google 表格中移动单元格范围