SidebarJS.html
4.1 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
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script>
/**
* Run initializations on sidebar load.
*/
$(function() {
$('#formSubmitBtn').click(submit);
// Assign handler functions to sidebar elements here, if needed.
// Call the server here to retrieve any information needed to build
// the dialog, if necessary.
// Start polling for updates
poll(1000);
});
/**
* Poll a server-side function at the given interval, to have
* results passed to a successHandler callback.
*
* https://stackoverflow.com/a/24773178/1677912
*
* @param {Number} interval (optional) Time in ms between polls.
* Default is 1s (1000ms)
*/
function poll(interval) {
interval = interval || 1000;
//interval = 500;
setTimeout(function() {
google.script.run
.withSuccessHandler(updateFormValues)
.withFailureHandler(function (error) { //To-do! Failuer처리는 어떻게?
var div = document.getElementById('error');
div.innerHTML = "ERROR: " + error.message;
})
.getRange();
}, interval);
};
function validateRange(range, topHeader) {
// domain내의 범위인가 (top header제외)
const top = 2;
const outOfRange = 34;
if (top >= range.row || outOfRange <= range.row)
return {code: "error-04"};
//range column이 left header와 right header사이에 있는가
const leftHeader = 2;
const rightHeader = 35;
if (range.col <= leftHeader || range.col >= rightHeader){ //To-do! 고정값으로 할 것인가, sheet인식해서 값 변동시킬 것인가.
return {code: "error-02"};
}
//range가 1열로 이루어져있는가
if (range.width != 1)
return {code: "error-01"};
// 이미 예약된 항목과 겹치는가 //To-do! 백엔드에서 동시성 제어하도록 전환할 것
for (var i = 0; i < range.height; ++i) {
for (var j = 0; j < range.width; ++j) {
if (range.values[i][j] !== ""){
return {code: "error-05"};
}
}
}
//수용인원을 초과하는가
var lastIdx = topHeader.lastIndexOf("명")
var startIdx = topHeader.lastIndexOf("\n", lastIdx) + 1;
var capacity = parseInt(topHeader.substring(startIdx, lastIdx));
if (capacity < $('input[name=num-people]').val())
return {code: "error-03"};
return {code: ""};
}
// param: rangeInfo -> [{int col, int width, int height}, String topHeader's value (like 전정\n205\n202명)]
function updateFormValues([range, topHeader, date]) {
var ret = validateRange(range, topHeader);
if (ret.code === "") {
var parsed = topHeader.split("\n");
var classroom = parsed[0] + " " + parsed[1];
$('input[name=classroom]').val(classroom);
var start = 9 * 60 + 30 * (range.row - 3);
var stop = start + 30 * range.height;
var startStr = parseInt(start/60).toString() + ":" + ((start%60)<10 ? '0' + (start % 60).toString() : (start % 60).toString());
var stopStr = parseInt(stop/60).toString() + ":" + ((stop%60)<10 ? '0' + (stop % 60).toString() : (stop % 60).toString());
var timeInterval = startStr + "~" + stopStr;
$('input[name=time]').val(date + " " + timeInterval);
$('#errorCode').val(ret.code);
poll(1000);
return;
}
$('input[name=classroom]').val("");
$('input[name=time]').val("");
$('#errorCode').val(ret.code);
poll(1000);
};
function submit() {
// 변수들
var inputValues = {
profName: $('#formInputProfName').val(),
classCode: $('#formInputClassCode').val(),
className: $('#formInputClassName').val(),
numPeople: $('#formInputNumPeople').val(),
classRoom: $('#formInputClassRoom').val(),
time: $('#formInputTime').val()
};
var errorCode = $('#errorCode').val();
google.script.run
.withSuccessHandler(function () {
})
.withFailureHandler(function (error) {
console.log('[submit() Error]', error);
})
.submit(inputValues, errorCode);
}
</script>