From cae4d063731e22442df6f232233427368db3e6ec Mon Sep 17 00:00:00 2001 From: Max Date: Sat, 29 Mar 2025 18:16:58 +0800 Subject: [PATCH] feat: Add sheet existence check to Excel module - Implement processSheetExists function to verify if a specified sheet exists in the workbook. - Update README.md with usage examples for the new sheet existence check feature. - Add unit tests for SheetExists functionality to ensure accurate detection of existing and non-existent sheets. --- excel/README.md | 12 ++++++++++++ excel/process.go | 15 +++++++++++++++ excel/sheet.go | 6 ++++++ excel/sheet_test.go | 11 +++++++++++ 4 files changed, 44 insertions(+) diff --git a/excel/README.md b/excel/README.md index e5624beb..e31122db 100644 --- a/excel/README.md +++ b/excel/README.md @@ -130,6 +130,18 @@ Process("excel.sheet.copy", h, "Sheet1", "Sheet1Copy"); Process("excel.sheet.delete", h, "Sheet1Copy"); ``` +#### Check if a sheet exists + +```typescript +/** + * Checks if a sheet exists in the workbook + * @param handle - Handle ID from excel.open + * @param name - Sheet name to check + * @returns boolean - true if sheet exists, false otherwise + */ +const exists: boolean = Process("excel.sheet.exists", h, "Sheet1"); +``` + ### Example: Sheet Operations Workflow ```typescript diff --git a/excel/process.go b/excel/process.go index 1dd6ca3b..f827dd86 100644 --- a/excel/process.go +++ b/excel/process.go @@ -19,6 +19,7 @@ func init() { "sheet.delete": processDeleteSheet, "sheet.copy": processCopySheet, "sheet.list": processListSheets, + "sheet.exists": processSheetExists, "read.cell": processReadCell, "read.row": processReadRow, @@ -721,3 +722,17 @@ func processListSheets(process *process.Process) interface{} { return xls.ListSheets() } + +// processSheetExists process the excel.sheet.exists +func processSheetExists(process *process.Process) interface{} { + process.ValidateArgNums(2) + handle := process.ArgsString(0) + name := process.ArgsString(1) + + xls, err := Get(handle) + if err != nil { + exception.New("excel.sheet.exists %s error: %s", 500, handle, err.Error()).Throw() + } + + return xls.SheetExists(name) +} diff --git a/excel/sheet.go b/excel/sheet.go index 23ebd70e..e78595ad 100644 --- a/excel/sheet.go +++ b/excel/sheet.go @@ -121,6 +121,12 @@ func (excel *Excel) ListSheets() []string { return excel.GetSheetList() } +// SheetExists checks if a sheet exists in the workbook +func (excel *Excel) SheetExists(name string) bool { + idx, _ := excel.GetSheetIndex(name) + return idx != -1 +} + // CopySheet copies a sheet to a new name func (excel *Excel) CopySheet(source, destination string) error { // Validate destination sheet name diff --git a/excel/sheet_test.go b/excel/sheet_test.go index 5e9338b9..a6b1d5cc 100644 --- a/excel/sheet_test.go +++ b/excel/sheet_test.go @@ -84,6 +84,17 @@ func TestSheetOperations(t *testing.T) { assert.Contains(t, sheets, "NewSheet") }) + // Test SheetExists + t.Run("SheetExists", func(t *testing.T) { + // Check existing sheet + exists := excel.SheetExists("TestSheet1") + assert.True(t, exists) + + // Check non-existent sheet + exists = excel.SheetExists("NonExistentSheet") + assert.False(t, exists) + }) + // Test CopySheet t.Run("CopySheet", func(t *testing.T) { // Copy existing sheet