How to get url text out of field with HYPERLINK in it

后端 未结 4 1118
醉梦人生
醉梦人生 2021-01-11 20:29

I have a column with hyperlinks formula in it, for example:

=HYPERLINK(\"http://example.com\", \"Link\")

I want to get additional column, w

4条回答
  •  长情又很酷
    2021-01-11 20:53

    You can accomplish this by using Apps Script to create a custom function. Try this:

    1. Open your Google Sheet.
    2. In the menu bar, open Tools > Script editor...
    3. In Code.gs, paste the following and save:

      function EXTRACT_URL(input) {
      
        var range = SpreadsheetApp.getActiveSheet().getRange(input);
        var re = /^.+?\(\"(.+?)\",.+?$/;
        if (input.indexOf(':') != -1) {
          var formulas = range.getFormulas();
          for (var i in formulas) {
            for (var j in formulas[i]) {
              formulas[i][j] = formulas[i][j].replace(re, "$1");
            }
          }
          return formulas;
        } else {
          return range.getFormula().replace(re, "$1");
        }
      
      }
      

    This creates a custom function. In your Google Sheet, you can use this function as you would use any other function; however, there's one caveat, which is that you'll have to put the cell in quotes, e.g.:

    =EXTRACT_URL("A1")
    

    To make the quotes thing less of a hassle, the above script supports ranges as well:

    =EXTRACT_URL("A1:B10")
    

    Hope this helps!

提交回复
热议问题