-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCode.js
102 lines (93 loc) · 3.61 KB
/
Code.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* Copyright The British Library
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* n.b. Boilerplate based on:
* https://github.com/gsuitedevs/apps-script-samples/blob/master/sheets/dateAddAndSubtract/dateAddAndSubtract.gs
*/
/**
* @fileoverview Provides the custom functions WEBARCHIVE_STATUS_UKWA, WEBARCHIVE_STATUS_UKGWA and WEBARCHIVE_STATUS_IA and
* the helper functions that they use.
* @OnlyCurrentDoc
*/
/**
* Runs when the document is opened, creating the add-on's menu. Custom function
* add-ons need at least one menu item, since the add-on is only enabled in the
* current spreadsheet when a function is run.
*/
function onOpen() {
SpreadsheetApp.getUi().createAddonMenu()
.addItem('Use in this spreadsheet', 'use')
.addToUi();
}
/**
* Enables the add-on on for the current spreadsheet (simply by running) and
* shows a popup informing the user of the new functions that are available.
*/
function use() {
var title = 'UKWA Google Sheets Functions';
var message = 'The functions WEBARCHIVE_STATUS_UKWA, WEBARCHIVE_STATUS_UKGWA and WEBARCHIVE_STATUS_IA are now available in ' +
'this spreadsheet. More information is available in the function help ' +
'box that appears when you start using them in a formula.';
var ui = SpreadsheetApp.getUi();
ui.alert(title, message, ui.ButtonSet.OK);
}
/**
* Checks the status of a URL at the UK Web Archive.
* @param {String} url The URL to check, e.g. https://www.bl.uk/
* @return {Number} The HTTP status code of that URL in the archive. i.e. 404 means it's not in the archive.
* @customFunction
*/
function WEBARCHIVE_STATUS_UKWA(input) {
console.log("Checking UKWA...")
if(input == "") {
return "";
}
var url = "https://www.webarchive.org.uk/wayback/archive/" + input;
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
// Try to avoid going too fast, due to 20,000 calls/day default quota per user (https://developers.google.com/apps-script/guides/services/quotas).
//Utilities.sleep(250);
return response.getResponseCode();
}
/**
* Checks the status of a URL at the UK Government Web Archive.
* @param {String} url The URL to check, e.g. https://www.gov.uk/
* @return {Number} The HTTP status code of that URL in the archive. i.e. 404 means it's not in the archive.
* @customFunction
*/
function WEBARCHIVE_STATUS_UKGWA(input) {
console.log("Checking UKGWA...")
if(input == "") {
return "";
}
var url = "https://webarchive.nationalarchives.gov.uk/" + input;
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
return response.getResponseCode();
}
/**
* Checks the status of a URL at the Internet Archive.
* @param {String} url The URL to check, e.g. https://www.bl.uk/
* @return {Number} The HTTP status code of that URL in the archive. i.e. 404 means it's not in the archive.
* @customFunction
*/
function WEBARCHIVE_STATUS_IA(input) {
console.log("Checking IA...")
if(input == "") {
return "";
}
var url = "https://web.archive.org/web/" + input;
var response = UrlFetchApp.fetch(url, {'muteHttpExceptions': true});
return response.getResponseCode();
}