How to extract URL from Link in Google Sheets using a formula?

后端 未结 9 1899
面向向阳花
面向向阳花 2020-12-13 09:33

I have copied from a website a series of hyperlinks and pasted them in a google sheet. The values show up as linked text, not hyperlink formulas, and are still linked correc

相关标签:
9条回答
  • 2020-12-13 10:06

    Ryan Tarpine's Example helped a lot. Thanks!

    With the code below, you can replace all embedded links by standard HYPERLINK formulas within a selected Range. Please note, that the Advanced Sheets Service must be activated.

    function embeddedURLsToHyperlink() {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = spreadsheet.getActiveSheet();
      var rows = sheet.getActiveRange().getValues();
      var z = sheet.getActiveRange().getRowIndex();
      var s = sheet.getActiveRange().getColumn();
    
      for (var r = 0; r < rows.length; r++) {
        var row = rows[r];
        for (var c = 0; c < row.length; c++) {
          var val = row[c];
          if (val) {
            var targetRange = sheet.getRange(r+z, c+s).getA1Notation();
            var result = Sheets.Spreadsheets.get(spreadsheet.getId(), {
              ranges: sheet.getName() + "!" + targetRange,
              fields: 'sheets.data.rowData.values'
            });
            if (result.sheets[0].data[0].rowData[0].values[0].hyperlink) {
              var url = result.sheets[0].data[0].rowData[0].values[0].hyperlink;
              var text = result.sheets[0].data[0].rowData[0].values[0].effectiveValue.stringValue;
              sheet.getRange(r + z, c + s).setFormula('=HYPERLINK("' + url + '","' + text + '")');
            }
          }
        }
      }
    }
    

    I you want to process the full sheet, replace lines 4-6 by the following code:

    var rows = sheet.getDataRange().getValues();
    var z = 1;
    var s = 1;
    
    0 讨论(0)
  • 2020-12-13 10:12

    If your hyperlink is specified in another cell as a formula—for example let's suppose that cell A1 contains the formula =HYPERLINK("https://www.wikipedia.org/","Wikipedia"), you can extract the Link text using a regular expression. All you need to do is:

    =REGEXEXTRACT(FORMULATEXT(A1),"""(.+)"",")

    This formula will yield the result:

    https://www.wikipedia.org/

    No custom functions required.

    0 讨论(0)
  • 2020-12-13 10:15

    The built-in SpreadsheetApp service doesn't seem to support pulling such URLs out, but the “Advanced” Sheets service does.

    Enable the Advanced Sheets service according to Google's instructions, and then try this code:

    function onOpen() {
      var menu = SpreadsheetApp.getUi().createMenu("Extract URLs");
      menu.addItem("Process =EXTRACT_URL(A1) formulas", "processFormulas");
      menu.addToUi();
    }
    
    function EXTRACT_URL() {
      return SpreadsheetApp.getActiveRange().getFormula();
    }
    
    function processFormulas() {
      var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = spreadsheet.getActiveSheet();
      var rows = sheet.getDataRange().getFormulas();
      for (var r = 0; r < rows.length; r++) {
        var row = rows[r];
        for (var c = 0; c < row.length; c++) {
          var formula = row[c];
          if (formula) {
            var matched = formula.match(/^=EXTRACT_URL\((.*)\)$/i);
            if (matched) {
              var targetRange = matched[1];
              if (targetRange.indexOf("!") < 0) {
                targetRange = sheet.getName() + "!" + targetRange;
              }
              var result = Sheets.Spreadsheets.get(spreadsheet.getId(), {
                ranges: targetRange,
                fields: 'sheets.data.rowData.values.hyperlink'
              });
              try {
                var value = result.sheets[0].data[0].rowData[0].values[0].hyperlink;
                sheet.getRange(r + 1, c + 1).setValue(value);
              } catch (e) {
                // no hyperlink; just ignore
              }
            }
          }
        }
      }
    }
    

    This creates a custom function called EXTRACT_URL, which you should call with a reference to the cell that contains the link; for example, =EXTRACT_URL(B3).

    Unfortunately it doesn't work immediately, because the Advanced Sheets service can't be used directly by custom functions. So this script adds a menu called “Extract URLs” to the spreadsheet menu bar, with one menu item labeled “Process =EXTRACT_URL(A1) formulas”. When you click it, it will replace all uses of the EXTRACT_URL function with the URL itself.

    0 讨论(0)
  • 2020-12-13 10:16

    After some update in 2020 all codes I have found on the Internet were broken, so here is my contribution:

    /** 
     * Returns the URL of a hyperlinked cell, if it's entered with control + k. 
     * Author: @Frederico Schardong based on https://support.google.com/docs/thread/28558721?hl=en&msgid=28927581 and https://github.com/andrebradshaw/utilities/blob/master/google_apps/convertHiddenLinks.gs 
     * Supports ranges
     */
    function linkURL(reference) {
      var sheet = SpreadsheetApp.getActiveSheet();
      var formula = SpreadsheetApp.getActiveRange().getFormula();
      var args = formula.match(/=\w+\((.*)\)/i);
      try {
        var range = sheet.getRange(args[1]);
      }
      catch(e) {
        throw new Error(args[1] + ' is not a valid range');
      }
    
      var formulas = range.getRichTextValues();
      var output = [];
      for (var i = 0; i < formulas.length; i++) {
        var row = [];
        for (var j = 0; j < formulas[0].length; j++) {
          row.push(formulas[i][j].getLinkUrl());
        }
        output.push(row);
      }
      return output
    }
    
    0 讨论(0)
  • 2020-12-13 10:17

    If you happy to use Google Apps Script then use below function to get the hyperlink from a text. When you pass the cell, you should send with double quote. Eg: =GETURL("A4") to get the A4 hyperlink.

    function GETURL(input) {
      var range = SpreadsheetApp.getActiveSheet().getRange(input);
      var url = /"(.*?)"/.exec(range.getFormulaR1C1())[1];
      return url;
    }
    

    Refer here for example.

    Edit: Ignore this answer. This will only work if url is linked in cell.

    0 讨论(0)
  • 2020-12-13 10:17

    Quick way to do it If cell rows are limited --->

    1. just right click cell and click edit link
    2. Remove Text and Click Apply
    3. Use f4 for every cell that follow

    Please if you have more to this or can be done with selection of cell. Let me know

    0 讨论(0)
提交回复
热议问题