Last active
April 5, 2023 13:31
-
-
Save csarigoz/026b8a4a2e3cd56a70daf17befdcf943 to your computer and use it in GitHub Desktop.
Google sheet custom function that returns the URL of a hyperlinked cell, if it's entered with hyperlink command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns the URL of a hyperlinked cell, if it's entered with hyperlink command. | |
* Supports ranges | |
* @param {A1} reference Cell reference | |
* @customfunction | |
*/ | |
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.getFormulas(); | |
var output = []; | |
for (var i = 0; i < formulas.length; i++) { | |
var row = []; | |
for (var j = 0; j < formulas[0].length; j++) { | |
var url = formulas[i][j].match(/=hyperlink\("([^"]+)"/i); | |
row.push(url ? url[1] : ''); | |
} | |
output.push(row); | |
} | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment