Skip to content

Instantly share code, notes, and snippets.

@leaguecodeuk
Forked from Banhawy/getStatusCode.js
Created December 5, 2022 09:51
Show Gist options
  • Save leaguecodeuk/0b8588efeb79e71cf77f6efbf01f6f23 to your computer and use it in GitHub Desktop.
Save leaguecodeuk/0b8588efeb79e71cf77f6efbf01f6f23 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
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