Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 correctly. For each row, I'm trying to extract the URL ONLY (not the friendly text) and insert it into the adjacent column. How could this be accomplished using a formula?

For example:

=SOMEFUNCTION(cellThatHoldsLink, returnedURLOnly) 

This and similar scenarios do not apply because the pasted data are not formulas. I will accept a script (GAS) solution, or any solution for that matter, but would prefer if it could be done using a formula. I have found dozens of HYPERLINK manipulation scripts, but nothing on this particular scenario, or even how to access the property that is holding that url. Thanks.

like image 898
iamtoc Avatar asked Feb 05 '16 18:02

iamtoc


People also ask

Can Google Sheets fetch data from URL?

Did you know that you can pull data from websites into your Google spreadsheet, automatically? There is an incredibly useful function in Google Sheets called IMPORTXML, which you can use to retrieve data from web pages, where that data is pulled into a Google spreadsheet on an automated basis.


1 Answers

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 } 
like image 146
Frederico Schardong Avatar answered Sep 18 '22 22:09

Frederico Schardong