Reputation: 1196
I have a column with hyperlinks formula in it, for example:
=HYPERLINK("http://example.com", "Link")
I want to get additional column, where only urls (as a text) from the first column will be present, that is, in this example:
http://example.com
Is there a function that allows extraction of an url from HYPERLINK? I was thinking also about getting formula text from the first column and cutting it with SPLIT/SUBSTITUTE in the final one, but I'm not sure if I can get one field code into another field.
Upvotes: 11
Views: 23048
Reputation: 31
I just created this script and it worked
function URL(reference) {
var sheet = SpreadsheetApp.getActiveSheet();
var formula = SpreadsheetApp.getActiveRange().getFormula();
var args = formula.match(/=\w+\((.*)\)/i);
try {
var range = sheet.getRange(args[1]).getRichTextValue().getLinkUrl();
}
catch(e) {
throw new Error(args[1] + ' is not a valid range');
}
return range;
}
Upvotes: 3
Reputation: 4874
You can get the text of the hyperlink formula cell using FORMULATEXT, then extract the URL using a regular expression.
=REGEXEXTRACT(FORMULATEXT(A1), "^=HYPERLINK(""([^""]+)")
Upvotes: 0
Reputation: 411
Try this formulas
A2=index(SPLIT(SUBSTITUTE(FORMULATEXT(A1),"=HYPERLINK(""",""),""","""),1,1)
example
A1=HYPERLINK("http://example.com", "Link")
result is
A2=http://example.com
Upvotes: 6
Reputation: 21
You can accomplish this by using Apps Script to create a custom function. Try this:
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!
Upvotes: 2
Reputation: 9106
You can use Google Apps Script and a combination of Macros, Menus, and Custom Functions to extract the value of the hyperlink out of the cell's formula.
Here's how it would work:
Code.gs
Code.gs
with the contents from this Pastebin, taken from this Google Docs Help Forum
topic=HYPERLINK
functionsExtract -> Replace formulas with Text strings
to initiate the scriptThe macro will recursively extract out the URL of the hyperlink function, leaving you with just the value.
Note: Always make a backup of your work before making any changes.
I would recommend performing this task on a duplicate spreadsheet or at a minimum a copy of the original cells in case anything should go wrong of if you wish to retain the original cell's content or formatting.
Upvotes: 1
Reputation: 59450
One way would be to copy the column containing the formulae (assuming you would like to retain them) and remove the 'excess'. The removal can be achieved with Edit > Find and Replace... with Replace with left blank in each case:
The part to the left:
Find =HYPERLINK("
and check Also search within formulae
(If you want to break the link, put '
in *Replace with *.)
The part to the right:
Find ".+
and check Search using regular expressions and Also search within formulae.
Upvotes: 1