Last active
May 11, 2021 19:58
-
-
Save naosim/9acbbae93ca3e05d3bd0840b0b442eeb to your computer and use it in GitHub Desktop.
【GAS】githubやgist上のライブラリを読み込むスニペット
This file contains hidden or 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
/** | |
* 外部ファイルから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