0
votes

My goal is to create a spreadsheet cell that has multiple hyperlinks from a Google script.

I can create a cell with multiple links selecting part of the cell text and then using Ctrl + K or the menu insert link. My cell had google and yahoo as an example, with two hyperlinks, one to each search engine.

I can read the hyperlinks in this cell in two different ways:

 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);

But there is no setLinkUrl method available. I couldn't find anything in the Google sheets API either.

I copied this cell and using xclip in Linux, the command

xclip -selection clipboard -o  -t text/html

The result is

<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>                                                                                                                                                                   

I'm not sure if this means that hyperlinks have their own runs, independent form text style runs? Any pointers are appreciated.

1

1 Answers

5
votes
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