Created
February 19, 2021 00:08
-
-
Save Banhawy/46a05994f14e1fc9fff8dec7a79627d2 to your computer and use it in GitHub Desktop.
A Google Sheets app script that takes a url and returns the resulting status code from requesting that url
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
function getStatusCode(url) { | |
var url_trimmed = url.trim(); | |
// Check if script cache has a cached status code for the given url | |
var cache = CacheService.getScriptCache(); | |
var result = cache.get(url_trimmed); | |
// If value is not in cache/or cache is expired fetch a new request to the url | |
if (!result) { | |
var options = { | |
'muteHttpExceptions': true, | |
'followRedirects': false | |
}; | |
var response = UrlFetchApp.fetch(url_trimmed, options); | |
var responseCode = response.getResponseCode(); | |
// Store the response code for the url in script cache for subsequent retrievals | |
cache.put(url_trimmed, responseCode, 21600); // cache maximum storage duration is 6 hours | |
result = responseCode; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment