From 38078b53101c661849325862a9c6add366c29608 Mon Sep 17 00:00:00 2001 From: Star <> Date: Tue, 12 May 2026 13:50:07 +0800 Subject: [PATCH] Initialize and optimize document module (v1.0.3) by AI --- TEST.md | 19 +++++++++++++++++++ office.go => document.go | 0 docx.go | 4 ++-- excel.go | 5 ++--- excel_test.go | 13 +++++++++++++ pdf.go | 4 ++-- pptx.go | 4 ++-- 7 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 TEST.md rename office.go => document.go (100%) diff --git a/TEST.md b/TEST.md new file mode 100644 index 0000000..3fd3127 --- /dev/null +++ b/TEST.md @@ -0,0 +1,19 @@ +# document TEST + +## 覆盖场景 +- [x] **Excel 基础读写**: 验证 `NewExcel`, `Set`, `Get`, `Save`, `OpenExcel`。 +- [x] **Excel 对象映射**: 验证 `SetData`, `GetData` 及动态列扩展。 +- [x] **统一 API**: 验证 `document.Open` 自动识别、`ToJSON` 和 `ToMarkdown`。 +- [x] **ID 生成解析**: 验证 `MakeCellID` 和 `ParseCellID` 的准确性。 + +## 性能测试 (Benchmark) +- **环境**: Darwin / Intel(R) Core(TM) i7-1068NG7 CPU @ 2.30GHz +- **Excel.SetData**: 写入 100 行对象数据。 + +```text +BenchmarkExcel_SetData-8 712 1673885 ns/op +``` + +## 基础设施对齐 +- 全面使用 `apigo.cc/go/cast` 替代原生类型转换。 +- 移除了所有原生 `os` 和 `strconv` 依赖。 diff --git a/office.go b/document.go similarity index 100% rename from office.go rename to document.go diff --git a/docx.go b/docx.go index a6b1acf..3d8fcf5 100644 --- a/docx.go +++ b/docx.go @@ -1,8 +1,8 @@ package document import ( + "fmt" "io" - "os" "apigo.cc/go/cast" "apigo.cc/go/file" @@ -19,7 +19,7 @@ type Docx struct { // OpenDocx 打开一个 Word 文档 (.docx)。 func OpenDocx(filename string) (*Docx, error) { if !file.Exists(filename) { - return nil, os.ErrNotExist + return nil, fmt.Errorf("file not found: %s", filename) } d := &Docx{ filename: filename, diff --git a/excel.go b/excel.go index da6b80d..efbe208 100644 --- a/excel.go +++ b/excel.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "regexp" - "strconv" "strings" "apigo.cc/go/cast" @@ -278,7 +277,7 @@ func (xls *Excel) getOrCreateSheet(name string) string { // MakeCellID 生成单元格 ID。 func MakeCellID(col, row int) string { - return MakeColID(col) + strconv.Itoa(row+1) + return MakeColID(col) + cast.To[string](row+1) } // MakeColID 生成列 ID。 @@ -307,7 +306,7 @@ func ParseCellID(cell string) (col, row int) { return parseCol(cell), 0 } col = parseCol(cell[:numIdx]) - row, _ = strconv.Atoi(cell[numIdx:]) + row = cast.To[int](cell[numIdx:]) row-- if row < 0 { row = 0 diff --git a/excel_test.go b/excel_test.go index 85cf770..207e464 100644 --- a/excel_test.go +++ b/excel_test.go @@ -144,6 +144,19 @@ func TestIDGeneration(t *testing.T) { } // Helper for test assertions +func BenchmarkExcel_SetData(b *testing.B) { + xls := NewExcel() + data := make([]map[string]any, 100) + for i := 0; i < 100; i++ { + data[i] = map[string]any{"ID": i, "Name": "User", "Value": i * 10} + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = xls.SetData("Bench", data, "A1", "") + } +} + func castTo[T any](v any) T { var res T if val, ok := v.(T); ok { diff --git a/pdf.go b/pdf.go index 37e7ff6..9718f4a 100644 --- a/pdf.go +++ b/pdf.go @@ -2,8 +2,8 @@ package document import ( "bytes" + "fmt" "io" - "os" "strings" "apigo.cc/go/cast" @@ -21,7 +21,7 @@ type PDF struct { // OpenPDF 打开一个 PDF 文档。 func OpenPDF(filename string) (*PDF, error) { if !file.Exists(filename) { - return nil, os.ErrNotExist + return nil, fmt.Errorf("file not found: %s", filename) } p := &PDF{ filename: filename, diff --git a/pptx.go b/pptx.go index e7792d6..3fae3a3 100644 --- a/pptx.go +++ b/pptx.go @@ -1,8 +1,8 @@ package document import ( + "fmt" "io" - "os" "apigo.cc/go/cast" "apigo.cc/go/file" @@ -19,7 +19,7 @@ type Pptx struct { // OpenPptx 打开一个 PowerPoint 文档 (.pptx)。 func OpenPptx(filename string) (*Pptx, error) { if !file.Exists(filename) { - return nil, os.ErrNotExist + return nil, fmt.Errorf("file not found: %s", filename) } p := &Pptx{ filename: filename,