-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGoogleAppsScript.html
More file actions
509 lines (454 loc) · 19.2 KB
/
GoogleAppsScript.html
File metadata and controls
509 lines (454 loc) · 19.2 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Apps Script 詳細診斷</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 1000px;
margin: 0 auto;
padding: 20px;
background: #f8f9fa;
line-height: 1.6;
}
.container {
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
margin-bottom: 20px;
}
h1, h2, h3 {
color: #333;
}
.step {
background: #e3f2fd;
padding: 20px;
border-radius: 8px;
margin: 20px 0;
border-left: 4px solid #2196f3;
}
.warning {
background: #fff3e0;
padding: 15px;
border-radius: 5px;
border-left: 4px solid #ff9800;
margin: 15px 0;
}
.error {
background: #ffebee;
padding: 15px;
border-radius: 5px;
border-left: 4px solid #f44336;
margin: 15px 0;
}
.success {
background: #e8f5e8;
padding: 15px;
border-radius: 5px;
border-left: 4px solid #4caf50;
margin: 15px 0;
}
.code {
background: #f5f5f5;
padding: 15px;
border-radius: 5px;
font-family: 'Courier New', monospace;
white-space: pre-wrap;
overflow-x: auto;
border: 1px solid #ddd;
}
.checklist {
background: #f9f9f9;
padding: 20px;
border-radius: 5px;
border: 1px solid #ddd;
}
.checklist input[type="checkbox"] {
margin-right: 10px;
transform: scale(1.2);
}
.checklist label {
cursor: pointer;
display: block;
margin: 8px 0;
padding: 5px;
}
.checklist label:hover {
background: #f0f0f0;
border-radius: 3px;
}
button {
background: #4285f4;
color: white;
padding: 12px 24px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
margin: 5px;
}
button:hover {
background: #3367d6;
}
.test-section {
border: 2px dashed #ddd;
padding: 20px;
margin: 20px 0;
border-radius: 8px;
}
input, textarea {
width: 100%;
padding: 10px;
margin: 5px 0;
border: 1px solid #ddd;
border-radius: 4px;
box-sizing: border-box;
}
.result {
margin: 10px 0;
padding: 10px;
border-radius: 4px;
min-height: 20px;
}
.tabs {
display: flex;
border-bottom: 1px solid #ddd;
margin-bottom: 20px;
}
.tab {
padding: 10px 20px;
cursor: pointer;
border: none;
background: none;
border-bottom: 2px solid transparent;
}
.tab.active {
border-bottom-color: #4285f4;
color: #4285f4;
font-weight: bold;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1>🔧 Google Apps Script 完整診斷工具</h1>
<div class="tabs">
<button class="tab active" onclick="showTab('checklist')">檢查清單</button>
<button class="tab" onclick="showTab('deployment')">部署指南</button>
<button class="tab" onclick="showTab('testing')">連接測試</button>
<button class="tab" onclick="showTab('debugging')">除錯工具</button>
</div>
<!-- 檢查清單標籤 -->
<div id="checklist" class="tab-content active">
<h2>📋 請逐一確認以下項目:</h2>
<div class="checklist">
<h3>🔧 Google Apps Script 設定</h3>
<label><input type="checkbox"> 已開啟 Google Apps Script 編輯器</label>
<label><input type="checkbox"> 已貼入完整的 doPost 函數代碼</label>
<label><input type="checkbox"> 已儲存腳本檔案</label>
<label><input type="checkbox"> 腳本有連結到一個 Google Sheets 試算表</label>
<h3>🚀 部署設定</h3>
<label><input type="checkbox"> 點擊了「部署」按鈕</label>
<label><input type="checkbox"> 選擇了「新增部署作業」</label>
<label><input type="checkbox"> 部署類型選擇「網頁應用程式」</label>
<label><input type="checkbox"> 執行身分設為「我」</label>
<label><input type="checkbox"> 存取權限設為「任何人」</label>
<label><input type="checkbox"> 已點擊「部署」完成部署</label>
<label><input type="checkbox"> 已複製部署後的網址(以 /exec 結尾)</label>
<h3>🔐 權限設定</h3>
<label><input type="checkbox"> 第一次部署時已授權應用程式</label>
<label><input type="checkbox"> 已授權 Google Sheets 存取權限</label>
<label><input type="checkbox"> 沒有出現權限錯誤訊息</label>
<h3>📊 試算表設定</h3>
<label><input type="checkbox"> Google Sheets 試算表已建立</label>
<label><input type="checkbox"> 試算表與 Apps Script 在同一個 Google 帳號下</label>
<label><input type="checkbox"> 試算表可以正常編輯</label>
</div>
<div class="warning">
⚠️ 如果任何一項沒有勾選,請先完成該項目再進行測試!
</div>
</div>
<!-- 部署指南標籤 -->
<div id="deployment" class="tab-content">
<h2>🚀 詳細部署步驟</h2>
<div class="step">
<h3>步驟 1: 建立 Google Apps Script</h3>
<p>1. 前往 <a href="https://script.google.com" target="_blank">script.google.com</a></p>
<p>2. 點擊「新增專案」</p>
<p>3. 將以下代碼貼入編輯器:</p>
<div class="code">function doPost(e) {
let data = {};
let isJson = true;
try {
// 嘗試解析 JSON 格式
data = JSON.parse(e.postData.contents);
} catch (err) {
isJson = false;
// 嘗試使用 URLSearchParams 格式
if (e.parameter.name || e.parameter.email || e.parameter.message) {
data = {
name: e.parameter.name || '',
email: e.parameter.email || '',
message: e.parameter.message || '',
token: e.parameter.token || ''
};
} else {
return ContentService
.createTextOutput(JSON.stringify({
success: false,
error: 'JSON 資料格式錯誤: ' + err.message
}))
.setMimeType(ContentService.MimeType.JSON);
}
}
try {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
if (sheet.getLastRow() === 0) {
sheet.appendRow(['時間', '姓名', '郵箱', '訊息', 'Token']);
}
sheet.appendRow([
new Date(),
data.name || '',
data.email || '',
data.message || '',
data.token || ''
]);
return ContentService
.createTextOutput(JSON.stringify({ success: true, format: isJson ? 'json' : 'urlencoded' }))
.setMimeType(ContentService.MimeType.JSON);
} catch (error) {
return ContentService
.createTextOutput(JSON.stringify({ success: false, error: error.toString() }))
.setMimeType(ContentService.MimeType.JSON);
}
}
function doGet(e) {
return ContentService.createTextOutput('Apps Script 運作正常!');
}</div>
<p>4. 點擊「儲存」並命名您的專案</p>
</div>
<div class="step">
<h3>步驟 2: 連結 Google Sheets</h3>
<p>1. 在 Apps Script 編輯器中,點擊左側的「資源」→「程式庫」</p>
<p>2. 或者建立一個新的 Google Sheets,然後在 Apps Script 中:</p>
<p>3. 點擊「資源」→「Cloud Platform 專案」</p>
<p>4. 確保腳本與試算表有相同的存取權限</p>
</div>
<div class="step">
<h3>步驟 3: 部署為網頁應用程式</h3>
<p>1. 點擊右上角的「部署」</p>
<p>2. 選擇「新增部署作業」</p>
<p>3. 在「類型」中選擇「網頁應用程式」</p>
<p>4. 設定:</p>
<ul>
<li><strong>執行身分:</strong>我</li>
<li><strong>存取權限:</strong>任何人</li>
</ul>
<p>5. 點擊「部署」</p>
<p>6. <strong>重要:</strong>複製提供的網址(應該以 /exec 結尾)</p>
</div>
<div class="error">
❌ <strong>常見錯誤:</strong><br>
• 使用編輯器網址而不是部署網址<br>
• 權限設定為「只有我」而不是「任何人」<br>
• 沒有完成授權流程
</div>
</div>
<!-- 測試標籤 -->
<div id="testing" class="tab-content">
<h2>🧪 連接測試</h2>
<div class="test-section">
<h3>測試 1: 基本連接測試</h3>
<input type="url" id="testUrl" placeholder="貼入您的 Apps Script 部署網址">
<button onclick="testBasicConnection()">測試 GET 請求</button>
<div id="basicResult" class="result"></div>
</div>
<div class="test-section">
<h3>測試 2: POST 資料測試</h3>
<input type="text" id="testName" placeholder="測試姓名" value="張三">
<input type="email" id="testEmail" placeholder="測試郵箱" value="test@example.com">
<textarea id="testMessage" placeholder="測試訊息">這是一個連接測試訊息</textarea>
<button onclick="testPostData()">發送 POST 請求</button>
<div id="postResult" class="result"></div>
</div>
<div class="test-section">
<h3>測試 3: 完整表單測試</h3>
<form id="fullTestForm">
<input type="text" name="name" placeholder="姓名" value="李四" required>
<input type="email" name="email" placeholder="郵箱" value="li@example.com" required>
<textarea name="message" placeholder="訊息" required>完整表單測試訊息</textarea>
<button type="submit">完整測試</button>
</form>
<div id="formResult" class="result"></div>
</div>
</div>
<!-- 除錯工具標籤 -->
<div id="debugging" class="tab-content">
<h2>🐛 除錯工具</h2>
<div class="warning">
<h3>如何檢查錯誤:</h3>
<ol>
<li><strong>Apps Script 執行記錄:</strong>
<br>• 在 Apps Script 編輯器點擊「執行作業」→「我的執行作業」
<br>• 查看最近的執行記錄和錯誤訊息
</li>
<li><strong>瀏覽器開發者工具:</strong>
<br>• 按 F12 打開開發者工具
<br>• 查看 Console 標籤的錯誤訊息
<br>• 查看 Network 標籤的請求狀態
</li>
<li><strong>Google Sheets 檢查:</strong>
<br>• 確認試算表是否可以手動編輯
<br>• 檢查是否有新的資料列
</li>
</ol>
</div>
<div class="step">
<h3>常見錯誤和解決方案:</h3>
<h4>錯誤 1: "Access denied" 或權限錯誤</h4>
<p><strong>解決方案:</strong></p>
<ul>
<li>重新部署,確保權限設為「任何人」</li>
<li>完成 Google 的授權流程</li>
<li>檢查試算表的共用設定</li>
</ul>
<h4>錯誤 2: "Cannot read property" 錯誤</h4>
<p><strong>解決方案:</strong></p>
<ul>
<li>檢查 POST 資料格式是否正確</li>
<li>確認 Content-Type 為 application/json</li>
<li>驗證 JSON 資料結構</li>
</ul>
<h4>錯誤 3: CORS 錯誤</h4>
<p><strong>解決方案:</strong></p>
<ul>
<li>確保使用正確的部署網址</li>
<li>檢查網站是否在 HTTPS 上運行</li>
<li>嘗試從不同的瀏覽器測試</li>
</ul>
</div>
<div class="test-section">
<h3>手動除錯步驟:</h3>
<button onclick="showDebuggingSteps()">顯示除錯檢查清單</button>
<div id="debugSteps" style="display:none;">
<ol>
<li>✅ 開啟瀏覽器開發者工具(F12)</li>
<li>✅ 在 Console 標籤查看任何錯誤訊息</li>
<li>✅ 嘗試發送測試表單</li>
<li>✅ 在 Network 標籤查看請求狀態</li>
<li>✅ 檢查 Google Apps Script 執行記錄</li>
<li>✅ 驗證 Google Sheets 是否有新資料</li>
</ol>
</div>
</div>
</div>
</div>
<script>
// 標籤切換功能
function showTab(tabName) {
// 隱藏所有標籤內容
const contents = document.querySelectorAll('.tab-content');
contents.forEach(content => content.classList.remove('active'));
// 移除所有標籤的活動狀態
const tabs = document.querySelectorAll('.tab');
tabs.forEach(tab => tab.classList.remove('active'));
// 顯示選中的標籤內容
document.getElementById(tabName).classList.add('active');
event.target.classList.add('active');
}
// 基本連接測試
async function testBasicConnection() {
const url = document.getElementById('testUrl').value;
const resultDiv = document.getElementById('basicResult');
if (!url) {
resultDiv.innerHTML = '<div class="error">請輸入 Apps Script 網址!</div>';
return;
}
resultDiv.innerHTML = '<div class="result">測試中...</div>';
try {
const response = await fetch(url, { method: 'GET' });
const text = await response.text();
if (response.ok) {
resultDiv.innerHTML = `<div class="success">✅ GET 請求成功!<br>回應: ${text}</div>`;
} else {
resultDiv.innerHTML = `<div class="error">❌ GET 請求失敗: ${response.status} - ${response.statusText}</div>`;
}
} catch (error) {
resultDiv.innerHTML = `<div class="error">❌ 連接錯誤: ${error.message}<br><small>可能的原因:網址錯誤、CORS 政策、或網路問題</small></div>`;
}
}
// POST 資料測試
async function testPostData() {
const url = document.getElementById('testUrl').value;
const resultDiv = document.getElementById('postResult');
if (!url) {
resultDiv.innerHTML = '<div class="error">請先在上方輸入 Apps Script 網址!</div>';
return;
}
const formData = new URLSearchParams();
formData.append('name', document.getElementById('testName').value);
formData.append('email', document.getElementById('testEmail').value);
formData.append('message', document.getElementById('testMessage').value);
formData.append('token', 'test_' + Date.now());
resultDiv.innerHTML = '<div class="result">發送 POST 請求中...</div>';
try {
const response = await fetch(url, {
method: 'POST',
body: formData
});
const responseText = await response.text();
if (response.ok) {
resultDiv.innerHTML = `<div class="success">✅ POST 請求成功!<br>伺服器回應: ${responseText}<br><strong>請檢查您的 Google Sheets 是否有新資料</strong></div>`;
} else {
resultDiv.innerHTML = `<div class="error">❌ POST 請求失敗<br>狀態碼: ${response.status}<br>回應: ${responseText}</div>`;
}
} catch (error) {
resultDiv.innerHTML = `<div class="error">❌ POST 請求錯誤: ${error.message}</div>`;
}
}
document.getElementById('fullTestForm').addEventListener('submit', async function(e) {
e.preventDefault();
const url = document.getElementById('testUrl').value;
const resultDiv = document.getElementById('formResult');
if (!url) {
resultDiv.innerHTML = '<div class="error">請先在上方輸入 Apps Script 網址!</div>';
return;
}
const formData = new URLSearchParams();
formData.append('name', this.name.value);
formData.append('email', this.email.value);
formData.append('message', this.message.value);
formData.append('token', 'form_test_' + Date.now());
resultDiv.innerHTML = '<div class="result">完整表單測試中...</div>';
try {
const response = await fetch(url, {
method: 'POST',
body: formData
});
const responseText = await response.text();
console.log('Response:', responseText);
if (response.ok) {
resultDiv.innerHTML = `<div class="success">🎉 完整測試成功!<br>回應: ${responseText}<br><strong>請立即檢查 Google Sheets</strong></div>`;
} else {
resultDiv.innerHTML = `<div class="error">❌ 完整測試失敗<br>HTTP ${response.status}: ${responseText}</div>`;
}
} catch (error) {
resultDiv.innerHTML = `<div class="error">❌ 測試錯誤: ${error.message}<br><small>建議檢查開發者工具的 Network 和 Console 標籤</small></div>`;
}
});
function showDebuggingSteps() {
const steps = document.getElementById('debugSteps');
steps.style.display = steps.style.display === 'none' ? 'block' : 'none';
}
</script>
</body>
</html>