「幫我從 sales.csv 製作區域銷售報告」是一個語言模型會很樂意回應的請求,它會輸出 Markdown 表格、四捨五入幾個數字,並描述它無法繪製的圖表。當 GroupDocs.Assembly.Mcp 在本機執行 (dnx GroupDocs.Assembly.Mcp --yes) 時,Claude Desktop、Cursor 或 Copilot 會把資料檔交給引擎:你的 Word 或 PowerPoint 範本已經包含表格、總計與真實圖表,伺服器會直接從 CSV 或試算表填入資料,數字根本不會經過模型:
從 sales.xlsx 建立區域銷售報告,將 sales-report.docx 輸出為 PDF,並將圖表投影片使用 sales-chart.pptx。
一步一步的設定與除錯說明請參考文件:How to build a report with tables and charts from a CSV or spreadsheet with an AI agent.
為什麼要讓數字繞過模型?
因為模型是文字產生器,而報告是算術加版面配置。當代理程式自行讀取 CSV 並撰寫報告時,每個值都是透過下一個 token 的預測重新輸入。大多數情況下會正確,唯一出錯的地方在於除非有人對照來源檢查,否則錯誤是隱形的,這正是報告本應省去的工作。圖表更糟:模型只能描述或產生程式碼,但你每季簡報中的圖表是 PowerPoint 檔案中的物件,由行銷人員設計樣式。
因此,將工作依各自擅長的部分分配。範本決定報告的外觀;引擎讀取檔案並執行計算;代理程式挑選範本與資料,並告訴你最終結果。
代理程式傳遞什麼,引擎看到什麼
以下表格說明分工:
the agent decides the engine does the template owns
----------------------- ------------------------- -----------------------
which template reads sales.xlsx/.csv the table layout
which data file types the columns the chart and its style
output format (pdf) sums, sorts, picks max headings, fonts, logo
fills table rows, chart what "total" means
在產生之前,代理程式可以先檢視資料檔。對於我們的試算表,伺服器回報一個包含三個欄位的表格:
-> get_document_info {"file":{"filePath":"sales.xlsx"}}
<- { "dataKind": "table",
"data": { "tableCount": 1, "tables": [ { "number": 1,
"columns": [ "Region", "Quarter", "Revenue" ] } ] } }
表格報告
Word 範本在第一個儲存格內有 <<foreach [r in sales]>>,最後一個儲存格有 <</foreach>>,其下還有兩行:<<[sales.Sum(r => r.Revenue)]>> 與 <<[sales.OrderByDescending(r => r.Revenue).First().Region]>>。一次呼叫如下:
-> assemble_document {"template":{"filePath":"sales-report.docx"},
"data":{"filePath":"sales.xlsx"},"format":"pdf"}
<- Assembled 'sales-report.docx' with 'sales.xlsx' (table 1 (3
column(s): Region, Quarter, Revenue) as 'sales') as PDF
Saved to: .../sales-report_assembled.pdf
報告產生四列(North 182000、South 146500、East 98250、West 211900),「Total revenue: 638650 EUR」 與 「Best region: West」。伺服器將資料命名為 sales,因為範本就是以此名稱迴圈;不需要額外說明。相同的範本使用 sales.csv 也會得到相同的總計;對於 CSV,hasHeaders 與 csvDelimiter 可處理沒有標頭列或使用分號的檔案。
圖表
GroupDocs.Assembly 範本中的圖表是一個普通圖表,只是在兩個位置加入標記。圖表標題寫成 Revenue by region<<foreach [r in sales]>><<x [r.Region]>>:對每一列迴圈,一個類別對應一個區域。系列名稱寫成 Revenue<<y [r.Revenue]>>:每列一個數值。圖表內的佔位資料僅供設計師預覽。
-> assemble_document {"template":{"filePath":"sales-chart.pptx"},
"data":{"filePath":"sales.xlsx"}}
<- Assembled 'sales-chart.pptx' with 'sales.xlsx' (table 1 (3
column(s): Region, Quarter, Revenue) as 'sales') as PPTX
Saved to: .../sales-chart_assembled.pptx
我們打開結果:一個標題為 Revenue by region 的柱狀圖,類別為 North、South、East、West,數值分別為 182000、146500、98250、211900,標記已移除且範本的樣式保持不變。相同的標記也適用於 Word 與 Excel 圖表。
為什麼不請代理程式產生圖表圖片?
可以這麼做,對於一次性的聊天回覆沒問題。但在報告中會有三個問題:圖片是靜態圖,無法點擊檢查數值;其樣式是繪圖程式的預設,而非你的企業樣式;而且圖上的數字已經透過模型傳遞過。範本圖表則是原生圖表物件,直接從檔案填入,樣式只由範本擁有者設定一次。
誠實的限制
- 表格與圖表標記必須事先寫入範本;代理程式不會自行設計它們。
get_template_fields會顯示範本會讀取哪些資料。 - 評估模式:每個輸出都會以 「Evaluation Only. Created with GroupDocs.Assembly 26.7…」 開頭,且儲存的活頁簿會多出一個工作表。
- 若資料中某個列表為空,將沒有欄位類型,算術運算會失敗,即使使用
allowMissingMembers。請確認你要求的過濾條件會回傳列。 - 在 Linux 上使用的簡報範本需要
libgdiplus,執行dnx時必須安裝;Docker 映像已內建此套件。 - 資料檔中的日期:請保留為日期格式或完整的 ISO 時間,並在範本中進行格式化。
FAQ
AI 代理程式能在不上傳 CSV 的情況下製作圖表嗎?
可以。伺服器在本機執行,直接從你的儲存資料夾讀取 CSV 或試算表,並在那裡產生報告;數字從檔案直接寫入文件,未經模型。
範本可以讀取哪些資料檔?
JSON、XML、CSV、TSV,以及試算表、Word 或 PowerPoint 文件中的表格;當有多個表格時,dataTable 會選取其中一個。
報告可以是 Excel 檔而不是 PDF 嗎?
可以,使用 XLSX 範本;標記寫入工作表儲存格。評估模式下,儲存的活頁簿會多出一個工作表。
更深入了解
- 文件、官方教學:How to build a report with tables and charts from a CSV or spreadsheet with an AI agent
- 文件中心:GroupDocs.Assembly MCP Server
- 入門指南:Let the model fill in the data, not write the document
- 相關文章:A hundred personalised PDFs from one prompt: mail merge with an AI agent
- 相關文章:What does this template need? Ask it.
- 相關文章:Your repo already has the data; let the agent make the PDF
- 本地部署與安全模型:3 architectures for AI document processing, and the one that keeps files inside your network
- 問題討論:GroupDocs Assembly forum
- 原始碼:GroupDocs.Assembly.Mcp on GitHub