-
Notifications
You must be signed in to change notification settings - Fork 2
/
sync_calendar.gs
290 lines (236 loc) · 8.51 KB
/
sync_calendar.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
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
function onEdit(e) {
Logger.log("onedit");
let range = e.range; // 從事件物件 e 中取出了被編輯的單元格範圍(Range),並將它存放在變數 range 中
let sheet = range.getSheet(); // 使用 getSheet 方法,取得了被編輯的單元格所在的工作表(Sheet),並將它存放在變數 sheet 中。
let row = range.getRow(); // 使用 getRow 方法,取得了被編輯的單元格的「行數」(水平),並將它存放在變數 row 中。
console.log("Sheet: " + sheet.getName());
if (sheet.getName() != "貼文表單") {
return;
}
_updateScheduledPosts(range, sheet, row);
_insertDashboardLink(sheet, row, "成效報表");
}
function checkCellPlace(c) {
if ((c >= 65 && c <= 68) || (c == 79))
return true;
else
return false;
}
function getRawDatas(sheet, start_row, numRows) {
let row_data = sheet.getRange(start_row, 1, numRows, 15).getValues();
return row_data;
}
function getDocLinks(sheet, start_row, numRows) {
let row_data = sheet.getRange(start_row, 12, numRows).getRichTextValues();
return row_data;
}
function getRowData(row_data, row) {
let team = row_data[0];
let client = row_data[1];
let postDate = row_data[2];
let title = row_data[3];
let index = row_data[4];
let status = row_data[14];
if (
typeof title === 'undefined' || title.length === 0 ||
typeof postDate === 'undefined' || postDate.length === 0 ||
typeof client === 'undefined' || client.length === 0 ||
typeof team === 'undefined' || team.length === 0
) {
return null;
}
let calPlace = matchDate(postDate);
let color = matchColor(status);
return {
'team': team,
'client': client,
'row': calPlace[0],
'col': calPlace[1],
'title': title,
'index': index,
'status': status,
'color': color
}
}
function reflashCalendar() {
const START_ROW = 3; // 起始行數
const POST_FORM_SHEET_NAME = '貼文表單';
const CALENDAR_SHEET_NAME = '貼文日曆';
const MAX_COLUMNS = 10; // 需要處理的最大列數
// 獲取工作表
const spreadsheet = SpreadsheetApp.getActive();
const postFormSheet = spreadsheet.getSheetByName(POST_FORM_SHEET_NAME);
const calendarSheet = spreadsheet.getSheetByName(CALENDAR_SHEET_NAME);
const lastRowPostForm = postFormSheet.getLastRow();
const lastRowCalendar = calendarSheet.getLastRow();
// 取出 貼文表單 的數據
const postFormData = getRawDatas(postFormSheet, START_ROW, lastRowPostForm - START_ROW + 1);
const docLinks = getDocLinks(postFormSheet, START_ROW, lastRowPostForm - START_ROW + 1);
let reqDatas = [];
// 處理每一行數據
for (let i = 0; i < lastRowPostForm - START_ROW + 1; i++) {
let data = getRowData(postFormData[i], i);
if (data) {
let url = docLinks[i][0].getLinkUrl();
if (url) data.doc_link = url;
reqDatas.push(data);
}
}
// 清理 貼文日曆
if(reqDatas.length > 0)
cleanCalender();
// 讀取日曆工作表的現有數據
const range = calendarSheet.getRange(1, 1, lastRowCalendar, MAX_COLUMNS);
let calendarValues = range.getRichTextValues();
let calendarBackgrounds = range.getBackgrounds();
// 新增 貼文事件 並更新 當日順序 欄位
reqDatas.forEach((data, index) => {
let [eventIndex, richText, color] = convertReqToEvent(calendarValues, data);
// 設置日曆中的富文本和背景色
calendarValues[data.row - 1][data.col - 1] = richText;
calendarBackgrounds[data.row - 1][data.col - 1] = color;
// 更新 貼文表單 中的 當日順序
postFormSheet.getRange(index + START_ROW, 5).setValue(eventIndex);
});
// 清理 貼文表單 中的 當日順序 欄位
for (let i = reqDatas.length + START_ROW; i <= lastRowPostForm; i++) {
postFormSheet.getRange(i, 5).setValue("");
}
const updateRange = calendarSheet.getRange(1, 1, lastRowCalendar, 3);
const displayValues = updateRange.getDisplayValues();
displayValues.forEach((row, rowIndex) => {
row.forEach((value, colIndex) => {
let richText = SpreadsheetApp.newRichTextValue().setText(value).build();
calendarValues[rowIndex][colIndex] = richText;
});
});
try {
// 將更新的數據批量寫回
range.setRichTextValues(calendarValues);
range.setBackgrounds(calendarBackgrounds);
} catch (error) {
console.log(error);
}
}
function cleanCalender() {
var sheet = SpreadsheetApp.getActive().getSheetByName('貼文日曆');
let lastRow = sheet.getLastRow();
var range = sheet.getRange('D4:J'+lastRow);
range.clear();
}
function convertReqToEvent(calenderArray, event) {
// team, person, row, col, title, color, url
const team = event.team;
const person = event.client;
const row = event.row;
const col = event.col;
const title = event.title;
const color = event.color;
const url = event.doc_link;
var text = title + "\n@" + team + " " + person;
// 製作 貼文日曆 事件
text = addEventContent(text, row, col, calenderArray);
var richText = SpreadsheetApp.newRichTextValue()
.setText(text)
.setLinkUrl(url)
.build();
var count = getCountEventOfDay(text);
return [count, richText, color]
}
function addEventContent(value, row, col, calenderArray) {
var cur_data = calenderArray[row-1][col-1].getText();
if (cur_data.length > 0)
cur_data = cur_data + "\n" + value;
else {
cur_data = value;
}
return cur_data;
}
function matchDate(postDate) {
let sheet = SpreadsheetApp.getActive().getSheetByName('貼文日曆');
let month = Utilities.formatDate(postDate, "GMT+8", "M");
let date = Utilities.formatDate(postDate, "GMT+8", "d");
var row = 1;
var real_row, real_col = 0;
for (row = 1; row < 200; row++) {
if (+sheet.getRange(row, 2).getValue() == month) {
break;
}
}
row_limit = row + 5;
var cur_date, tmp_date;
while (row < row_limit) {
cur_date = sheet.getRange(row, 3).getValue();
if (+cur_date > date) {
real_row = row - 1;
real_col = 11 + (date - cur_date);
break;
} else if (+cur_date < tmp_date) {
real_row = row - 1;
real_col = 4 + (date - tmp_date);
break;
}
row += 1;
tmp_date = cur_date;
}
return [real_row, real_col];
}
function matchColor(status) {
if (status == "已審閱")
color = "#a4c2f4";
else if (status == "待審閱")
color = "#ffcfc8";
else if (status == "已排程")
color = "#b7d7a8";
else if (status == "已發布")
color = "#cccccc";
else
color = "transparent";
return color;
}
function getCountEventOfDay(cur_data) {
let count = cur_data.split('').filter(char => char === '@').length;
return count;
}
const _insertDashboardLink = (sheet, editedRow, editedValue) => {
// Specify the column number where you want to add the link
const linkColumn = 18; // For example, column G
// Get the cell to which you want to add the link
const linkCell = sheet.getRange(editedRow, linkColumn);
const postDate = sheet.getRange(editedRow, 3).getValue();
const year = postDate.getFullYear();
const month = String(postDate.getMonth() + 1).padStart(2, '0'); // Months are zero-indexed
const day = String(postDate.getDate()).padStart(2, '0');
const fb = sheet.getRange(editedRow, 7).getValue();
const x = sheet.getRange(editedRow, 8).getValue();
const ig = sheet.getRange(editedRow, 9).getValue();
const linkedin = sheet.getRange(editedRow, 10).getValue();
const platform = x ? 'x' : '';
// Construct the link URL based on the edited value
// it will be replaced with the actual logic to construct the link URL
const linkUrl = `https://metabase.pycon.tw/question/214-social-media-marketing-metrics?date=${year}-${month}-${day}&platform=${platform}`
// Create a rich text value with the link
const richTextValue = SpreadsheetApp.newRichTextValue()
.setText(editedValue)
.setLinkUrl(linkUrl)
.build();
// Set the rich text value to the link cell
linkCell.setRichTextValue(richTextValue);
}
const _updateScheduledPosts = (range) => {
let editRange = range.getA1Notation().split(":");
// 根據 編輯範圍 採取對應處理方式
if(editRange.length > 1) {
console.log("Start: "+ editRange[0] + ",End: "+ editRange[1]);
// 編輯 多欄位,且欄位範圍符合條件則採取 刷新日曆
reflashCalendar();
} else {
console.log("Place: "+ editRange[0].charCodeAt(0));
// 編輯 單一欄位,且欄位範圍符合條件則採取 刷新日曆
if(checkCellPlace(editRange[0].charCodeAt(0))) {
reflashCalendar();
} else {
console.log("Edit is outside of the target range.");
}
}
}