Part 08 Unit 03

工作流程整合

打造完整的文件處理流水線



學習目標

  • 設計端到端的文件處理流程
  • 整合多種工具和服務
  • 建立自動化觸發機制
  • 實現完整的文件管理系統

什麼是工作流程整合?


定義:將多個獨立的步驟串連成一個自動化流程,讓工作能夠順暢進行


1
輸入
從各種來源收集文件
2
處理
AI 整理 + Pandoc 轉換
3
輸出
分發到各個目的地

常見的整合場景


📥

收信整理

Email 附件自動處理

📁

資料夾監控

新檔案自動轉換

📤

報告發送

定時產生並寄送


🔄

版本管理

與 Git 整合

☁️

雲端同步

與雲端硬碟整合

🤖

AI 處理

智慧分類整理

設計流程的原則


1. 單一職責

每個步驟只做一件事,容易維護和除錯


2. 可重複使用

建立通用的元件,在不同流程中重複使用


3. 容錯設計

任何步驟失敗都不會影響其他步驟


4. 可觀察性

能夠追蹤每個步驟的執行狀態

實作:資料夾監控轉換


目標:監控特定資料夾,新的 .md 檔案自動轉換成 .docx


1開啟 Antigravity


2請 AI 幫你建立監控腳本

請幫我建立一個 PowerShell 腳本, 可以監控指定資料夾, 當有新的 .md 檔案出現時, 自動使用 Pandoc 轉換成 .docx

資料夾監控腳本

# watch-folder.ps1 param( [string]$WatchPath = "D:\待處理", [string]$OutputPath = "D:\已完成" ) # 確保輸出資料夾存在 if (!(Test-Path $OutputPath)) { New-Item -ItemType Directory -Path $OutputPath | Out-Null } Write-Host "開始監控: $WatchPath" -ForegroundColor Cyan Write-Host "輸出到: $OutputPath" -ForegroundColor Cyan Write-Host "按 Ctrl+C 停止`n" # 建立檔案監控器 $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $WatchPath $watcher.Filter = "*.md" $watcher.EnableRaisingEvents = $true # 當有新檔案時觸發 Register-ObjectEvent $watcher "Created" -Action { $file = $Event.SourceEventArgs.FullPath $name = $Event.SourceEventArgs.Name $output = "$using:OutputPath\$([System.IO.Path]::ChangeExtension($name, '.docx'))" Start-Sleep -Seconds 1 # 等待檔案寫入完成 pandoc $file -o $output Write-Host "已轉換: $name" -ForegroundColor Green } # 保持腳本運行 while ($true) { Start-Sleep -Seconds 1 }

定時任務設定


Windows 工作排程器

讓腳本在指定時間自動執行


# 使用 PowerShell 建立排程任務 $action = New-ScheduledTaskAction ` -Execute "PowerShell.exe" ` -Argument "-File D:\Scripts\daily-report.ps1" $trigger = New-ScheduledTaskTrigger ` -Daily -At "09:00" Register-ScheduledTask ` -TaskName "每日報告生成" ` -Action $action ` -Trigger $trigger ` -Description "每天早上 9 點自動生成報告"

範例:完整日報流程


1
收集資料
從資料庫或檔案讀取當天數據
2
AI 分析
使用 AI 生成摘要和洞察
3
生成報告
套用模板產生 Markdown
4
轉換格式
Pandoc 轉成 Word/PDF
5
發送通知
Email 或訊息通知相關人員

日報生成腳本

# daily-report.ps1 $date = Get-Date -Format "yyyy-MM-dd" $reportDir = "D:\報告\$date" # 建立目錄 New-Item -ItemType Directory -Path $reportDir -Force | Out-Null # 步驟 1: 收集資料 $data = @{ date = $date sales = Get-Content "D:\數據\sales.json" | ConvertFrom-Json tasks = Get-Content "D:\數據\tasks.csv" | ConvertFrom-Csv } # 步驟 2: 生成報告內容 $template = Get-Content "D:\模板\daily-template.md" -Raw $report = $template -replace '\{\{date\}\}', $date # 步驟 3: 儲存並轉換 $mdFile = "$reportDir\daily-report.md" $docxFile = "$reportDir\daily-report.docx" $report | Out-File $mdFile -Encoding UTF8 pandoc $mdFile -o $docxFile Write-Host "日報已生成: $docxFile" -ForegroundColor Green

與雲端服務整合


OneDrive / Google Drive 整合

將輸出檔案自動同步到雲端


# 設定輸出到 OneDrive 同步資料夾 $outputPath = "$env:USERPROFILE\OneDrive\報告" # 轉換後檔案會自動同步到雲端 pandoc report.md -o "$outputPath\report.docx" Write-Host "報告已儲存並同步到雲端"

技巧:只要將輸出目錄設在 OneDrive 或 Google Drive 的同步資料夾,檔案就會自動上傳!

與 Git 版本控制整合


# git-publish.ps1 # 文件修改後自動提交和發布 param([string]$Message = "更新文件") $docsPath = "D:\專案\docs" # 轉換所有文件 Get-ChildItem "$docsPath\*.md" | ForEach-Object { $output = [System.IO.Path]::ChangeExtension($_.FullName, ".html") pandoc $_.FullName -o $output --standalone } # Git 操作 Set-Location $docsPath git add . git commit -m $Message git push Write-Host "文件已發布!" -ForegroundColor Green

多格式輸出流程


一次轉換,多種格式輸出

