-
Notifications
You must be signed in to change notification settings - Fork 1
/
web app.js
251 lines (216 loc) · 7.75 KB
/
web app.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
function doGet(e)
{
// log some data
// Logger.log("e: " + JSON.stringify(e));
// Logger.log("Session.getActiveUser().getEmail(): " + Session.getActiveUser().getEmail());
// Logger.log("Session.getEffectiveUser().getEmail(): " + Session.getEffectiveUser().getEmail());
// load the HTML file
return HtmlService.createHtmlOutputFromFile("index");
}
function importXML(formObject)
{
var xml, xmlText, spreadsheet, rootFilterElement, fwRules;
var dataRows = [_allFields_], numFields = _allFields_.length;
// get the raw XML text
if(formObject.fromSource == "xmlFile")
{
if(!formObject.xmlFile.getContentType().match(/^text\/((plain)|(xml))$/))
{
return {"success": false, "message": "invalid file type: " + formObject.xmlFile.getContentType()};
}
xmlText = formObject.xmlFile.getDataAsString();
}
else
{
xmlText = formObject.xmlText;
}
// check if the existing sheet exists
if(formObject.destinationType == "googleSheet" && formObject.googleSheetType == "existing")
{
var sheetURL = formObject.existingSheetURL;
var fileID = sheetURL.match(/[-\w]{25,}/);
if(fileID)
{
try
{
spreadsheet = SpreadsheetApp.openById(fileID);
}
catch(e)
{
return {"success": false, "message": "unable to open '" + sheetURL + "'"};
}
}
else
{
return {"success": false, "message": "invalid URL '" + sheetURL + "'"};
}
}
// try reading the XML
try
{
xml = XmlService.parse(xmlText);
}
catch(e)
{
return {"success": false, "message": "invalid XML"};
}
rootFilterElement = xml.getRootElement();
if(rootFilterElement.getName() != "filter") rootFilterElement = rootFilterElement.getChild("filter");
if(!rootFilterElement)
{
return {"success": false, "message": "no 'filter' element"};
}
fwRules = rootFilterElement.getChildren("rule");
if(!fwRules.length)
{
return {"success": false, "message": "no rules"};
}
for(var i = 0, numRules = fwRules.length; i < numRules; ++i)
{
var fwRule = fwRules[i];
var dataRow = [i + 1];
dataRows.push(dataRow);
for(var j = 0; j < numFields; ++j)
{
var fieldName = _allFields_[j];
var fieldToPropertyXMLSchemaMapping = _rulePropertyXMLSchema_[fieldName];
dataRow[j] = _xmlElementGet_(fieldToPropertyXMLSchemaMapping, fwRule, i);
if(formObject.destinationType == "csv" && dataRow[j])
{
dataRow[j] = dataRow[j].toString().replace(/"/g, '""');
}
}
}
if(formObject.destinationType == "googleSheet")
{
if(formObject.googleSheetType == "new")
{
spreadsheet = SpreadsheetApp.create("pfSense FW RM - " + Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd_HH:mm:ss"));
}
var sheet = spreadsheet.insertSheet(Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "yyyy-MM-dd_HH:mm:ss"));
sheet.deleteColumns(2, sheet.getMaxColumns() - 2);
sheet.getRange(1, 1, dataRows.length, dataRows[0].length).setValues(dataRows);
sheet.getRange(1, 1, dataRows.length, dataRows[0].length).applyRowBanding(SpreadsheetApp.BandingTheme.CYAN);
sheet.getRange(1, 1, dataRows.length, dataRows[0].length).createFilter();
sheet.setFrozenRows(1);
for(var j = 0; j < numFields; ++j)
{
var fieldName = _allFields_[j];
var fieldToPropertyXMLSchemaMapping = _rulePropertyXMLSchema_[fieldName];
if(fieldToPropertyXMLSchemaMapping.options)
{
sheet.getRange(2, j + 1, sheet.getMaxRows() - 1).setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(false)
.setHelpText("Please select a valid option from the drop-down.")
.requireValueInList(fieldToPropertyXMLSchemaMapping.options, true)
.build()
);
}
else if(fieldToPropertyXMLSchemaMapping.type == RULE_PROPERTY_XML_TYPE.BOOL)
{
sheet.getRange(2, j + 1, sheet.getMaxRows() - 1).setDataValidation(SpreadsheetApp.newDataValidation()
.setAllowInvalid(false)
.setHelpText("Please enter TRUE or FALSE.")
.requireCheckbox()
.build()
);
}
}
SpreadsheetApp.flush();
return {"success": true, "data": spreadsheet.getUrl() + "#gid=" + sheet.getSheetId()};
}
else
{
for(var i = 0, numRows = dataRows.length; i < numRows; ++i)
{
dataRows[i] = '"' + dataRows[i].join('","') + '"'
}
return {"success": true, "data": dataRows.join("\n")};
}
}
function getSheetNames(sheetURL)
{
var spreadsheet;
var fileID = sheetURL.match(/[-\w]{25,}/);
if(fileID)
{
try
{
spreadsheet = SpreadsheetApp.openById(fileID);
}
catch(e)
{
return {"success": false, "message": "unable to open '" + sheetURL + "'"};
}
}
else
{
return {"success": false, "message": "invalid URL '" + sheetURL + "'"};
}
var sheetNames = [];
var allSheets = spreadsheet.getSheets();
for(var i = 0, numSheets = allSheets.length; i < numSheets; ++i)
{
var sheet = allSheets[i];
if(sheet.getRange(1, 1, 1, _allFields_.length).getDisplayValues()[0].join("|") == _allFields_.join("|"))
{
sheetNames.push(sheet.getName());
}
}
if(sheetNames.length)
{
return {"success": true, "names": sheetNames};
}
else
{
return {"success": false, "message": "no valid sheets found"};
}
}
function exportXML(formObject)
{
var dataRows;
switch(formObject.fromSource)
{
case "googleSheet":
var fileID = formObject.sheetURL.match(/[-\w]{25,}/);
dataRows = SpreadsheetApp.openById(fileID).getSheetByName(formObject.sheetName).getDataRange().getValues();
break;
case "csvFile":
dataRows = Utilities.parseCsv(formObject.csvFile.getDataAsString());
break;
case "csvInput":
dataRows = Utilities.parseCsv(formObject.csvText);
break;
}
if(dataRows[0].join("|") != _allFields_.join("|"))
{
return {"success": false, "message": "malformed/invalid data"};
}
var headerRow = dataRows.shift();
var numFields = headerRow.length;
var orderColumn = headerRow.indexOf("Order");
dataRows.sort((a, b) =>
{
if(a[orderColumn] == b[orderColumn]) return 0;
else return a[orderColumn] < b[orderColumn] ? -1 : 1;
});
var xmlRoot = XmlService.createElement("filter");
for(var i = 0, numRows = dataRows.length; i < numRows; ++i)
{
var dataRow = dataRows[i];
// skip rows where interface is not set
if(!dataRow[orderColumn]) continue;
var xmlRule = XmlService.createElement("rule");
xmlRoot.addContent(xmlRule);
for(var j = 0; j < numFields; ++j)
{
var fieldName = headerRow[j];
var fieldToPropertyXMLSchemaMapping = _rulePropertyXMLSchema_[fieldName];
_xmlElementSet_(fieldToPropertyXMLSchemaMapping, xmlRule, dataRow[j], lookupQuery =>
{
return dataRow[headerRow.indexOf(lookupQuery)];
});
}
}
return {"success": true, "xml": XmlService.getPrettyFormat().format(XmlService.createDocument(xmlRoot))};
}