简体   繁体   中英

Unlink all hyperlinks from a sheet

I'm trying to Unlink all hyperlinks in a Google Spreadsheet, so far I'm trying to do a search and replace which is the code below, but wondering if there is a specific function to do the unlink?

function replacelinks(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Links");
  var values = sheet.getDataRange().getValues();  
  replaceInSheet(values, '.com', '');
  replaceInSheet(values, '.net', '');

I believe your goal as follows.

  • You want to remove all hyperlinks on a sheet in a Google Spreadsheet using Google Apps Script.

For this, how about this answer?

Modification points:

  • Unfortunately, in the current stage, it seems that there are no methods for directly removing the hyperlinks of the cells and texts. So in order to achieve your goal, I would like to propose a workaround. The flow of this workaround is as follows.

    1. Copy the sheet you want to remove the hyperlinks in the Google Spreadsheet as a temporal sheet.
    2. Retrieve the richTextValues from the data range of the source sheet.
    3. Retrieve the cell coordinates, which have the hyperlinks, and create an object for using the method of batchUpdate in Sheets API.
    4. Request to the batchUpdate method using the created object.
      • The cells with the hyperlinks on the source sheet are cleared, and the values of the same coordinates are copied from the temporal sheet to the source sheet. By this, the hyperlinks are removed.

Sample script:

Please copy and paste the following script to the script editor. And, please enable Sheets API at Advanced Google services . And please set the sheet name you want to remove the hyperlinks.

function myFunction() {
  const sheetName = "Sheet1"; // Please set the sheet name.

  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const sheetId = sheet.getSheetId();
  const range = sheet.getDataRange();
  const temp = ss.insertSheet();
  const tempId = temp.getSheetId();
  range.copyTo(temp.getRange("A1"), {contentsOnly: true});
  const requests = range.getRichTextValues().reduce((ar, row, i) => {
    row.forEach((col, j) => {
      const runs = col.getRuns();
      if (col.getLinkUrl() || (runs && runs.some(e => e.getLinkUrl()))) {
        const req1 = {updateCells:{range:{sheetId:sheetId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},fields:"userEnteredValue"}};
        const req2 = {copyPaste:{
          source:{sheetId:tempId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},
          destination:{sheetId:sheetId,startRowIndex:i,endRowIndex:i + 1,startColumnIndex:j,endColumnIndex:j + 1},
          pasteType:"PASTE_NORMAL"
        }};
        ar = ar.concat(req1, req2);
      }
    });
    return ar;
  }, []);
  Sheets.Spreadsheets.batchUpdate({requests: requests}, ss.getId());
  ss.deleteSheet(temp);
}
  • In this script, the hyperlinks of both with and without HYPERLINK() can be removed.

Note:

  • In the current stage, it seems that getRichTextValues() cannot retrieve the number values. By this, when the number values has the hyperlinks, these hyperlinks cannot be removed. About this issue, I have already reported to the issue tracker. Ref So when this issue was removed, I think that using above script, all hyperlinks might be be able to be removed. Or, in the current stage, the method of getLinkUrl() used in above script is not included in the official document. So I also think that the method for directly removing the hyperlinks might be added in the future update.

References:

What helped for me was to setShowHyperlink to false for the range.

Example:

const ss = SpreadsheetApp.getActiveSpreadsheet();
const ws = ss.getActiveSheet();
const range = ws.getRange(lastRow, 1)
range.setValue('https://example.com');
range.setShowHyperlink(false);

But as soon as you double click such a cell, Google will re-apply the hyperlink.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM