Forked from glureau/GoogleSheet : export links and texts separately from a rich text
Created
September 3, 2021 05:12
Star
You must be signed in to star a gist
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.getRichTextValues(); | |
var output = []; | |
for (var i = 0; i < formulas.length; i++) { | |
var formula = formulas[i] | |
var urls = []; | |
var urlsStr = ""; | |
for (var j = 0; j < formula.length; j++) { | |
var run = formula[j].getRuns(); | |
for (var k = 0; k < run.length; k++) { | |
var url = run[k].getLinkUrl(); | |
if (url != null) { | |
urlsStr += run[k].getText() + " " + run[k].getLinkUrl() + "\n"; | |
} | |
} | |
} | |
output.push(urlsStr); | |
} | |
return output | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment