-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstudentmatrix-mainsheet.js
More file actions
98 lines (84 loc) · 3.69 KB
/
studentmatrix-mainsheet.js
File metadata and controls
98 lines (84 loc) · 3.69 KB
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
/**
* @file
* Settings for selecting main sheet and changing which columns to use.
*/
StudentMatrix.plugins.mainsheet = {
name : 'Main sheet and column settings',
description : 'Allows changing which sheet to read student list from, and changing/viewing which columns are used by modules and plugins.',
version : '1.0',
required : true,
updateUrl : 'https://raw.github.com/Itangalo/studentmatrix/3.x/studentmatrix-mainsheet.js',
cell : 'D8',
dependencies : {
core : '3.2',
modules : {
settings : '1.0',
},
},
settings : {
mainSheetAndColumns : {
group : 'Main sheet settings',
options : {
mainSheet : {},
},
optionsBuilder : function(handler, container, defaults) {
var app = UiApp.getActiveApplication();
container.add(app.createHTML('<strong>Select sheet to use for student list</strong>'));
var mainSheet = app.createListBox().setName('mainSheet');
for (var sheet in SpreadsheetApp.getActiveSpreadsheet().getSheets()) {
mainSheet.addItem(SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheet].getName(), SpreadsheetApp.getActiveSpreadsheet().getSheets()[sheet].getName());
}
container.add(mainSheet);
handler.addCallbackElement(mainSheet);
container.add(app.createHTML('<strong>Columns used by StudentMatrix</strong>'));
var columns = StudentMatrix.getColumns();
var columnValues = StudentMatrix.getProperty('StudentMatrixColumns') || {};
var columnGrid = app.createGrid(Object.keys(columns).length, 2);
var columnSelectors = {};
var columnHeaders = StudentMatrix.mainSheet().getRange('1:1').getValues()[0];
var row = 0;
for (var column in columns) {
columnGrid.setText(row, 0, columns[column]);
columnSelectors[column] = app.createListBox().setName('column-' + column);
columnSelectors[column].addItem('add new column', null);
for (var i in columnHeaders) {
var number = parseInt(i) + 1;
columnSelectors[column].addItem(columnHeaders[i] + ' (' + number + ')', number);
}
if (columnValues[column] != undefined) {
columnSelectors[column].setSelectedIndex(parseInt(columnValues[column]));
}
handler.addCallbackElement(columnSelectors[column]);
columnGrid.setWidget(row, 1, columnSelectors[column]);
row++;
}
container.add(columnGrid);
},
// Read and store the settings for main sheet and columns to use.
optionsSaver : function(eventInfo) {
// Store main sheet name, and also ID for fallback (if name should change).
var mainSheet = eventInfo.parameter.mainSheet;
StudentMatrix.setProperty(mainSheet, 'StudentMatrixMainSheetName');
StudentMatrix.setProperty(SpreadsheetApp.getActiveSpreadsheet().getSheetByName(mainSheet).getSheetId(), 'StudentMatrixMainSheetID');
var columns = StudentMatrix.getColumns();
var columnMapping = {};
for (var column in columns) {
if (eventInfo.parameter['column-' + column] != null) {
columnMapping[column] = eventInfo.parameter['column-' + column];
}
}
StudentMatrix.setProperty(columnMapping, 'StudentMatrixColumns');
StudentMatrix.setUpColumns();
},
},
},
// Helper function to get a sheet by ID, from a spreadsheet.
getSheetByID : function(spreadsheet, sheetID) {
// This is a rather silly way of loading a sheet by ID, but I found no better.
for (var i in spreadsheet.getSheets()) {
if (spreadsheet.getSheets()[i].getSheetId() == sheetID) {
return spreadsheet.getSheets()[i];
}
}
},
};