-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodeFreeAIForSheets.gs
More file actions
205 lines (180 loc) · 6.52 KB
/
CodeFreeAIForSheets.gs
File metadata and controls
205 lines (180 loc) · 6.52 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
/**
* [서버 사이드] 구글 시트 메뉴 생성
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('⭐️ 코드프리 설정')
.addItem('설정 사이드바 보기', 'showSidebar')
.addToUi();
}
/**
* [서버 사이드] 사이드바 실행
*/
function showSidebar() {
var html = HtmlService.createHtmlOutput(HTML_SIDEBAR)
.setTitle('웹훅 및 트리거 설정')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
/**
* 설정 불러오기 (URL 및 트리거 상태)
*/
function getSettings() {
var triggers = ScriptApp.getProjectTriggers();
var isTriggerActive = triggers.some(t => t.getHandlerFunction() === 'onFormSubmitTriggerForCodeFree');
return {
url: PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL') || "",
triggerActive: isTriggerActive
};
}
/**
* URL 저장 함수
*/
function processForm(url) {
if (url && url.startsWith("http")) {
PropertiesService.getScriptProperties().setProperty('WEBHOOK_URL', url);
return "웹훅 URL이 저장되었습니다!";
} else {
throw new Error("유효하지 않은 URL입니다.");
}
}
/**
* 트리거 토글 함수 (켜기/끄기)
*/
function toggleTrigger(enable) {
var triggers = ScriptApp.getProjectTriggers();
// 기존 트리거 삭제
triggers.forEach(t => {
if (t.getHandlerFunction() === 'onFormSubmitTriggerForCodeFree') {
ScriptApp.deleteTrigger(t);
}
});
if (enable) {
// 트리거 생성 (양식 제출 시)
ScriptApp.newTrigger('onFormSubmitTriggerForCodeFree')
.forSpreadsheet(SpreadsheetApp.getActive())
.onFormSubmit()
.create();
return "양식 제출 트리거가 활성화되었습니다.";
} else {
return "양식 제출 트리거가 비활성화되었습니다.";
}
}
// 수정 시 실행 (마지막 컬럼 수정 시)
function onEdit(e) {
var range = e.range;
var sheet = range.getSheet();
var lastColumn = sheet.getLastColumn();
if (range.getColumn() == lastColumn) {
var row = range.getRow();
var rowData = sheet.getRange(row, 1, 1, lastColumn).getValues()[0];
sendRowToWebhook(rowData, sheet.getName());
}
}
// 양식 제출 시 실행
function onFormSubmitTriggerForCodeFree(e) {
var rowData = e.values;
sendRowToWebhook(rowData);
}
function sendRowToWebhook(rowData, sheetName) {
var url = PropertiesService.getScriptProperties().getProperty('WEBHOOK_URL');
if (!url) return;
var payload = {
"action": "new_row_added",
"sheetName": sheetName || "Unknown",
"data": rowData
};
var options = {
"method": "post",
"contentType": "application/json",
"payload": JSON.stringify(payload)
};
UrlFetchApp.fetch(url, options);
}
/**
* [클라이언트 사이드] HTML 코드 정의
*/
var HTML_SIDEBAR = `
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
body { font-family: sans-serif; padding: 15px; background-color: #f9f9f9; color: #333; }
.section { margin-bottom: 20px; border-bottom: 1px solid #ddd; padding-bottom: 15px; }
label { font-weight: bold; display: block; margin-bottom: 8px; }
input[type="text"] { width: 100%; padding: 8px; margin-bottom: 10px; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; }
button { width: 100%; padding: 10px; background-color: #4285f4; color: white; border: none; border-radius: 4px; cursor: pointer; font-weight: bold; }
button:hover { background-color: #357abd; }
/* 토글 스위치 스타일 */
.switch-container { display: flex; align-items: center; justify-content: space-between; }
.switch { position: relative; display: inline-block; width: 40px; height: 22px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 22px; }
.slider:before { position: absolute; content: ""; height: 16px; width: 16px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
input:checked + .slider { background-color: #4285f4; }
input:checked + .slider:before { transform: translateX(18px); }
#status { margin-top: 15px; font-size: 0.85em; color: #666; min-height: 1.2em; }
</style>
</head>
<body>
<div class="section">
<label>웹훅 URL 설정</label>
<input type="text" id="webhookUrl" placeholder="https://...">
<button onclick="saveSettings()">URL 저장</button>
</div>
<div class="section">
<div class="switch-container">
<label style="margin:0;">양식 제출 트리거</label>
<label class="switch">
<input type="checkbox" id="triggerToggle" onchange="handleToggle(this)">
<span class="slider"></span>
</label>
</div>
<p style="font-size: 0.75em; color: #777; margin-top: 5px;">활성화 시 구글 양식 제출을 감지합니다.</p>
</div>
<div id="status">데이터를 불러오는 중...</div>
<script>
window.onload = function() {
google.script.run
.withSuccessHandler(function(settings) {
document.getElementById('webhookUrl').value = settings.url;
document.getElementById('triggerToggle').checked = settings.triggerActive;
document.getElementById('status').innerText = "설정을 성공적으로 불러왔습니다.";
})
.getSettings();
};
function saveSettings() {
var url = document.getElementById('webhookUrl').value;
showStatus("저장 중...", "#666");
google.script.run
.withSuccessHandler(function(msg) {
showStatus(msg, "green");
})
.withFailureHandler(function(err) {
showStatus("에러: " + err.message, "red");
})
.processForm(url);
}
function handleToggle(checkbox) {
var isEnabled = checkbox.checked;
showStatus("트리거 설정 변경 중...", "#666");
google.script.run
.withSuccessHandler(function(msg) {
showStatus(msg, "green");
})
.withFailureHandler(function(err) {
checkbox.checked = !isEnabled; // 실패 시 복구
showStatus("에러: " + err.message, "red");
})
.toggleTrigger(isEnabled);
}
function showStatus(msg, color) {
var statusDiv = document.getElementById('status');
statusDiv.innerText = msg;
statusDiv.style.color = color;
}
</script>
</body>
</html>
`;