Httpget is a useful function that allows you to evaluate code from a url. Using Google's UrlFetchApp api, you can create a similar function for Google user scripts.
Warning: To perform an httpget, you must use an
eval
statement to run ALL code from the remote url. Because of this, it can be very easy to accidentally run malicious code if used recklessly. Please be careful when using this method.
As stated before, httpget simply fetches and evaluates any code placed in a remote url. This can be used for many reasons, such as minimizing installation steps (a quick copy and paste sure does the trick doesn't it?), fetching others' code without having to add a library (use the raw link), or in some cases shrinking file size.
Performing an httpget is simple. Using Google's Url Fetch Service, grabbing the code comes down to a one liner:
let data = UrlFetchApp.fetch("https://example.com/file.gs").getContentText();
Next, evaluate the code that the variable data
returns:
eval(data);
Like I said before, please be careful when using this statement as it is INCREDIBLY easy to accidentally run malicious code. Since there is no other way to run code like this in gs, eval
is the only alternative.
// Normal (100 bytes)
function httpget(url) {
let data = UrlFetchApp.fetch(url).getContentText();
eval(data);
}
// when using in function form, replace the
// actual url with an assignable variable.
// Compressed (59 bytes)
function l(s){eval(UrlFetchApp.fetch(s).getContentText());}
1U5RzdkF4G3QdK9HKJ3B6ScNewj7KThz17yjAyXT3AM-neyNTJQ9qa7Qf
Follow my Github or share (this means a lot to me)
https://github.com/WillDev12
https://WillDev12.github.io