-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogging.js
128 lines (123 loc) · 4.41 KB
/
Logging.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
function testConsole () {
console.log('hello');
}
function logEvent (configTable, event, actionResults, extraConfig) {
// DEBUG CODE
//msg = 'logEvent('+JSON.stringify(configTable)+','+JSON.stringify(event)+',';
//msg += JSON.stringify(actionResults)+','+JSON.stringify(extraConfig)+')';
//emailError (msg, "NO ERROR",{'subject':'logEvent debug info'})
// END DEBUG CODE
console.log('configTable %s; event %s results %s config %s',configTable,event,actionResults,extraConfig);
var settings = lookupFields(configTable,getResponseItems(event.response, actionResults));
settings.Triggers = {};
for (var key in actionResults) {
settings[key+'Action'] = actionResults[key]
settings.Triggers[key] = actionResults[key]
}
for (var key in extraConfig) {
settings[key] = extraConfig[key];
}
settings.ResponseId = event.response.getId()
try {
var sheet = getSheetById(SpreadsheetApp.openById(settings.SpreadsheetId),settings.SheetId);
}
catch (err) {
emailError('Unable to fetch sheet '+settings.SpreadsheetId+' + '+settings.SheetId,
err)
throw err;
}
var idCol = configTable.idCol ? configTable.idCol : undefined
var table = Table(sheet.getDataRange(),idCol);
//Logger.log('Updating row with %s',shortStringify(settings));
//emailError('logEvent updating table '+shortStringify(table)+'row with settings: '+shortStringify(settings),'No error'); // DEBUG
try {
table.updateRow(settings) // we just push our settings -- the set up of the table then becomes the key...
}
catch (err) {
emailError('Error updating '+settings.SpreadsheetId+' with '+JSON.stringify(settings), err);
throw err;
}
return settings
}
function createLogHeaderRow (form) {
row = ['ResponseId','Timestamp']
ignoreTypes = [FormApp.ItemType.SECTION_HEADER,
FormApp.ItemType.PAGE_BREAK]
//form = FormApp.openById() // Comment me
form.getItems().forEach( function (item) {
if (ignoreTypes.indexOf(item.getType()) == -1) {
if (row.indexOf(item.getTitle()) == -1) {
// Don't add duplicate header names.
// We assume that duplicate names are part of
// multi-page flows that are intended to provide
// one field value from two different points in a
// flow through a form as part of a UI
row.push(item.getTitle());
}
}
}) // end forEach item
return row
}
function setupLoggingSheet (form, logSheet) {
var configOptions = {
}
if (! logSheet) {
var spreadsheet = SpreadsheetApp.create("Log");
var logSheet = spreadsheet.getActiveSheet();
var headerRow = createLogHeaderRow(form)
headerRow.forEach(function (r) {configOptions[r]='%'+r});
logSheet.appendRow(headerRow)
} // end new-sheet set-up
else {
// If there is an existing sheet... grab the first row...
logSheet = SpreadsheetApp.getActiveSheet() // comment me
var headers = logSheet.getRange(1,1,1,logSheet.getLastColumn());
headers.forEach( function (i) {
if (getItemByTitle(form,i)) {
configOptions[i]='%'+i;
}
else {
configOptions[i]='';
}
})
} // end pre-existing sheet set-up
// Set up ID and Spreadsheet options
configOptions['SheetId'] = logSheet.getSheetId();
configOptions['SpreadsheetId'] = logSheet.getParent().getId();
return configOptions
}
function testLog () {
fakeEvent = {
response : {
getItemResponses : function () { return [
{getItem : function () { return {getTitle : function () { return 'Test Item'}}},
getResponse : function () {return 'testValue'},
}
]},
getTimestamp : function () { return new Date() },
getRespondentEmail : function () { return '[email protected]'},
getId : function () {
// return (Math.random()*100000).toFixed(0);
return 79722
},
}
}
configTable = {
'TestLookup':'%Test Item',
'SpreadsheetId':'1dJZALNnN86MIt2UIXhpoq7Gd2oFuHMqxKY2-763LSEI',
'SheetId':'0',
'idCol':'ResponseId',
}
extraConfig = {'Foo':'Bear'}
actionResults = {}
logEvent(configTable,fakeEvent,actionResults,extraConfig);
}
function testIDWackiness () {
var ss = SpreadsheetApp.openById('1U2MpGDV9RmczNYJ38z4D-5Pps7_wQlhl_CeSdMVhQRk');
var sheet = ss.getActiveSheet();
var table = Table(sheet.getDataRange(),'ResponseId');
table.updateRow(
{'ResponseId':'2_ABaOnufyOAOCS9OEtiO00cDjpvhAQhEM_QnLrAFMJAdHoBX7koD3EugQGjG0pQ',
'Signature':'Test Update From Script!'}
);
}