Merge pull request #912 from trheyi/main
feat: Add sheet existence check to Excel module
This commit is contained in:
commit
33927699c5
4 changed files with 44 additions and 0 deletions
|
|
@ -130,6 +130,18 @@ Process("excel.sheet.copy", h, "Sheet1", "Sheet1Copy");
|
||||||
Process("excel.sheet.delete", h, "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
|
### Example: Sheet Operations Workflow
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ func init() {
|
||||||
"sheet.delete": processDeleteSheet,
|
"sheet.delete": processDeleteSheet,
|
||||||
"sheet.copy": processCopySheet,
|
"sheet.copy": processCopySheet,
|
||||||
"sheet.list": processListSheets,
|
"sheet.list": processListSheets,
|
||||||
|
"sheet.exists": processSheetExists,
|
||||||
|
|
||||||
"read.cell": processReadCell,
|
"read.cell": processReadCell,
|
||||||
"read.row": processReadRow,
|
"read.row": processReadRow,
|
||||||
|
|
@ -721,3 +722,17 @@ func processListSheets(process *process.Process) interface{} {
|
||||||
|
|
||||||
return xls.ListSheets()
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -121,6 +121,12 @@ func (excel *Excel) ListSheets() []string {
|
||||||
return excel.GetSheetList()
|
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
|
// CopySheet copies a sheet to a new name
|
||||||
func (excel *Excel) CopySheet(source, destination string) error {
|
func (excel *Excel) CopySheet(source, destination string) error {
|
||||||
// Validate destination sheet name
|
// Validate destination sheet name
|
||||||
|
|
|
||||||
|
|
@ -84,6 +84,17 @@ func TestSheetOperations(t *testing.T) {
|
||||||
assert.Contains(t, sheets, "NewSheet")
|
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
|
// Test CopySheet
|
||||||
t.Run("CopySheet", func(t *testing.T) {
|
t.Run("CopySheet", func(t *testing.T) {
|
||||||
// Copy existing sheet
|
// Copy existing sheet
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue