Skip to content

Instantly share code, notes, and snippets.

@dsottimano
Last active May 23, 2023 18:10
Show Gist options
  • Save dsottimano/b7432b226187a953734c318db3742fa1 to your computer and use it in GitHub Desktop.
Save dsottimano/b7432b226187a953734c318db3742fa1 to your computer and use it in GitHub Desktop.
URL Parser for Apps Script
//functions to parse URLs adapted for easy usage in apps script
//past the code below in the tools > script editor of any google sheet
//call the PARSE_URI() formula directly from a cell
// PARSE_URI 1.2.2
// (c) Steven Levithan <stevenlevithan.com>
// MIT License
//adapted for apps script by @dsottimano
/**
* Returns URL parts
* @param {"https://developer.mozilla.org/en-US/docs/Web/"} url the url you want to parse
* @param {"host"} part the url part you want to return. "source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor" are acceptable values
* @customfunction
*/
function PARSE_URI(url,part) {
try {
if(url.map) {
return url.map(function(u) {return PARSE_URI(u,part)})
} else {
PARSE_URI.options = {
strictMode: false,
key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
q: {
name: "queryKey",
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
if(!IS_VALID_URL(url)) return "Please enter a valid URL";
var o = PARSE_URI.options,
m = o.parser[o.strictMode ? "strict" : "loose"].exec(url),
uri = {},
i = 14;
while (i--) uri[o.key[i]] = m[i] || "";
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
if (!uri[part]) return ("No part defined")
if(part === "host") {
uri = uri[part].split(".")
if (uri.length > 2) return uri[uri.length-2] + "." + uri[uri.length-1]
return uri.join('.')
}
if(part) return uri[part]
return uri
}
}catch(e) {
return e
}
};
//isValidURl credit: https://stackoverflow.com/a/49849482/2121455
function IS_VALID_URL(str) {
if (typeof str != 'string' || !str) throw "Please enter a valid string"
try {
if (str.map) {
return str.map(function(s) {return IS_VALID_UR(s)});
} else {
var res = str.match(/(http(s)?:\/\/.)?(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g);
if (res) return true
return false
}
} catch(e) {
return e
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment