Skip to content

Instantly share code, notes, and snippets.

@naosim
Last active May 11, 2021 19:58
Show Gist options
  • Save naosim/9acbbae93ca3e05d3bd0840b0b442eeb to your computer and use it in GitHub Desktop.
Save naosim/9acbbae93ca3e05d3bd0840b0b442eeb to your computer and use it in GitHub Desktop.
【GAS】githubやgist上のライブラリを読み込むスニペット
/**
* 外部ファイルからjsを読み込む
* githubやgists上にあるコードを使いたい時用
*/
function loadLibForOneMethod(mainMethodName, url) {
// リトライありのfetch
function retryFetch(url) {
var lastError = null;
for(var i = 0; i < 3; i++) {
try {
var res = UrlFetchApp.fetch(url)
if(res.getResponseCode() == 200) {
return res.getContentText("UTF-8")
} else {
lastError = 'HTTPステータスコードが200以外: ' + res.getResponseCode() + ', ' + url;
}
} catch(e) {
lastError = e;
Logger.log(e);
}
Utilities.sleep(3000);
}
throw lastError + ', ' + url
}
eval(retryFetch(url));
return eval(mainMethodName);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment