-
Notifications
You must be signed in to change notification settings - Fork 10
/
accessors.gs
162 lines (123 loc) · 4.85 KB
/
accessors.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
// File: accessors.gas
// Description:
// This file contains functions that access data in the
// 'Student Submissions' and 'Grades' sheets.
// Functions in this file are meant to centralize and obfuscate private
// information about where different fields (like hidden rows) are stored.
// Other functions that simply make spreadsheet access easier
// (e.g. getCellValue), but which don't utilize private info, should be
// placed in the 'utilities.gas' file instead of this one.
// getNumQuestionsFromSubmissions:
// Given a reference to the submissions sheet, calculates and returns the number
// of questions that were asked in the assignment, including the implicit
// "Timestamp" question.
function getNumQuestionsFromSubmissions(subm_sheet)
{
// Write the question values into an array.
var n_ques = subm_sheet.getLastColumn();
var question_vals = singleRowToArray(subm_sheet, 1, n_ques, false);
// Remove any blank columns at the end, which may have been created by the user
// rearranging the columns.
var i = question_vals.length - 1;
for (; i >= 0; i--, n_ques--)
{
if (question_vals[i] !== "")
{
break;
}
}
return n_ques;
}
function getQuestionValsFromSubmissions(subm_sheet)
{
var n_ques = subm_sheet.getLastColumn();
var num_ques = getNumQuestionsFromSubmissions(subm_sheet);
var question_vals = singleRowToArray(subm_sheet, 1, num_ques, false);
question_vals[0] = langstr("FLB_STR_GRADE_STEP2_LABEL_SUBMISSION_TIME");
return question_vals;
}
// getSheetWithSubmissions:
// Finds the sheet in the active spreadsheet with the submissions from the form,
// and returns an instance of it. If not found, returns null.
function getSheetWithSubmissions(ss)
{
var sheet = getSheetWithSubmissionsInternal(ss);
// handle GAS bug
if (sheet === null)
{
// Try again. may be bug in GAS. Line below prevents occurence of this bug:
// https://code.google.com/p/google-apps-script-issues/issues/detail?id=2559
ss.setActiveSheet(SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]);
}
sheet = getSheetWithSubmissionsInternal(ss);
//Debug.assert(sheet !== null, "getSheetWithSubmissions() - can't open submissions sheet");
return sheet;
}
function getSheetWithSubmissionsInternal(ss)
{
var up = PropertiesService.getUserProperties();
var sheet = ss.getSheetByName(langstr("FLB_STR_SHEETNAME_STUD_SUBM"));
if (sheet === null)
{
// could be that user switch languages since older version.
// just check with old (pre v3.0) name ("Student Submissions")
// for backwards compatability.
sheet = ss.getSheetByName(gbl_subm_sheet_name);
// if not english, then rename old style name to localized
// version.
var lang_id = up.getProperty(USER_PROP_LANGUAGE_ID);
if ((sheet != null) && (lang_id != null) && (lang_id != 'en-us'))
{
sheet.setName(langstr("FLB_STR_SHEETNAME_STUD_SUBM"));
}
}
return sheet;
}
// TODO_AJR - This gets called a lot, leaving lots of places for
// checks for whether or not a grades sheet exists. This needs
// reviewing.
// getSheetWithGrades:
// Finds the sheet in the active spreadsheet with the grades,
// and returns an instance of it. If not found, returns null.
function getSheetWithGrades(ss)
{
var sheet = getSheetWithGradesInternal(ss);
if (sheet === null)
{
// Try again. may be bug in GAS. Line below prevents occurence of this bug:
// https://code.google.com/p/google-apps-script-issues/issues/detail?id=2559
ss.setActiveSheet(SpreadsheetApp.getActiveSpreadsheet().getSheets()[0]);
}
sheet = getSheetWithGradesInternal(ss);
return sheet;
}
function getSheetWithGradesInternal(ss)
{
var up = PropertiesService.getDocumentProperties();
var sheet = ss.getSheetByName(langstr("FLB_STR_SHEETNAME_GRADES"));
if (sheet === null)
{
// could be that user switch languages since older version.
// just check with old (pre v3.0) name ("Student Submissions")
// for backwards compatability.
sheet = ss.getSheetByName(gbl_grades_sheet_name);
// if not english, then rename old style name to localized
// version.
var lang_id = up.getProperty(USER_PROP_LANGUAGE_ID);
if ((sheet != null) && (lang_id != null) && (lang_id != 'en-us'))
{
sheet.setName(langstr("FLB_STR_SHEETNAME_GRADES"));
}
}
return sheet;
}
function gotSheetWithGrades(ss)
{
return getSheetWithGrades(ss) !== null;
} // gotSheetWithGrades()
function getSheetWithCategories(ss)
{
var category_sheet_name = langstr("FLB_STR_CATEGORIES_SHEET_NAME");
var sheet = ss.getSheetByName(category_sheet_name);
return sheet; // null if not present.
}