Google Apps Script snippets ᕦʕ •ᴥ•ʔᕤ

Snippet of Unlink all URLs in the range

Snippet

index.js
/**
 * Removes all links from the range
 *
 * @param {GoogleAppsScript.Spreadsheet.Range} range A range
 * @return {GoogleAppsScript.Spreadsheet.RichTextValue[][]}
 */
function unlinkUrls_(range = SpreadsheetApp.getActiveRange()) {
  return range.setRichTextValues(
    range.getRichTextValues().map((rowRichTextValues) =>
      rowRichTextValues.map((richTextValue) => {
        if (richTextValue.getRuns().some((ruin) => ruin.getLinkUrl())) {
          const copy = richTextValue.copy();
          copy.setLinkUrl(undefined);
          return copy.build();
        }
        return richTextValue;
      })
    )
  );
}

Run it

run.js
/* global unlinkUrls_ */

/**
 * Runs the snippet.
 * Unlink URLs
 */
function run() {
  const sheet = SpreadsheetApp.getActiveSheet();
  const range = sheet.getDataRange();
  unlinkUrls_(range);
}

/**
 * Create menu for handy use
 */
function onOpen() {
  SpreadsheetApp.getUi()
    .createMenu('Apps Script Snippets')
    .addItem('Unlink all urls on the Sheet', 'run')
    .addToUi();
}

Issues

This code does not remove links if they are the only content in the cell. See google apps script - Unlink all hyperlinks from a sheet - Stack Overflow

#sheets #Sheets advanced service