-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatus.gs
More file actions
316 lines (271 loc) · 6.96 KB
/
Status.gs
File metadata and controls
316 lines (271 loc) · 6.96 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
* 状態管理クラス
* どのプロジェクトまで読み込んだかの管理
*/
function Status(param) {
this.COLUMNS = {
HOST : 0,
PROJECT : 1,
STATUS : 2,
};
this.lenColumn = Object.keys(this.COLUMNS).length;
this.sheet = SpreadsheetApp.getActive().getSheetByName(param.sheet);
// 起点
this.index = this.sheet.getRange(1, 1);
// 初期化
this.statuses = [];
this.projects = {};
}
/**
* シート未設定なら、プロジェクトの情報でシートを初期化
* @protected
*/
Status.prototype.initialize = function() {
if (this.projects.length == 0) {
throw new Error('プロジェクト情報が設定されていません');
}
var projects;
var statuses = [];
// 値があるかどうかをチェック
var rowLast = this.sheet.getDataRange().getLastRow();
var range = this.index.offset(0, 0, rowLast, 2)
if (range.isBlank()) {
// 無ければ初期化
for (var host in this.projects) {
projects = this.projects[host];
for (var projectKey in projects) {
statuses.push([host, projectKey]);
}
}
this.index.offset(0, 0, statuses.length, (this.lenColumn - 1)).setValues(statuses);
}
};
/**
* スペース配列に状態を読み込み
* @protected
*/
Status.prototype.load = function() {
// シートの内容を読み込み
var rowLast = this.sheet.getDataRange().getLastRow();
this.statuses = this.index.offset(0, 0, rowLast, this.lenColumn).getValues();
};
/**
* 該当プロジェクトのfind関数
* @protected
* @return コールバックの戻り値をそのまま返却
*/
Status.prototype.findStatus = function(host, projectKey) {
var status;
for (var i in this.statuses) {
status = this.statuses[i];
if (status[this.COLUMNS.PROJECT] == projectKey) {
if (status[this.COLUMNS.HOST] == host) {
return status;
}
}
};
return undefined;
};
/**
* 状態配列を key -> status の連想配列形式を取得
* @protected
*/
Status.prototype.getHashStatus = function() {
var self = this;
var statuses = {};
this.statuses.forEach(function(status) {
// 全て1なら読み込み済み判定
statuses[status[self.COLUMNS.HOST]] = status[self.COLUMNS.STATUS];
});
return statuses;
};
/**
* 指定状態のスペースを返却
* @protected
*/
Status.prototype.getSpaces = function(isMarked) {
var spaces = [];
// 状態配列を連想配列にしたものを取得
var statuses = this.getHashStatus();
// スペースを検索
for (var host in statuses) {
// 処理済み
if (isMarked && statuses[host] == 1) {
spaces.push(host);
}
else if (statuses[host] != 1) {
spaces.push(host);
}
}
return spaces;
};
/**
* 指定スペース/状態のプロジェクトを返却
* @protected
*/
Status.prototype.getProjects = function(host, isMarked) {
var status;
var projects = [];
for (var i in this.statuses) {
status = this.statuses[i];
if (status[this.COLUMNS.HOST] == host) {
// 処理済み
if (isMarked && status[this.COLUMNS.STATUS] == 1) {
projects.push(status[this.COLUMNS.PROJECT]);
}
// 未処理
else if (status[this.COLUMNS.STATUS] != 1) {
projects.push(status[this.COLUMNS.PROJECT]);
}
}
}
return projects;
};
/**
* プロジェクトkey情報を保持
*/
Status.prototype.setProjects = function(host, keys) {
this.projects[host] = keys;
};
/**
* 状態配列にシートから状態を読み込んで同期
* @note setProjects が呼ばれていること
*/
Status.prototype.sync = function() {
// なければ初期化
this.initialize();
// シート読み込み
this.load();
};
/**
* シート状態とconfig状態のプロジェクト一致確認
* @note setProjects が呼ばれていること
*/
Status.prototype.equalProject = function() {
var host, project, status;
// シートだけにあるか、configに存在しないならエラー
for (var i in this.statuses) {
status = this.statuses[i];
host = status[this.COLUMNS.HOST];
project = status[this.COLUMNS.PROJECT];
if (this.projects[host][project] === undefined) {
return false;
}
}
return true;
};
/**
* 対象スペースが処理済みかどうかを判定
*/
Status.prototype.isMarkedSpace = function(host) {
// 状態配列を連想配列にしたものを取得
var statuses = this.getHashStatus();
// 未処理のスペース
if (statuses[host] != 1) {
// 無ければ未処理
return false;
}
// 処理済み
return true;
};
/**
* 対象プロジェクトが処理済みかどうかを判定
*/
Status.prototype.isMarkedPrject = function(host, projectKey) {
// 該当プロジェクトを検索
var status = this.findStatus(host, projectKey);
if (status != undefined) {
// 状態を判定
if (status[this.COLUMNS.STATUS] == 1) {
// 処理済み
return true;
}
}
};
/**
* 指定状態のスペース件数を返却
*/
Status.prototype.getCountSpace = function(isMarked) {
var spaces = this.getSpaces(isMarked);
return spaces.length;
};
/**
* 指定スペース/状態のプロジェクト件数を返却
*/
Status.prototype.getCountProject = function(host, isMarked) {
var projects = this.getProjects(host, isMarked);
return projects.length;
};
/**
* 全て読み込み済みかどうか
* @return 0 全て未処理
* @return 1 読み込み中
* @return 2 全て読み込み済み
* @return -1 それ以外(データが無い等)
*/
Status.prototype.getStatus = function() {
var isSet = false;
var isUnset = false;
var result = -1;
for (var i in this.statuses) {
if (this.statuses[i][this.COLUMNS.STATUS] == 1) {
isSet = true;
}
else {
isUnset = true;
}
};
if (isSet && isUnset) {
result = 1;
}
else if (isSet) {
result = 2;
}
else if (isUnset) {
result = 0;
}
return result;
};
/**
* 初回の読み込みかどうか
* = 全て未読み込みかどうかを判定
*/
Status.prototype.isFirst = function() {
for (var i in this.statuses) {
if (this.statuses[i][this.COLUMNS.STATUS] == 1) {
return false;
}
};
return true;
};
/**
* 全て読み込み済みかどうか
*/
Status.prototype.isComplete = function() {
for (var i in this.statuses) {
if (this.statuses[i][this.COLUMNS.STATUS] == 0) {
return false;
}
};
return true;
};
/**
* プロジェクトに処理完了済みを設定
*/
Status.prototype.mark = function(host, projectKey) {
// 該当プロジェクトを検索
var status = this.findStatus(host, projectKey);
if (status != undefined) {
// 状態を変更して、シートに同期
status[this.COLUMNS.STATUS] = 1;
this.index.offset(0, 0, this.statuses.length, this.lenColumn).setValues(this.statuses);
}
};
/**
* 初期化。シートをまっさらにする。
*/
Status.prototype.reset = function() {
// 一旦、シートをクリアにする
this.sheet.clear();
this.statuses = [];
};