Created
April 4, 2023 08:28
-
-
Save ineiti/ba5611c91ed2745ce99b7613f4ddb322 to your computer and use it in GitHub Desktop.
Getting the link from a google spreadsheet with a richtext link
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 link of the richtext link in the given cell. If there | |
// is no link, returns an empty string. | |
// If there is a link, removes an eventual "mailto:" in front of it. | |
// To use it: | |
// | |
// =getCellEmail(C2) | |
function getCellEmail(address) { | |
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 + ' is not a valid range' + formula); | |
} | |
var url = range.getRichTextValue().getLinkUrl() | |
if (!url) | |
return ""; | |
var email = url.match(/^mailto:(.*)/); | |
if (email.length > 1) | |
return email[1]; | |
return url; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment