-
Notifications
You must be signed in to change notification settings - Fork 0
/
code.js
179 lines (161 loc) · 4.77 KB
/
code.js
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
//rangeの列のリスト
//0スタートなので、スプレッドシート上で利用するときは+1すること
var COL = {
TITLE: 0,
CAL: 1,
ALLDAY: 2,
START: 3,
END: 4,
LOOP: 5,
LOOP_END: 6,
DESCRIPTION: 7
};
var eventUrl = 'https://calendar.google.com/calendar/r/eventedit/';
function onOpen(e) {
Logger.log("onOpen");
SpreadsheetApp.getUi().createAddonMenu()
.addItem('起動', 'showSidebar')
.addToUi();
}
function onInstall(e) {
Logger.log("onInstall");
onOpen(e);
}
function showSidebar() {
Logger.log("showSidebar");
var ui = HtmlService.createHtmlOutputFromFile('sidebar').setTitle('ScheduleGenerator');
SpreadsheetApp.getUi().showSidebar(ui);
}
/**
* 予定入力用シートの追加
*
*/
function addSheet() {
//ScriptPropertiesにテンプレートのスプレッドシートのIDが保存してあるので、それを取得
var ssId = PropertiesService.getScriptProperties().getProperty('template');
var ss = SpreadsheetApp.getActiveSpreadsheet();
//現在のスプレッドシートにテンプレートをコピー
var sheet = SpreadsheetApp.openById(ssId).getSheetByName('template').copyTo(ss);
sheet.activate();
updateCalendarList();
return sheet.getName();
}
//カレンダー一覧を取得
/**
* カレンダー一覧情報の更新
*
* @returns カレンダー一覧情報のMAP
*/
function updateCalendarList() {
//自分のカレンダー一覧を取得
var calendars = CalendarApp.getAllOwnedCalendars();
var calendarList = {};
for (var i in calendars) {
//あとでカレンダー名からカレンダーobjを取得しやすいようにmapを作成
calendarList[calendars[i].getName()] = calendars[i];
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(2, 3, sheet.getMaxRows() - 1, 1);
var calendarNames = Object.keys(calendarList);
// カレンダー選択列に入力規則としてカレンダー名を指定
var rule = SpreadsheetApp.newDataValidation().requireValueInList(calendarNames, true).setAllowInvalid(false).build();
range.setDataValidation(rule);
return calendarList;
}
/**
* 予定の一括登録
*
*/
function generateEvents() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(2, 2, sheet.getLastRow() - 1, sheet.getLastColumn() - 1);
var values = range.getValues();
var checkBox = sheet.getRange(2, 1, sheet.getLastRow() - 1, 1);
var results = checkBox.getValues();
var result = 0;
var calendarList = updateCalendarList();
for (var i in values) {
if (results[i][0] == false) {
var value = values[i];
var event = generateEvent(
calendarList[value[COL.CAL]],
value[COL.TITLE],
value[COL.START],
value[COL.END],
value[COL.ALLDAY],
value[COL.DESCRIPTION],
value[COL.LOOP],
value[COL.LOOP_END]
);
if (event != null) {
results[i][0] = true;
result.success++;
} else {
results[i][0] = false;
}
}
}
checkBox.setValues(results);
return result;
}
function generateEvent(calendar, title, start, end, allDay, description, loop, loopEnd) {
//タイトル・開始・終了は必須
if (calendar == '' || title == '' || start == '' || end == '') {
return null;
}
try {
//var calendar = CalendarApp.getDefaultCalendar();
//繰り返しの予定かどうかで分岐
if (loop == '') {
if (allDay == false) {
//繰り返しなし・終日でない
return calendar.createEvent(title, start, end, { 'description': description });
} else if (allDay == true) {
//繰り返しなし・終日
return calendar.createAllDayEvent(title, start, end, { 'description': description });
}
} else if (loop != '') {
//繰り返しルールの作成
var recurrence = buildRecurrenceRule(loop, loopEnd);
if (allDay == false) {
//繰り返し・終日でない
return calendar.createEventSeries(title, start, end, recurrence, { 'description': description });
} else if (allDay == true) {
//繰り返し・終日
return calendar.createAllDayEventSeries(title, start, recurrence, { 'description': description });
}
}
} catch (error) {
return null;
}
}
function buildRecurrenceRule(loop, end) {
var recurrence = CalendarApp.newRecurrence();
switch (loop) {
case '毎日':
recurrence = recurrence.addDailyRule();
break;
case '毎週':
recurrence = recurrence.addWeeklyRule();
break;
case '毎月':
recurrence = recurrence.addMonthlyRule();
break;
case '毎年':
recurrence = recurrence.addYearlyRule();
break;
}
if (end != '') {
recurrence.until(end);
}
return recurrence;
}
function showModal(title, body, width, height) {
var htmlOutput = HtmlService
.createHtmlOutput(body)
.setWidth(width)
.setHeight(height);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, title);
}