Updated Specification of Google Spreadsheet: Multiple Hyperlinks to a Cell

Gists

Recently, it seems that the specification of Google Spreadsheet was updated. Before this, when a cell has only one hyperlink. In this case, the hyperlink was given to a cell using =HYPERLINK("http://www.google.com/", "Google") as following figure.

Updated Specification of Google Spreadsheet: Multiple Hyperlinks to a Cell

But by the recent update, a cell got to be able to have multiple hyperlinks as following figure. In this case, the hyperlinks are set by the RichTextValue object.

Updated Specification of Google Spreadsheet: Multiple Hyperlinks to a Cell

In this report, I would like to introduce the method for setting and retrieving the multiple URLs for a cell.

Set multiple URLs to a cell

In this section, I would like to introduce the method for setting the multiple URLs to a cell using the sample script. As the sample situation, above figure is achieved using a sample script.

Sample script

const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
const RichTextValue = SpreadsheetApp.newRichTextValue()
  .setText("url1 and url2")
  .setLinkUrl(0, 4, "http://www.google.com/")
  .setLinkUrl(9, 13, "https://tanaikech.github.io/")
  .build();
sheet.getRange("A1").setRichTextValue(RichTextValue);

Get multiple URLs from a cell

In this section, I would like to introduce the method for retrieving the multiple URLs from a cell using the sample script. As the sample situation, the URLs are retrieved from above figure.

Sample script

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

Note

References

 Share!