Created
June 8, 2022 01:40
-
-
Save diva-D/999b926cf06b85cd5be913b31518d13f to your computer and use it in GitHub Desktop.
https://banhawy.medium.com/how-to-use-google-spreadsheets-to-check-for-broken-links-1bb0b35c8525 https://gist.github.com/Banhawy/46a05994f14e1fc9fff8dec7a79627d2#file-getstatuscode-js
This file contains 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