-
Notifications
You must be signed in to change notification settings - Fork 0
/
apps-script.js
72 lines (60 loc) · 2.26 KB
/
apps-script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
//Return the code of the response only. Errors all set to 400 or parse as needed.
function urlStatusCode(url){
let responseCode = null;
try{
responseCode = UrlFetchApp.fetch(url).getResponseCode();
}catch(err){
responseCode = 400;
}
return responseCode;
}
//Check if a URL has a valid endpoint and return true or false for display as checkboxes.
function checkUrl(url){
//init a variable to hold the current status.
let status = null;
//This is the list of acceptable HTTPResponse codes returned by the .getResponseCode() method
//A list of HTTPResponse codes can be found here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
//Alternatively we could check for ranges instead of the individual codes.
const goodUrlCodes = [200,204,201,206];
try{
//URLFetchApp calls googles services to perform the fetch() call
//Google services limits the amount of times this function can be called: 20k on personal accts and 100k per day on business
let responseCode = UrlFetchApp.fetch(url).getResponseCode();
//Check if the response code received matches any in the goodUrlCodes array
if(responseCode && goodUrlCodes.find((elem) => elem = responseCode) === responseCode){
status = true;
}else{
status = false
}
}catch(err){
//If there is ANY error set the value to false
status = false;
}
//return the value of status which will be an enum: True, False or null
return status;
}
//This function calls and sets the cache for future calls if working with a large volume of links
function checkUrlCache(url){
let status = null;
const goodUrlCodes = [200,204,201,206];
//This is for caching responses to avoid hitting the daily quotas on refeshing the browser
let cache = CacheService.getScriptCache();
let cached = cache.get(url);
if(cached && goodUrlCodes.find((elem) => elem = cached) === cached){
status = true;
return status;
}
try{
let responseCode = UrlFetchApp.fetch(url).getResponseCode();
//Store the response in cache if the
cache.put(url,responseCode, 21600);
if(responseCode && goodUrlCodes.find((elem) => elem = responseCode) === responseCode){
status = true;
}else{
status = false
}
}catch(err){
status = false;
}
return status;
}