-
Notifications
You must be signed in to change notification settings - Fork 1
/
Utils.js
executable file
·129 lines (110 loc) · 3.29 KB
/
Utils.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/**
* Include file into a template
* @param String filename HTML file to include
* @return String HTML file content
*/
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
function showYesNoAlert(title, message) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
title,
message,
ui.ButtonSet.YES_NO);
return result == ui.Button.YES;
}
/**
* Show an alert with only OK button
* @param String title alert title
* @param String message alert message
*
*/
function showOKAlert(title, message) {
var ui = SpreadsheetApp.getUi();
var result = ui.alert(
title,
message,
ui.ButtonSet.OK);
}
/**
* Get all DwC fields used in the active spreadsheet
* @return {array}
*/
function getSpredsheetFields(sheetIndex = 0, onlyIDs = false) {
var documentProperties = PropertiesService.getDocumentProperties();
var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
var sheet = null;
if (sheetIndex > 0 && sheetIndex <= sheets.length) {
sheet = sheets[sheetIndex-1];
} else {
sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
}
var key = sheet.getName().replace(/\s+/gi, '-').toLowerCase() + '-metadata';
var metadata = documentProperties.getProperty(key);
// Has DwC header
if (metadata) {
var fields = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
var terms = fields[0];
terms = terms
.filter(function(t){ return t != ''})
.flatMap(function(t,i){ return {'index': i, 'term': t }; });
if ( onlyIDs ){
terms = terms.filter(function(t) {
return t.term.indexOf("ID") != -1;
});
}
return terms;
}
else{
SpreadsheetApp.getUi().alert('You need add mappings before this step. Please use Add/Remove mapping in the add-on menu.');
}
return [];
}
/**
* Read Darwin Core XSD and return terms in array
*
* @return {array} Darwin core terms
*/
function xmltoArray() {
var url = 'https://dwc.tdwg.org/xml/tdwg_dwcterms.xsd';
var response = UrlFetchApp.fetch(url);
var xml = response.getContentText();
var document = XmlService.parse(xml);
var root = document.getRootElement();
var xmlschema = XmlService.getNamespace('http://www.w3.org/2001/XMLSchema');
var namespaces = {
dwc: 'http://rs.tdwg.org/dwc/terms/',
dcterms: 'http://purl.org/dc/terms/'
};
var groups = root.getChildren('group', xmlschema);
var terms = [];
for (var i = 0; i < groups.length; i++) {
var entries = groups[i].getChild('sequence', xmlschema).getChildren();
for (var j = 0; j < entries.length; j++) {
qname=entries[j].getAttribute('ref');
if (qname) {
terms.push(qname.getValue());
}
}
}
return {
namespaces: namespaces,
terms: terms
};
}
function saveSettings(settings) {
var props = PropertiesService.getUserProperties();
props.setProperty('enableZenodo', settings.enableZenodo);
props.setProperty('zenodoToken', settings.zenodoToken);
}
function showurl(downloadURL) {
var html = HtmlService.createHtmlOutput()
.setWidth(250)
.setHeight(60)
.setTitle("Your DwC-Archive is ready!")
.setContent('<a href="' + downloadURL + '" target="_blank">Click here to download</a>');
SpreadsheetApp.getUi()
.showModalDialog(html, 'Your DwC-Archive is ready!');
}