-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropdown.gs
287 lines (227 loc) · 9.63 KB
/
Dropdown.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
// global variables:
var questions = [];
var answers = [];
var questionStyle = {};
questionStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Arial';
questionStyle[DocumentApp.Attribute.FONT_SIZE] = 14;
questionStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
questionStyle[DocumentApp.Attribute.SPACING_BEFORE] = 1;
questionStyle[DocumentApp.Attribute.SPACING_AFTER] = 0;
var infoStyle = {};
infoStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Arial';
infoStyle[DocumentApp.Attribute.FONT_SIZE] = 9;
infoStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
infoStyle[DocumentApp.Attribute.SPACING_BEFORE] = 0;
infoStyle[DocumentApp.Attribute.SPACING_AFTER] = 1;
var creditStyle = {};
creditStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Arial';
creditStyle[DocumentApp.Attribute.FONT_SIZE] = 8;
creditStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#666666';
creditStyle[DocumentApp.Attribute.SPACING_BEFORE] = 0;
creditStyle[DocumentApp.Attribute.SPACING_AFTER] = 1;
var titleStyle = {};
titleStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Arial';
titleStyle[DocumentApp.Attribute.FONT_SIZE] = 18;
titleStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
var headerStyle = {};
headerStyle[DocumentApp.Attribute.FONT_FAMILY] = 'Arial';
headerStyle[DocumentApp.Attribute.FONT_SIZE] = 11;
headerStyle[DocumentApp.Attribute.FOREGROUND_COLOR] = '#000000';
/**
* Creates a menu entry in the Google Docs UI when the document is opened.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onOpen trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode.
*/
function onOpen(e) {
DocumentApp.getUi().createAddonMenu()
.addItem('Text Field', 'showTextFieldSidebar')
.addItem('Multiple Choice', 'showMultipleChoiceSidebar')
//.addItem('Grading', 'showGradingSidebar')
.addItem('Header', 'showHeaderSidebar')
.addToUi();
}
/**
* Runs when the add-on is installed.
* This method is only used by the regular add-on, and is never called by
* the mobile add-on version.
*
* @param {object} e The event parameter for a simple onInstall trigger. To
* determine which authorization mode (ScriptApp.AuthMode) the trigger is
* running in, inspect e.authMode. (In practice, onInstall triggers always
* run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or
* AuthMode.NONE.)
*/
function onInstall(e) {
onOpen(e);
}
/**********************************************************
* These showSidebar-type functions are utilized to open up
* the sidebar containing the element's user interface.
**********************************************************/
function showTextFieldSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('textfield')
.setTitle('Text Field');
DocumentApp.getUi().showSidebar(ui);
}
function showMultipleChoiceSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('multiplechoice')
.setTitle('Multiple Choice');
DocumentApp.getUi().showSidebar(ui);
}
function showGradingSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('grading')
.setTitle('Grading');
DocumentApp.getUi().showSidebar(ui);
}
function showHeaderSidebar() {
var ui = HtmlService.createHtmlOutputFromFile('header')
.setTitle('Header');
DocumentApp.getUi().showSidebar(ui);
}
/*************************************************
* This function creates the Text Field question.
**************************************************/
function addTextField(title,response,answer,points,is_graded) {
/*************************************************
* Obtains the document and gets the body section
* of the given document.
**************************************************/
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
/*************************************************
* Stores all the given variables necessary to
* customize the text-field question.
**************************************************/
var question = title;
var ans = answer;
var points = points;
var lines = response;
if(is_graded) {
questions.push(question);
answers.push(ans);
body.appendParagraph(question).setAttributes(questionStyle).appendText(' (' + points + ' pts)').setBold(true);
}else{
/*************************************************
* Creates the initial question, assigns the points,
* and adds the information text.
**************************************************/
body.appendParagraph(question).setAttributes(questionStyle).setBold(true);
}
/*************************************************
* Creates a table based on the variables given.
**************************************************/
var table = body.appendTable();
for(var i = 0; i < lines; i++){
var tr = table.appendTableRow();
var td = tr.appendTableCell();
}
table.setAttributes(infoStyle);
if(is_graded){ // adds answer sheet if needed
for(var i = 0; i < questions.length; i++){
body.appendParagraph(questions[i]).setAttributes(questionStyle);
body.appendListItem(answers[i]).setAttributes(infoStyle).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);;
}
}
/*************************************************
* Saves and closes the document.
**************************************************/
doc.saveAndClose();
}
/*************************************************
* This function creates the Multiple Choice question.
**************************************************/
function addMultipleChoice(question, options, questionNumStyle, is_graded, points, answerNum) {
/*************************************************
* Obtains the document and gets the body section
* of the given document.
**************************************************/
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
/*************************************************
* Creates the initial question, assigns the points,
* and adds the information text.
**************************************************/
if(is_graded) body.appendParagraph(question).setAttributes(questionStyle).appendText(' (' + points + ' pts)').setBold(true);
else body.appendParagraph(question).setAttributes(questionStyle);
/************************************************
* Creates bulleted list based on variable given.
*************************************************/
if(is_graded) questions.push(question);
var optionsAr = options.split(",");
var optionsCnt = 0;
var listId;
var item;
for(var idx in optionsAr){
if(questionNumStyle == 1)
item = body.appendListItem(optionsAr[idx]).setAttributes(infoStyle).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);
else
item = body.appendListItem(optionsAr[idx]).setAttributes(infoStyle).setGlyphType(DocumentApp.GlyphType.LATIN_UPPER);
if(optionsCnt == 0){
item1 = item;
} else
item.setListId(item1);
optionsCnt++;
if(optionsCnt == answerNum && is_graded){
answers.push(optionsAr[idx]);
}
}
if(is_graded){ // adds answer sheet if needed
for(var i = 0; i < questions.length; i++){
body.appendParagraph(questions[i]).setAttributes(questionStyle);
body.appendListItem(answers[i]).setAttributes(infoStyle).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);;
}
}
/*************************************************
* Saves and closes the document.
**************************************************/
doc.saveAndClose();
}
/*************************************************
* This function creates the Header
**************************************************/
function addHeader(month, year, day, title, name) {
/*************************************************
* Obtains the document and gets the header section
* of the given document.
**************************************************/
var doc = DocumentApp.getActiveDocument();
try{
var header = doc.addHeader();
} catch(e) {
var header = doc.getHeader();
header.clear();
}
/*************************************************
* Creates the header by adding name, date, and title
**************************************************/
header.appendParagraph("Name: "+name+"\t\t\t\t\t\t\t\t").setAttributes(headerStyle).appendText("Date: "+month+"/"+day+"/"+year);
header.appendParagraph(title).setAttributes(titleStyle).setAlignment(DocumentApp.HorizontalAlignment.CENTER);
/*************************************************
* Saves and closes the document.
**************************************************/
doc.saveAndClose();
}
/*************************************************
* This function generates the answer sheet for the quiz.
**************************************************/
function addAnswerSheet(){
/*************************************************
* Obtains the document and gets the body section
* of the given document.
*************************************************/
var doc = DocumentApp.getActiveDocument();
var body = doc.getBody();
/* body.appendParagraph(questions.length);
/******************************************************************
* Loops through the questions and answers and adds it to the sheet
******************************************************************/
for(var i = 0; i < questions.length; i++){
body.appendParagraph(questions[i]).setAttributes(questionStyle);
body.appendListItem(answers[i]).setAttributes(infoStyle).setGlyphType(DocumentApp.GlyphType.HOLLOW_BULLET);;
}
doc.saveAndClose();
}