-
Notifications
You must be signed in to change notification settings - Fork 67
/
form.js
135 lines (105 loc) · 3.24 KB
/
form.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
// form obj
var form = FormApp.getActiveForm();
// [Forms](#)
// * [Managing Form Files](#)
// * [Get Form Id](#)
// * [Utility Functions for Forms](#)
// * [Array of Form Items](#)
// * [Get Form Item by Name](#)
// * [Set Item Choices](#)
// * [Get Destination Sheet](#)
// * [Get Last Form Response](#)
function testEverything() {}
// Forms
// - Form Management
// -- FLAG --
// -- Get Form Id
function formId() {
var _id = FormApp.getActiveForm().getId();
}
Logger.log(formId());
// -- Array of Form Items
function arrItemsIn(formObj) {
var _arr = [];
var items = formObj.getItems()
for (var i = 0; i < items.length; i++) {
var j = {};
j.index = i;
j.title = items[i].getTitle();
j.id = items[i].getId();
j.type = items[i].getType();
j.item = items[i];
_arr.push(j);
}
return _arr;
}
var items = arrItemsIn(form);
// Logger.log(items);
// -- Get Form Item by Name
// note: from source.js
function findObjValIn(arrObj, pQuery, val, pReturn) {
for (var i = 0; i < arrObj.length; i++) {
var obj = arrObj[i];
for (var prop in obj) {
if (obj.hasOwnProperty(pQuery) && prop == pQuery && obj[prop] == val) {
return obj[pReturn];
}
}
}
}
// var ex_fovi = findObjValIn(items, "title", "Multiple Choice Example", "item");
// Logger.log(form.getItemById(ex_fovi).getTitle());
// -- Set Item Choices
var choices = ["1", "2", "3", "4", "5"];
// --- Multiple Choice
var multi = findObjValIn(items, "title", "Multiple Choice Example", "item").asMultipleChoiceItem();
multi.setChoiceValues(choices);
// --- Dropdown
var drop = findObjValIn(items, "title", "Dropdown Example", "item").asListItem();
drop.setChoiceValues(choices);
// --- Checkbox
var chkbox = findObjValIn(items, "title", "Checkbox Example", "item").asCheckboxItem();
chkbox.setChoiceValues(choices);
// -- Get Destination Sheet
// var destSheet = SpreadsheetApp.openById(form.getDestinationId());
// Logger.log(destSheet.getName());
// - Form Responses
// -- Get Last Form Response
// BUG FIX: form is a global object in this sheet...not actually using argument...
function lastResponse(formObj) {
var all = formObj.getResponses();
var last = all[all.length - 1];
var rsps = last.getItemResponses();
var j = {};
for (var i = 0; i < rsps.length; i++) {
var rsp = rsps[i];
var prop = rsp.getItem().getTitle();
var val = rsp.getResponse();
if (val !== "") {
j[prop] = val;
} else {
j[prop] = undefined;
}
}
j.email = last.getRespondentEmail();
j.timestamp = last.getTimestamp();
return j;
}
// update in cheat-sheet?
// function lastFormResponse(form) {
// var allResponses = form.getResponses();
// var lastResponse = allResponses[allResponses.length - 1];
// var itemResponses = lastResponse.getItemResponses();
// var result = {};
// for (var i = 0; i < itemResponses.length; i++) {
// var item = itemResponses[i];
// var property = item.getItem().getTitle();
// var value = item.getResponse();
// result[property] = value;
// }
// result["Email Address"] = lastResponse.getRespondentEmail();
// result.Timestamp = lastResponse.getTimestamp();
// return result;
// }
// var last = lastResponse(form);
// Logger.log(last);