質問編集履歴

1

変更点だと思うコードを追加しました。

2018/01/16 10:34

投稿

yumatakei
yumatakei

スコア20

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,307 @@
17
17
  このように横に1日何度かの出勤退勤を全て保存できるようにしたいと思っていますが、javascript初心者の私には難しいです。
18
18
 
19
19
  どなたかご教授をお願い致します。
20
+
21
+
22
+
23
+ 変更点はこのコードだと考えています...
24
+
25
+
26
+
27
+ ```javascript
28
+
29
+ loadGSTimesheets = function () {
30
+
31
+ var GSTimesheets = function(spreadsheet, settings) {
32
+
33
+ this.spreadsheet = spreadsheet;
34
+
35
+ this.settings = settings;
36
+
37
+ this._sheets = {};
38
+
39
+
40
+
41
+ this.scheme = {
42
+
43
+ columns: [
44
+
45
+ { name: '日付' },
46
+
47
+ { name: '出勤' },
48
+
49
+ { name: '退勤' },
50
+
51
+ { name: 'ノート' },
52
+
53
+ ],
54
+
55
+ properties: [
56
+
57
+ { name: 'DayOff', value: '土,日', comment: '← 月,火,水みたいに入力してください。アカウント停止のためには「全部」と入れてください。'},
58
+
59
+ ]
60
+
61
+ };
62
+
63
+ };
64
+
65
+
66
+
67
+ GSTimesheets.prototype._getSheet = function(username) {
68
+
69
+ if(this._sheets[username]) return this._sheets[username];
70
+
71
+
72
+
73
+ var sheet = this.spreadsheet.getSheetByName(username);
74
+
75
+ if(!sheet) {
76
+
77
+ sheet = this.spreadsheet.insertSheet(username);
78
+
79
+ if(!sheet) {
80
+
81
+ throw "エラー: "+sheetName+"のシートが作れませんでした";
82
+
83
+ }
84
+
85
+ else {
86
+
87
+ // 中身が無い場合は新規作成
88
+
89
+ if(sheet.getLastRow() == 0) {
90
+
91
+ // 設定部の書き出し
92
+
93
+ var properties = [["Properties count", this.scheme.properties.length, null]];
94
+
95
+ this.scheme.properties.forEach(function(s) {
96
+
97
+ properties.push([s.name, s.value, s.comment]);
98
+
99
+ });
100
+
101
+ sheet.getRange("A1:C"+(properties.length)).setValues(properties);
102
+
103
+
104
+
105
+ // ヘッダの書き出し
106
+
107
+ var rowNo = properties.length + 2;
108
+
109
+ var cols = this.scheme.columns.map(function(c) { return c.name; });
110
+
111
+ sheet.getRange("A"+rowNo+":"+String.fromCharCode(65 + cols.length - 1)+rowNo).setValues([cols]);
112
+
113
+ }
114
+
115
+ //this.on("newUser", username);
116
+
117
+ }
118
+
119
+ }
120
+
121
+
122
+
123
+ this._sheets[username] = sheet;
124
+
125
+
126
+
127
+ return sheet;
128
+
129
+ };
130
+
131
+
132
+
133
+ GSTimesheets.prototype._getRowNo = function(username, date) {
134
+
135
+ if(!date) date = DateUtils.now();
136
+
137
+ var rowNo = this.scheme.properties.length + 4;
138
+
139
+ var startAt = DateUtils.parseDate(this.settings.get("開始日"));
140
+
141
+ var s = new Date(startAt[0], startAt[1]-1, startAt[2], 0, 0, 0);
142
+
143
+ rowNo += parseInt((date.getTime()-date.getTimezoneOffset()*60*1000)/(1000*24*60*60)) - parseInt((s.getTime()-s.getTimezoneOffset()*60*1000)/(1000*24*60*60));
144
+
145
+ return rowNo;
146
+
147
+ };
148
+
149
+
150
+
151
+ GSTimesheets.prototype.get = function(username, date) {
152
+
153
+ var sheet = this._getSheet(username);
154
+
155
+ var rowNo = this._getRowNo(username, date);
156
+
157
+ var row = sheet.getRange("A"+rowNo+":"+String.fromCharCode(65 + this.scheme.columns.length - 1)+rowNo).getValues()[0].map(function(v) {
158
+
159
+ return v === '' ? undefined : v;
160
+
161
+ });
162
+
163
+
164
+
165
+ return({ user: username, date: row[0], signIn: row[1], signOut: row[2], note: row[3] });
166
+
167
+ };
168
+
169
+
170
+
171
+ GSTimesheets.prototype.set = function(username, date, params) {
172
+
173
+ var row = this.get(username, date);
174
+
175
+ _.extend(row, _.pick(params, 'signIn', 'signOut', 'note'));
176
+
177
+
178
+
179
+ var sheet = this._getSheet(username);
180
+
181
+ var rowNo = this._getRowNo(username, date);
182
+
183
+
184
+
185
+ var data = [DateUtils.toDate(date), row.signIn, row.signOut, row.note].map(function(v) {
186
+
187
+ return v == null ? '' : v;
188
+
189
+ });
190
+
191
+ sheet.getRange("A"+rowNo+":"+String.fromCharCode(65 + this.scheme.columns.length - 1)+rowNo).setValues([data]);
192
+
193
+
194
+
195
+ return row;
196
+
197
+ };
198
+
199
+
200
+
201
+ GSTimesheets.prototype.getUsers = function() {
202
+
203
+ return _.compact(_.map(this.spreadsheet.getSheets(), function(s) {
204
+
205
+ var name = s.getName();
206
+
207
+ return String(name).substr(0, 1) == '_' ? undefined : name;
208
+
209
+ }));
210
+
211
+ };
212
+
213
+
214
+
215
+ GSTimesheets.prototype.getByDate = function(date) {
216
+
217
+ var self = this;
218
+
219
+ return _.map(this.getUsers(), function(username) {
220
+
221
+ return self.get(username, date);
222
+
223
+ });
224
+
225
+ };
226
+
227
+
228
+
229
+ // 休みの曜日を数字で返す
230
+
231
+ GSTimesheets.prototype.getDayOff = function(username) {
232
+
233
+ var sheet = this._getSheet(username);
234
+
235
+ return DateUtils.parseWday(sheet.getRange("B2").getValue());
236
+
237
+ };
238
+
239
+
240
+
241
+ return GSTimesheets;
242
+
243
+ };
244
+
245
+
246
+
247
+ ```
248
+
249
+
250
+
251
+ ```javascript
252
+
253
+ // 出勤
254
+
255
+ Timesheets.prototype.actionSignIn = function(username, message) {
256
+
257
+ if(this.datetime) {
258
+
259
+ var data = this.storage.get(username, this.datetime);
260
+
261
+ if(!data.signIn || data.signIn === '-') {
262
+
263
+ this.storage.set(username, this.datetime, {signIn: this.datetime});
264
+
265
+ this.responder.template("出勤", username, this.datetimeStr);
266
+
267
+ }
268
+
269
+ else {
270
+
271
+ // 更新の場合は時間を明示する必要がある
272
+
273
+ if(!!this.time) {
274
+
275
+ this.storage.set(username, this.datetime, {signIn: this.datetime});
276
+
277
+ this.responder.template("出勤更新", username, this.datetimeStr);
278
+
279
+ }
280
+
281
+ }
282
+
283
+ }
284
+
285
+ };
286
+
287
+
288
+
289
+ // 退勤
290
+
291
+ Timesheets.prototype.actionSignOut = function(username, message) {
292
+
293
+ if(this.datetime) {
294
+
295
+ var data = this.storage.get(username, this.datetime);
296
+
297
+ if(!data.signOut || data.signOut === '-') {
298
+
299
+ this.storage.set(username, this.datetime, {signOut: this.datetime});
300
+
301
+ this.responder.template("退勤", username, this.datetimeStr);
302
+
303
+ }
304
+
305
+ else {
306
+
307
+ // 更新の場合は時間を明示する必要がある
308
+
309
+ if(!!this.time) {
310
+
311
+ this.storage.set(username, this.datetime, {signOut: this.datetime});
312
+
313
+ this.responder.template("退勤更新", username, this.datetimeStr);
314
+
315
+ }
316
+
317
+ }
318
+
319
+ }
320
+
321
+ };
322
+
323
+ ```