# multi-format.ps1 param([string]$InputFile) $baseName = [System.IO.Path]::GetFileNameWithoutExtension($InputFile) $outputDir = "output" # 確保輸出目錄存在 New-Item -ItemType Directory -Path $outputDir -Force | Out-Null # 定義要輸出的格式 $formats = @{ "docx" = "" "html" = "--standalone --self-contained" "pdf" = "--pdf-engine=xelatex" } foreach ($format in $formats.Keys) { $output = "$outputDir\$baseName.$format" $args = $formats[$format] Write-Host "轉換為 $format..." -NoNewline Invoke-Expression "pandoc $InputFile -o $output $args" Write-Host " 完成" -ForegroundColor Green } Write-Host "`n所有格式已輸出到 $outputDir\"

條件式流程處理


根據檔案類型或內容決定處理方式

# smart-process.ps1 param([string]$InputFile) # 讀取檔案判斷類型 $content = Get-Content $InputFile -Raw -Encoding UTF8 # 根據內容決定處理方式 if ($content -match "^---") { Write-Host "偵測到 YAML 標頭,使用模板轉換" pandoc $InputFile -o output.docx --reference-doc=template.docx } elseif ($content -match "```") { Write-Host "偵測到程式碼,使用語法高亮" pandoc $InputFile -o output.html --highlight-style=monokai } else { Write-Host "一般文件,標準轉換" pandoc $InputFile -o output.docx }

管線式資料處理


將多個步驟串連起來

# pipeline-process.ps1 # 處理流程:收集 → 過濾 → 轉換 → 輸出 Get-ChildItem "D:\收件匣\*.md" | # 收集檔案 Where-Object { $_.Length -gt 100 } | # 過濾太小的 ForEach-Object { # 轉換每個檔案 $output = "D:\輸出\$($_.BaseName).docx" pandoc $_.FullName -o $output # 回傳處理結果 [PSCustomObject]@{ Source = $_.Name Output = $output Size = (Get-Item $output).Length } } | Export-Csv "處理報告.csv" -NoTypeInformation # 輸出報告 Write-Host "處理完成,請查看 處理報告.csv"

加入通知功能


Windows Toast 通知

# 完成後顯示桌面通知 $notify = New-Object -ComObject WScript.Shell $notify.Popup("報告已生成完成!", 5, "轉換完成", 64)

發送 Email (需要設定 SMTP)

# 發送通知信件 $params = @{ To = "manager@company.com" From = "system@company.com" Subject = "日報已生成 - $date" Body = "請查收附件中的日報" Attachments = $docxFile SmtpServer = "smtp.company.com" } Send-MailMessage @params

完整整合範例

# complete-workflow.ps1 # 完整的文件處理工作流程 param( [string]$InputFolder = "D:\待處理", [string]$OutputFolder = "D:\已完成", [switch]$Notify ) Write-Host "=== 文件處理工作流程 ===" -ForegroundColor Cyan $startTime = Get-Date # 1. 收集待處理檔案 $files = Get-ChildItem "$InputFolder\*.md" Write-Host "找到 $($files.Count) 個檔案待處理" # 2. 處理每個檔案 $results = @() foreach ($file in $files) { try { $output = "$OutputFolder\$($file.BaseName).docx" pandoc $file.FullName -o $output $results += [PSCustomObject]@{ File = $file.Name Status = "成功" } } catch { $results += [PSCustomObject]@{ File = $file.Name Status = "失敗: $_" } } } # 3. 輸出報告 $duration = (Get-Date) - $startTime Write-Host "`n處理完成,耗時 $($duration.TotalSeconds.ToString('F1')) 秒" # 4. 發送通知 if ($Notify) { $shell = New-Object -ComObject WScript.Shell $shell.Popup("處理完成!", 3, "工作流程", 64) }

實作練習


任務:建立個人工作流程


1選擇一個你常做的文件處理任務


2列出處理步驟(輸入→處理→輸出)


3用 Antigravity AI 幫你寫腳本


4測試並優化流程


5(進階)設定定時自動執行

用 AI 協助設計流程


在 Antigravity 中提問

我想要建立一個自動化流程: 1. 每天早上檢查特定資料夾的新檔案 2. 將 Markdown 檔案轉換成 Word 3. 移動到「已處理」資料夾 4. 產生處理報告 請幫我設計這個流程,並寫出完整的腳本

AI 會幫你:設計流程架構、撰寫腳本、加入錯誤處理

流程管理技巧


維護建議

  • 版本控制 - 將腳本納入 Git 管理
  • 文件記錄 - 在腳本開頭寫註解說明用途
  • 定期檢視 - 每月檢查流程是否還符合需求
  • 備份設定 - 保存排程設定和腳本副本

注意:自動化流程可能會持續運行,要確保不會產生意外的大量檔案或佔用過多資源

常見整合模式


1. 事件驅動

檔案變更、時間到達時觸發


2. 批次處理

累積一定數量後統一處理


3. 請求驅動

手動執行或 API 呼叫觸發


4. 持續整合

與開發流程結合,程式碼變更時自動更新文件

小測驗


哪個 PowerShell 類別可以用來監控資料夾變更?



本單元總結


學到的技巧

  • 資料夾監控自動轉換
  • Windows 排程任務設定
  • 與雲端服務整合
  • 多格式批次輸出
  • 管線式資料處理
  • 通知功能整合

恭喜!你已完成 Part 08 進階技巧與優化的學習!