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.
This commit is contained in:
Max 2025-03-29 18:16:58 +08:00
parent 5d01f62a9e
commit cae4d06373
4 changed files with 44 additions and 0 deletions

View file

@ -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

View file

@ -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 <handle> <name>
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)
}

View file

@ -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

View file

@ -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