-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
742 lines (640 loc) · 21.8 KB
/
Copy pathCode.gs
File metadata and controls
742 lines (640 loc) · 21.8 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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
/**
* ============================================================================
* 工廠環境衛生點檢管理系統 - 主程式模組
* Factory Environmental Hygiene Inspection System - Main Module
* ============================================================================
*
* @file Code.gs
* @version v3.0
* @date 2026-02-14
* @author System Developer
* @description 系統主程式,包含選單建立、系統初始化、UI 介面開啟等核心功能
*
* @iso_standard ISO 22000:2018 食品安全管理系統
* @form_number P-4-001 版本 A1 (可在 System_Config 自訂)
*
* @constants
* - SHEETS 工作表名稱常數
* - DEFAULTS 系統預設值
*
* @functions
* - onOpen() 建立選單(自動執行)
* - initializeSystem() 初始化系統
* - openInspectionForm() 開啟點檢表單
* - openStatistics() 開啟統計報表
* - exportData() 匯出資料
* - getInspectorList() 取得點檢人員清單
* - getSystemConfig() 取得系統設定(含表單編號)
*
* @dependencies
* - SheetInitializer.gs
* - InspectionItems.gs
* - DataHandler.gs
* - AlertSystem.gs
* - Statistics.gs
*
* @changelog
* v3.0 (2026-02-14) - 項目主檔管理與PDF報表輸出
* v2.2.5 (2026-01-26) - ISO 22000 整合,動態表單編號
* v2.2.4 (2026-01-26) - 效能優化,完整告警系統
* v2.2.3 (2026-01-26) - UI 優化,資料路由修正
*
* ============================================================================
*/
// ==================== 全域變數設定 ====================
const SPREADSHEET_ID = SpreadsheetApp.getActiveSpreadsheet().getId();
// Sheet 名稱常數
const SHEETS = {
RECORDS: 'Inspection_Records', // Sheet 1: 點檢記錄表
ITEM_MASTER: 'Item_Master', // Sheet 2: 項目主檔
TEMP_HUMIDITY: 'Temperature_Humidity', // Sheet 3: 溫濕度記錄
CUSTOM_STATS: 'Custom_Period_Statistics',// Sheet 4: 自訂期間統計
SYSTEM_CONFIG: 'System_Config', // Sheet 5: 系統設定
TEMP_CRITERIA: 'TempHumidity_Criteria', // Sheet 6: 溫濕度標準設定
LANGUAGE: 'Language_Settings', // Sheet 7: 多語系設定
ITEM_HISTORY: 'Item_Master_History' // Sheet 8: 項目主檔修訂歷史
};
// 系統預設值
const DEFAULTS = {
SHIFTS: ['早', '晚', '其它'],
LANGUAGE: 'zh-TW',
ISO_FORM_NUMBER: 'P-4-001 版本 A1',
ISO_STANDARD: 'ISO 22000:2018'
};
// ==================== 初始化函數 ====================
/**
* 建立選單 - 當試算表開啟時自動執行
*/
function onOpen() {
const ui = SpreadsheetApp.getUi();
ui.createMenu('🏭 點檢系統')
.addItem('📝 開啟點檢表單', 'openInspectionForm')
.addItem('📊 查看統計報表', 'openStatistics')
.addSeparator()
.addSubMenu(ui.createMenu('📋 點檢項目主檔管理')
.addItem('🔄 同步點檢項目主檔', 'syncItemMasterUI')
.addItem('📜 查看修訂歷史', 'viewItemHistory')
.addItem('📥 匯出點檢項目主檔', 'exportItemMasterUI'))
.addSeparator()
.addSubMenu(ui.createMenu('🌡️ 溫濕度管理')
.addItem('🔄 同步溫濕度標準', 'syncTempCriteriaUI')
.addItem('📋 查看標準設定', 'viewTempCriteria'))
.addSeparator()
.addItem('⚙️ 系統設定', 'openSettings')
.addItem('🔄 初始化系統', 'initializeSystem')
.addItem('📤 匯出資料', 'exportData')
.addToUi();
}
/**
* 初始化整個系統 - 建立所有必要的工作表和資料
*/
function initializeSystem() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const ui = SpreadsheetApp.getUi();
const response = ui.alert(
'初始化系統',
'這將建立所有必要的工作表和資料。\n已存在的工作表將被保留。\n\n確定要繼續嗎?',
ui.ButtonSet.YES_NO
);
if (response !== ui.Button.YES) {
return;
}
try {
// 建立各個工作表
createSheet_InspectionRecords(ss);
createSheet_ItemMaster(ss);
createSheet_TempHumidity(ss);
createSheet_CustomStats(ss);
createSheet_SystemConfig(ss);
createSheet_TempCriteria(ss);
createSheet_Language(ss);
createSheet_ItemHistory(ss); // v3.0 新增
// 初始化資料
initializeItemMaster();
initializeTempCriteria();
initializeLanguageSettings();
initializeSystemConfig();
// 設定觸發器
setupTriggers();
ui.alert('✅ 系統初始化完成!', '所有工作表和資料已建立完成。', ui.ButtonSet.OK);
} catch (error) {
ui.alert('❌ 錯誤', '初始化失敗:' + error.message, ui.ButtonSet.OK);
Logger.log('初始化錯誤:' + error.stack);
}
}
/**
* 取得或建立工作表
*/
function getOrCreateSheet(ss, sheetName, headers = []) {
let sheet = ss.getSheetByName(sheetName);
if (!sheet) {
sheet = ss.insertSheet(sheetName);
if (headers.length > 0) {
sheet.getRange(1, 1, 1, headers.length).setValues([headers]);
sheet.getRange(1, 1, 1, headers.length)
.setFontWeight('bold')
.setBackground('#4285F4')
.setFontColor('#FFFFFF');
sheet.setFrozenRows(1);
}
}
return sheet;
}
// ==================== Web App 入口 ====================
/**
* Web App 主入口 - doGet
*/
function doGet(e) {
const page = e.parameter.page || 'inspection';
const lang = e.parameter.lang || 'zh-TW';
let template;
switch(page) {
case 'inspection':
template = HtmlService.createTemplateFromFile('InspectionForm');
break;
case 'statistics':
template = HtmlService.createTemplateFromFile('StatisticsView');
break;
default:
template = HtmlService.createTemplateFromFile('InspectionForm');
}
template.lang = lang;
return template.evaluate()
.setTitle('工廠環境衛生點檢系統 - Factory Environmental Hygiene Inspection System')
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.addMetaTag('viewport', 'width=device-width, initial-scale=1');
}
/**
* 開啟點檢表單
*/
function openInspectionForm() {
const html = HtmlService.createTemplateFromFile('InspectionForm')
.evaluate()
.setWidth(1200)
.setHeight(800);
SpreadsheetApp.getUi().showModalDialog(html, '工廠環境衛生點檢系統');
}
/**
* 開啟統計報表
*/
function openStatistics() {
const html = HtmlService.createTemplateFromFile('StatisticsView')
.evaluate()
.setWidth(1200)
.setHeight(800);
SpreadsheetApp.getUi().showModalDialog(html, '統計分析報表');
}
/**
* 開啟系統設定
*/
function openSettings() {
const ui = SpreadsheetApp.getUi();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const configSheet = ss.getSheetByName(SHEETS.SYSTEM_CONFIG);
if (!configSheet) {
ui.alert('錯誤', '找不到系統設定工作表,請先執行「初始化系統」', ui.ButtonSet.OK);
return;
}
// 直接開啟系統設定工作表
ss.setActiveSheet(configSheet);
ui.alert(
'系統設定',
'您現在可以在「System_Config」工作表中修改系統設定。\n\n常用設定:\n• 預設點檢人員(逗號分隔)\n• 點檢時段\n• ISO表單編號',
ui.ButtonSet.OK
);
}
/**
* 匯出資料
*/
function exportData() {
const ui = SpreadsheetApp.getUi();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const response = ui.alert(
'匯出資料',
'請選擇要匯出的資料類型:\n\n' +
'「是」- 匯出點檢記錄\n' +
'「否」- 匯出溫濕度記錄\n' +
'「取消」- 取消操作',
ui.ButtonSet.YES_NO_CANCEL
);
try {
if (response === ui.Button.YES) {
// 匯出點檢記錄
const recordSheet = ss.getSheetByName(SHEETS.RECORDS);
if (!recordSheet) {
ui.alert('錯誤', '找不到點檢記錄表', ui.ButtonSet.OK);
return;
}
const data = recordSheet.getDataRange().getValues();
if (data.length <= 1) {
ui.alert('提示', '目前沒有點檢記錄可匯出', ui.ButtonSet.OK);
return;
}
// 建立新試算表
const newSs = SpreadsheetApp.create('點檢記錄匯出_' + Utilities.formatDate(new Date(), 'GMT+8', 'yyyyMMdd_HHmmss'));
const newSheet = newSs.getActiveSheet();
newSheet.setName('Inspection_Records');
// 複製資料
newSheet.getRange(1, 1, data.length, data[0].length).setValues(data);
// 格式化
newSheet.getRange(1, 1, 1, data[0].length).setFontWeight('bold').setBackground('#4285F4').setFontColor('white');
newSheet.autoResizeColumns(1, data[0].length);
ui.alert(
'✅ 匯出成功',
`點檢記錄已匯出 ${data.length - 1} 筆\n\n請至 Google Drive 查看:\n「點檢記錄匯出_${Utilities.formatDate(new Date(), 'GMT+8', 'yyyyMMdd_HHmmss')}」`,
ui.ButtonSet.OK
);
// 開啟新檔案
const url = newSs.getUrl();
const htmlOutput = HtmlService.createHtmlOutput(`
<script>
window.open('${url}', '_blank');
google.script.host.close();
</script>
`);
ui.showModalDialog(htmlOutput, '正在開啟匯出檔案...');
} else if (response === ui.Button.NO) {
// 匯出溫濕度記錄
const tempSheet = ss.getSheetByName(SHEETS.TEMP_HUMIDITY);
if (!tempSheet) {
ui.alert('錯誤', '找不到溫濕度記錄表', ui.ButtonSet.OK);
return;
}
const data = tempSheet.getDataRange().getValues();
if (data.length <= 1) {
ui.alert('提示', '目前沒有溫濕度記錄可匯出', ui.ButtonSet.OK);
return;
}
// 建立新試算表
const newSs = SpreadsheetApp.create('溫濕度記錄匯出_' + Utilities.formatDate(new Date(), 'GMT+8', 'yyyyMMdd_HHmmss'));
const newSheet = newSs.getActiveSheet();
newSheet.setName('Temperature_Humidity');
// 複製資料
newSheet.getRange(1, 1, data.length, data[0].length).setValues(data);
// 格式化
newSheet.getRange(1, 1, 1, data[0].length).setFontWeight('bold').setBackground('#4285F4').setFontColor('white');
newSheet.autoResizeColumns(1, data[0].length);
ui.alert(
'✅ 匯出成功',
`溫濕度記錄已匯出 ${data.length - 1} 筆\n\n請至 Google Drive 查看:\n「溫濕度記錄匯出_${Utilities.formatDate(new Date(), 'GMT+8', 'yyyyMMdd_HHmmss')}」`,
ui.ButtonSet.OK
);
// 開啟新檔案
const url = newSs.getUrl();
const htmlOutput = HtmlService.createHtmlOutput(`
<script>
window.open('${url}', '_blank');
google.script.host.close();
</script>
`);
ui.showModalDialog(htmlOutput, '正在開啟匯出檔案...');
}
} catch (error) {
ui.alert('❌ 錯誤', '匯出失敗:' + error.message, ui.ButtonSet.OK);
Logger.log('匯出錯誤:' + error.stack);
}
}
/**
* Include 函數 - 用於載入 HTML 檔案中的 CSS/JS
*/
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
// ==================== 輔助函數 ====================
/**
* 生成記錄ID
*/
function generateRecordId(prefix, date, shift) {
const dateStr = Utilities.formatDate(date, 'GMT+8', 'yyyyMMdd');
const shiftCode = shift === '早' ? 'AM' : (shift === '晚' ? 'PM' : 'OT');
const timestamp = new Date().getTime().toString().slice(-3);
return `${prefix}${dateStr}${shiftCode}${timestamp}`;
}
/**
* 格式化日期
*/
function formatDate(date, format = 'yyyy-MM-dd') {
return Utilities.formatDate(new Date(date), 'GMT+8', format);
}
/**
* 取得當前時間戳記
*/
function getTimestamp() {
return new Date();
}
/**
* 記錄系統日誌
*/
function logSystem(message, level = 'INFO') {
const timestamp = Utilities.formatDate(new Date(), 'GMT+8', 'yyyy-MM-dd HH:mm:ss');
Logger.log(`[${timestamp}] [${level}] ${message}`);
}
/**
* 取得點檢人員清單
*/
function getInspectorList() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(SHEETS.SYSTEM_CONFIG);
if (!sheet) return ['張三', '李四', '王五', '趙六', '陳七'];
const data = sheet.getDataRange().getValues();
for (let i = 1; i < data.length; i++) {
if (data[i][0] === '預設點檢人員' && data[i][1]) {
const inspectors = data[i][1].toString().split(',').map(name => name.trim()).filter(name => name);
if (inspectors.length > 0) {
return inspectors;
}
}
}
return ['張三', '李四', '王五', '趙六', '陳七'];
} catch (error) {
logSystem('取得點檢人員清單失敗:' + error.message, 'ERROR');
return ['張三', '李四', '王五', '趙六', '陳七'];
}
}
/**
* 取得系統設定(包含 ISO 表單編號)
* @returns {Object} 系統設定物件
*/
function getSystemConfig() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(SHEETS.SYSTEM_CONFIG);
if (!sheet) {
logSystem('System_Config 工作表不存在,使用預設值', 'WARNING');
return {
formNumber: DEFAULTS.ISO_FORM_NUMBER,
isoStandard: DEFAULTS.ISO_STANDARD,
systemVersion: 'v2.2.5'
};
}
const data = sheet.getDataRange().getValues();
const config = {
formNumber: DEFAULTS.ISO_FORM_NUMBER,
isoStandard: DEFAULTS.ISO_STANDARD,
systemVersion: 'v2.2.5'
};
// 讀取設定值
for (let i = 1; i < data.length; i++) {
const key = data[i][0];
const value = data[i][1];
if (key === 'ISO表單編號' && value) {
config.formNumber = value.toString().trim();
} else if (key === 'ISO標準' && value) {
config.isoStandard = value.toString().trim();
} else if (key === '系統版本' && value) {
config.systemVersion = value.toString().trim();
}
}
return config;
} catch (error) {
logSystem('取得系統設定失敗:' + error.message, 'ERROR');
return {
formNumber: DEFAULTS.ISO_FORM_NUMBER,
isoStandard: DEFAULTS.ISO_STANDARD,
systemVersion: 'v3.0'
};
}
}
// ==================== v3.0 項目主檔管理選單函數 ====================
/**
* 同步點檢項目主檔 UI
*/
function syncItemMasterUI() {
const ui = SpreadsheetApp.getUi();
const response = ui.alert(
'同步點檢項目主檔',
'這將從 Sheet 2 (Item_Master) 同步項目資料到系統。\n\n確定要繼續嗎?',
ui.ButtonSet.YES_NO
);
if (response !== ui.Button.YES) {
return;
}
try {
const result = syncItemMaster();
if (result.success) {
ui.alert(
'同步成功',
`✅ 點檢項目主檔同步成功!\n\n` +
`啟用項目數: ${result.count}\n` +
`版本: ${result.version}\n` +
`時間: ${Utilities.formatDate(result.timestamp, 'GMT+8', 'yyyy-MM-dd HH:mm:ss')}\n\n` +
`請重新整理點檢表以查看更新。`,
ui.ButtonSet.OK
);
} else {
ui.alert('同步失敗', '❌ ' + result.error, ui.ButtonSet.OK);
}
} catch (error) {
ui.alert('同步失敗', '❌ ' + error.message, ui.ButtonSet.OK);
}
}
/**
* 查看修訂歷史
*/
function viewItemHistory() {
const ui = SpreadsheetApp.getUi();
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(SHEETS.ITEM_HISTORY);
if (!sheet) {
ui.alert(
'工作表不存在',
'找不到項目修訂歷史工作表。\n\n請先執行「初始化系統」。',
ui.ButtonSet.OK
);
return;
}
// 切換到修訂歷史工作表
ss.setActiveSheet(sheet);
ui.alert(
'修訂歷史',
'已切換到項目修訂歷史工作表 (Sheet 8)。\n\n' +
'您可以查看所有項目的修訂記錄。',
ui.ButtonSet.OK
);
}
/**
* 匯出點檢項目主檔 UI
*/
function exportItemMasterUI() {
const ui = SpreadsheetApp.getUi();
const response = ui.alert(
'匯出點檢項目主檔',
'這將建立一個新的試算表,包含目前的點檢項目主檔資料。\n\n確定要繼續嗎?',
ui.ButtonSet.YES_NO
);
if (response !== ui.Button.YES) {
return;
}
try {
const result = exportItemMaster();
if (result.success) {
ui.alert(
'匯出成功',
`✅ 點檢項目主檔已匯出!\n\n` +
`版本: ${result.version}\n` +
`時間: ${result.timestamp}\n\n` +
`請開啟以下連結查看:\n${result.url}`,
ui.ButtonSet.OK
);
} else {
ui.alert('匯出失敗', '❌ ' + result.error, ui.ButtonSet.OK);
}
} catch (error) {
ui.alert('匯出失敗', '❌ ' + error.message, ui.ButtonSet.OK);
}
}
// ==================== 溫濕度管理 ====================
/**
* 同步溫濕度標準 - UI 介面
*/
function syncTempCriteriaUI() {
const ui = SpreadsheetApp.getUi();
const response = ui.alert(
'同步溫濕度標準',
'這將從 Sheet 2 讀取所有「填空題」項目,\n並自動建立對應的溫濕度標準。\n\n確定要繼續嗎?',
ui.ButtonSet.YES_NO
);
if (response !== ui.Button.YES) {
return;
}
try {
const result = syncTempCriteriaFromItems();
if (result.success) {
ui.alert(
'同步成功',
`✅ 溫濕度標準已同步!\n\n` +
`新增測點: ${result.added}\n` +
`更新測點: ${result.updated}\n` +
`總計: ${result.total} 個測點\n\n` +
`請至 Sheet 6 確認標準值是否正確。`,
ui.ButtonSet.OK
);
} else {
ui.alert('同步失敗', '❌ ' + result.error, ui.ButtonSet.OK);
}
} catch (error) {
ui.alert('同步失敗', '❌ ' + error.message, ui.ButtonSet.OK);
}
}
/**
* 從 Sheet 2 同步溫濕度標準
*/
function syncTempCriteriaFromItems() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const itemSheet = ss.getSheetByName(SHEETS.ITEM_MASTER);
const criteriaSheet = ss.getSheetByName(SHEETS.TEMP_CRITERIA);
if (!itemSheet) {
throw new Error('找不到點檢項目主檔');
}
if (!criteriaSheet) {
throw new Error('找不到溫濕度標準設定表');
}
// 讀取現有標準
const existingData = criteriaSheet.getDataRange().getValues();
const existingNames = new Set();
for (let i = 1; i < existingData.length; i++) {
existingNames.add(existingData[i][0]);
}
// 讀取填空題項目
const itemData = itemSheet.getDataRange().getValues();
const newCriteria = [];
let added = 0;
let updated = 0;
for (let i = 1; i < itemData.length; i++) {
const [itemId, area, itemNo, nameCh, nameEn, questionType, unit, isEnabled] = itemData[i];
// 只處理啟用的填空題
if (questionType === '填空題' && (isEnabled === true || isEnabled === 'TRUE' || isEnabled === '是')) {
// 檢查是否已存在
if (existingNames.has(nameCh)) {
updated++;
continue;
}
let measureType = '';
let lower = 0;
let upper = 0;
// 根據單位判斷類型和預設標準
if (unit === '%RH' || unit === 'RH' || unit.includes('濕度')) {
measureType = '濕度';
lower = 30;
upper = 60;
} else if (unit === '℃' || unit === 'C' || unit.includes('溫度')) {
measureType = '溫度';
// 根據項目名稱判斷溫度範圍
const name = nameCh.toLowerCase();
if (name.includes('冷凍') || name.includes('成品庫') || name.includes('原料庫')) {
lower = -20;
upper = -15;
} else if (name.includes('半成品') && name.includes('冷凍')) {
lower = -18;
upper = -10;
} else if (name.includes('暫存') || name.includes('冷藏')) {
lower = 0;
upper = 7;
} else {
// 預設冷藏
lower = 0;
upper = 7;
}
} else {
// 其他單位暫不設定標準
continue;
}
newCriteria.push([
nameCh, // 測點名稱
area, // 所屬區域
measureType, // 測量類型
unit, // 單位
lower, // 下限值
upper, // 上限值
true, // 是否啟用告警
'郵件', // 告警方式
'', // 告警收件人
'自動同步' // 備註
]);
added++;
}
}
// 將新標準加入 Sheet 6
if (newCriteria.length > 0) {
const lastRow = criteriaSheet.getLastRow();
criteriaSheet.getRange(lastRow + 1, 1, newCriteria.length, 10).setValues(newCriteria);
}
logSystem(`溫濕度標準同步完成:新增 ${added} 個,更新 ${updated} 個`, 'INFO');
return {
success: true,
added: added,
updated: updated,
total: added + updated
};
} catch (error) {
logSystem('同步溫濕度標準失敗:' + error.message, 'ERROR');
return {
success: false,
error: error.message
};
}
}
/**
* 查看溫濕度標準設定
*/
function viewTempCriteria() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(SHEETS.TEMP_CRITERIA);
if (sheet) {
ss.setActiveSheet(sheet);
SpreadsheetApp.getUi().alert(
'溫濕度標準設定',
'已切換到 Sheet 6 (TempHumidity_Criteria)\n\n' +
'您可以在此檢視和修改溫濕度標準值。',
SpreadsheetApp.getUi().ButtonSet.OK
);
} else {
SpreadsheetApp.getUi().alert(
'錯誤',
'找不到溫濕度標準設定表 (Sheet 6)',
SpreadsheetApp.getUi().ButtonSet.OK
);
}
}