-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_sheets_pdf_additional_options.gs
352 lines (286 loc) · 20.9 KB
/
test_sheets_pdf_additional_options.gs
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/*
* Copyright Laura Taylor
* (https://github.com/techstreams/TSDynamicUrls)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
// Setup QUnit helpers with global object
QUnit.helpers(this);
/*
* Create QUnit Testing Dashboard for Google Apps Script
* See https://github.com/simula-innovation/qunit/tree/gas/gas for more information and setup instructions
*
* @param {Object} e - event object passed to doGet() method
* @return {string} html of testing output
*/
function doGet(e) {
QUnit.urlParams(e.parameter);
QUnit.config({ title: 'Unit tests for TSDynamicUrls' });
QUnit.load(cases_);
return QUnit.getHtml();
};
/*
* TSDynamicUrls Unit Tests Main Function
*
* To perform TSDynamicUrls unit testing, change the desired unit test configuration = "true" in the "testConfig" object below
*/
var cases_ = function() {
// To perform TSDynamicUrls unit testing, change the desired unit configuration parameter to 'true' as indicated below
var testConfig = {
testfrozenrows: false, // Change to 'true' to test Sheets PDF core frozen rows
testfrozencolumns: false, // Change to 'true' to test Sheets PDF core frozen columns
testgridlines: false, // Change to 'true' to test Sheets PDF core gridlines
testtitle: false, // Change to 'true' to test Sheets PDF core ss title
testsheetnames: false, // Change to 'true' to test Sheets PDF core sheet names
testpagenumbers: false, // Change to 'true' to test Sheets PDF core page numbers
testnotes: false, // Change to 'true' to test Sheets PDF core notes
testnooptions: false, // Change to 'true' to test Sheets PDF core no additional pdf options
sheetspdfoptions: {
input: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/edit?usp=sharing',
rows: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=false&fzc=true&gridlines=true&printtitle=true&sheetnames=true&pagenum=CENTER&printnotes=true&attachment=true',
columns: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=true&fzc=false&gridlines=true&printtitle=true&sheetnames=true&pagenum=CENTER&printnotes=true&attachment=true',
gridlines: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=true&fzc=true&gridlines=false&printtitle=true&sheetnames=true&pagenum=CENTER&printnotes=true&attachment=true',
title: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=true&fzc=true&gridlines=true&printtitle=false&sheetnames=true&pagenum=CENTER&printnotes=true&attachment=true',
sheetnames: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=true&fzc=true&gridlines=true&printtitle=true&sheetnames=false&pagenum=CENTER&printnotes=true&attachment=true',
pagenumbers: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=true&fzc=true&gridlines=true&printtitle=true&sheetnames=true&printnotes=true&attachment=true',
notes: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=true&fzc=true&gridlines=true&printtitle=true&sheetnames=true&pagenum=CENTER&printnotes=false&attachment=true',
nooptions: 'https://docs.google.com/spreadsheets/d/1VwLdHIZHxE8kKBZ72e1LEwrlm2WtI9kgar6Z1iSPtW4/export?format=pdf&size=0&portrait=false&scale=1&pageorder=1&horizontal_alignment=CENTER&vertical_alignment=TOP&fzr=false&fzc=false&gridlines=false&printtitle=false&sheetnames=false&printnotes=false&attachment=true',
},
pdfoptions: {
rows: ['Repeat Frozen Columns','Show Gridlines','Show Spreadsheet Title','Show Sheet Names','Show Page Numbers','Show Notes'],
columns: ['Repeat Frozen Rows','Show Gridlines','Show Spreadsheet Title','Show Sheet Names','Show Page Numbers','Show Notes'],
gridlines: ['Repeat Frozen Rows','Repeat Frozen Columns','Show Spreadsheet Title','Show Sheet Names','Show Page Numbers','Show Notes'],
title: ['Repeat Frozen Rows','Repeat Frozen Columns','Show Gridlines','Show Sheet Names','Show Page Numbers','Show Notes'],
sheetnames: ['Repeat Frozen Rows','Repeat Frozen Columns','Show Gridlines','Show Spreadsheet Title','Show Page Numbers','Show Notes'],
pagenumbers: ['Repeat Frozen Rows','Repeat Frozen Columns','Show Gridlines','Show Spreadsheet Title','Show Sheet Names','Show Notes'],
notes: ['Repeat Frozen Rows','Repeat Frozen Columns','Show Gridlines','Show Spreadsheet Title','Show Sheet Names','Show Page Numbers'],
},
formSubmitFunction: 'processResponses',
email: Session.getActiveUser().getEmail()
};
module('TSDynamicUrls');
// Test Sheets - PDF Options
if (testConfig.testfrozenrows === true) {
test('Sheets URLs PDF - No Frozen Rows', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Frozen Rows
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.rows, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.rows, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Frozen Rows) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testfrozencolumns === true) {
test('Sheets URLs PDF - No Frozen Columns', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Frozen Columns
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.columns, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.columns, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Frozen Columns) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testgridlines === true) {
test('Sheets URLs PDF - No Gridlines', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Gridlines
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.gridlines, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.gridlines, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Gridlines) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testtitle === true) {
test('Sheets URLs PDF - No Spreadsheet Title', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Spreadsheet Title
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.title, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.title, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Spreadsheet Title) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testsheetnames === true) {
test('Sheets URLs PDF - No Sheet Names', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Sheet Names
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.sheetnames, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.sheetnames, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Sheet Names) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testpagenumbers === true) {
test('Sheets URLs PDF - No Page Numbers', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Page Numbers
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.pagenumbers, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.pagenumbers, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Page Numbers) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testnotes === true) {
test('Sheets URLs PDF - No Notes', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Notes
TestUtil.createSheetResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top',testConfig.pdfoptions.notes, 'No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.notes, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Notes) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
// Test Sheets - PDF Options
if (testConfig.testnooptions === true) {
test('Sheets URLs PDF - No Additional Options', function() {
expect(1);
var tsdu = null,
form = FormApp.getActiveForm(),
responses, response;
// Disable form submit trigger for testing
TestUtil.deleteTriggers(ScriptApp.EventType.ON_FORM_SUBMIT, testConfig.formSubmitFunction);
// Test Sheets PDF - All Sheets No Custom Margin - No Additional Options
TestUtil.createSheetNoopResponse(form, ['Sheets', 'PDF (Portable Document Format)', testConfig.sheetspdfoptions.input, 0, ['Letter','Landscape','Normal (100%)','Down, then over','Center','Top','No','All Sheets']]);
responses = form.getResponses();
response = responses[responses.length-1];
tsdu = new TSDynamicUrls(form, response).convertUrl();
equal(tsdu.getUrl(), testConfig.sheetspdfoptions.nooptions, 'TSDynamicUrls successfully creates Sheets PDF (All Sheets with No Additional Options) URL');
// Re-enable form submit trigger and form email collection after testing
tsdu.enableForm();
});
}
/*
* Various TSDynamic Unit Test Utility Helper Functions
*/
var TestUtil = {
createSheetResponse: function(form, params) {
var response = form.createResponse();
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Google Document Format?',0)).asMultipleChoiceItem().createResponse(params[0]));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Google Sheets Modification Type?',0)).asMultipleChoiceItem().createResponse(params[1]));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.LIST, 'Size?',0)).asListItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Orientation?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Scale?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Page Order?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Horizontal Alignment?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Vertical Alignment?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.CHECKBOX, 'Additional PDF Options?',0)).asCheckboxItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Custom Margins?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Include Sheets?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.TEXT, 'Google Document URL',params[3])).asTextItem().createResponse(params[2]));
response.submit();
},
createSheetNoopResponse: function(form, params) {
var response = form.createResponse();
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Google Document Format?',0)).asMultipleChoiceItem().createResponse(params[0]));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Google Sheets Modification Type?',0)).asMultipleChoiceItem().createResponse(params[1]));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.LIST, 'Size?',0)).asListItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Orientation?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Scale?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Page Order?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Horizontal Alignment?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Vertical Alignment?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Custom Margins?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.MULTIPLE_CHOICE, 'Include Sheets?',0)).asMultipleChoiceItem().createResponse(params[4].shift()));
response.withItemResponse(form.getItemById(TestUtil.getItemId(form, FormApp.ItemType.TEXT, 'Google Document URL',params[3])).asTextItem().createResponse(params[2]));
response.submit();
},
deleteTriggers: function(type, functionName) {
ScriptApp.getProjectTriggers().forEach(function(trigger) {
if ((trigger.getEventType() === type && trigger.getHandlerFunction() === functionName )) {
ScriptApp.deleteTrigger(trigger);
}
});
},
getItemId: function(form, type, name, location) {
var ids = form.getItems(type).filter(function(item) {
return item.getTitle() === name;
});
return ids[location].getId();
},
getTriggers: function(type, functionName) {
return ScriptApp.getProjectTriggers().filter(function(trigger) {
if ((trigger.getEventType() === type && trigger.getHandlerFunction() === functionName )) {
return trigger;
}
});
}
}
}