Skip to content

Commit

Permalink
Add 4 new functions: delete_defined_name, get_cell_style, remove_page…
Browse files Browse the repository at this point in the history
…_break and remove_row

- Update unit tests and docs for the function
  • Loading branch information
xuri committed Jan 3, 2025
1 parent 0e11565 commit 3c9a9d2
Show file tree
Hide file tree
Showing 8 changed files with 196 additions and 15 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2024 The excelize Authors.
Copyright (c) 2024 - 2025 The excelize Authors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
98 changes: 96 additions & 2 deletions excelize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
code is governed by a BSD-style license that can be found in the LICENSE file.
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
source code is governed by a BSD-style license that can be found in the LICENSE
file.
Package excelize-py is a Python port of Go Excelize library, providing a set of
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
Expand Down Expand Up @@ -1079,6 +1080,33 @@ def delete_comment(self, sheet: str, cell: str) -> Optional[Exception]:
).decode(ENCODE)
return None if err == "" else Exception(err)

def delete_defined_name(self, defined_name: DefinedName) -> Optional[Exception]:
"""
Delete the defined names of the workbook or worksheet. If not specified
scope, the default scope is workbook.
Args:
defined_name (DefinedName): The defined name options
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
For example:
.. code-block:: python
err = f.delete_defined_name(excelize.DefinedName(
name="Amount",
scope="Sheet2",
))
"""
lib.DeleteDefinedName.restype = c_char_p
options = py_value_to_c(defined_name, types_go._DefinedName())
err = lib.DeleteDefinedName(self.file_index, byref(options)).decode(ENCODE)
return None if err == "" else Exception(err)

def delete_picture(self, sheet: str, cell: str) -> Optional[Exception]:
"""
Delete all pictures in a cell by given worksheet name and cell reference.
Expand Down Expand Up @@ -1265,6 +1293,25 @@ def get_cell_hyperlink(
None if err == "" else Exception(err),
)

def get_cell_style(self, sheet: str, cell: str) -> Tuple[int, Optional[Exception]]:
"""
Get cell style index by given worksheet name and cell reference.
Args:
sheet (str): The worksheet name
cell (str): The cell reference
Returns:
Tuple[int, Optional[Exception]]: A tuple containing the cell style,
and an Exception object if an error occurred, otherwise None.
"""
lib.GetCellStyle.restype = types_go._IntErrorResult
res = lib.GetCellStyle(
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE)
)
err = res.err.decode(ENCODE)
return res.val, None if err == "" else Exception(err)

def get_cell_value(
self, sheet: str, cell: str, *opts: Options
) -> Tuple[str, Optional[Exception]]:
Expand Down Expand Up @@ -1650,6 +1697,53 @@ def remove_col(self, sheet: str, col: str) -> Optional[Exception]:
).decode(ENCODE)
return None if err == "" else Exception(err)

def remove_page_break(self, sheet: str, cell: str) -> Optional[Exception]:
"""
Remove a page break by given worksheet name and cell reference.
Args:
sheet (str): The worksheet name
cell (str): The cell reference
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.RemovePageBreak.restype = c_char_p
err = lib.RemovePageBreak(
self.file_index, sheet.encode(ENCODE), cell.encode(ENCODE)
).decode(ENCODE)
return None if err == "" else Exception(err)

def remove_row(self, sheet: str, row: int) -> Optional[Exception]:
"""
Remove single row by given worksheet name and Excel row number. Use this
method with caution, which will affect changes in references such as
formulas, charts, and so on. If there is any referenced value of the
worksheet, it will cause a file error when you open it. The excelize
only partially updates these references currently.
Args:
sheet (str): The worksheet name
row (int): The row number
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
Example:
For example, remove row 3 in Sheet1:
.. code-block:: python
err = f.remove_row("Sheet1", 3)
"""
lib.RemoveRow.restype = c_char_p
err = lib.RemoveRow(self.file_index, sheet.encode(ENCODE), c_int(row)).decode(
ENCODE
)
return None if err == "" else Exception(err)

def set_active_sheet(self, index: int) -> Optional[Exception]:
"""
Set the default active sheet of the workbook by a given index. Note that
Expand Down
73 changes: 71 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2024 The excelize Authors. All rights reserved. Use of this source
// code is governed by a BSD-style license that can be found in the LICENSE file.
// Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package excelize-py is a Python port of Go Excelize library, providing a set
// of functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM
Expand Down Expand Up @@ -881,6 +882,28 @@ func DeleteComment(idx int, sheet, cell *C.char) *C.char {
return C.CString(emptyString)
}

// DeleteDefinedName provides a function to delete the defined names of the
// workbook or worksheet. If not specified scope, the default scope is
// workbook.
//
//export DeleteDefinedName
func DeleteDefinedName(idx int, definedName *C.struct_DefinedName) *C.char {
var df excelize.DefinedName
goVal, err := cValueToGo(reflect.ValueOf(*definedName), reflect.TypeOf(excelize.DefinedName{}))
if err != nil {
return C.CString(err.Error())
}
df = goVal.Elem().Interface().(excelize.DefinedName)
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).DeleteDefinedName(&df); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}

// DeletePicture provides a function to delete charts in spreadsheet by given
// worksheet name and cell reference. Note that the image file won't be
// deleted from the document currently.
Expand Down Expand Up @@ -1031,6 +1054,22 @@ func GetCellHyperLink(idx int, sheet, cell *C.char) C.struct_GetCellHyperLinkRes
return C.struct_GetCellHyperLinkResult{link: C._Bool(link), target: C.CString(target), err: C.CString(emptyString)}
}

// GetCellStyle provides a function to get cell style index by given worksheet
// name and cell reference. This function is concurrency safe.
//
//export GetCellStyle
func GetCellStyle(idx int, sheet, cell *C.char) C.struct_IntErrorResult {
f, ok := files.Load(idx)
if !ok {
return C.struct_IntErrorResult{val: C.int(0), err: C.CString(errFilePtr)}
}
idx, err := f.(*excelize.File).GetCellStyle(C.GoString(sheet), C.GoString(cell))
if err != nil {
return C.struct_IntErrorResult{val: C.int(idx), err: C.CString(err.Error())}
}
return C.struct_IntErrorResult{val: C.int(idx), err: C.CString(emptyString)}
}

// GetCellValue provides a function to get formatted value from cell by given
// worksheet name and cell reference in spreadsheet. The return value is
// converted to the `string` data type. If the cell format can be applied to
Expand Down Expand Up @@ -1430,6 +1469,36 @@ func RemoveCol(idx int, sheet, col *C.char) *C.char {
return C.CString(emptyString)
}

// RemovePageBreak remove a page break by given worksheet name and cell
// reference.
//
//export RemovePageBreak
func RemovePageBreak(idx int, sheet, cell *C.char) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).RemovePageBreak(C.GoString(sheet), C.GoString(cell)); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}

// RemoveRow provides a function to remove single row by given worksheet name
// and Excel row number.
//
//export RemoveRow
func RemoveRow(idx int, sheet *C.char, row int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).RemoveRow(C.GoString(sheet), row); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}

// Save provides a function to override the spreadsheet with origin path.
//
//export Save
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
code is governed by a BSD-style license that can be found in the LICENSE file.
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
source code is governed by a BSD-style license that can be found in the LICENSE
file.
Package excelize-py is a Python port of Go Excelize library, providing a set of
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
Expand Down
18 changes: 16 additions & 2 deletions test_excelize.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
code is governed by a BSD-style license that can be found in the LICENSE file.
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
source code is governed by a BSD-style license that can be found in the LICENSE
file.
Package excelize-py is a Python port of Go Excelize library, providing a set of
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
Expand Down Expand Up @@ -118,6 +119,9 @@ def test_style(self):
self.assertIsNone(err)
self.assertEqual(style, s)
self.assertIsNone(f.set_cell_style("Sheet1", "A1", "B2", style_id))
result, err = f.get_cell_style("Sheet1", "A2")
self.assertIsNone(err)
self.assertEqual(result, style_id)
self.assertEqual(
str(f.set_cell_style("SheetN", "A1", "B2", style_id)),
"sheet SheetN does not exist",
Expand Down Expand Up @@ -289,6 +293,8 @@ def test_style(self):
)
self.assertIsNone(f.move_sheet("Sheet2", "Sheet1"))
self.assertIsNone(f.remove_col("Sheet1", "Z"))
self.assertIsNone(f.remove_page_break("Sheet1", "A1"))
self.assertIsNone(f.remove_row("Sheet1", 100))
self.assertIsNone(f.ungroup_sheets())
self.assertIsNone(f.update_linked_value())
self.assertIsNone(f.save())
Expand Down Expand Up @@ -1032,6 +1038,14 @@ def test_defined_name(self):
)
)
)
self.assertIsNone(
f.delete_defined_name(
excelize.DefinedName(
name="Amount",
scope="Sheet1",
)
)
)
self.assertIsNone(f.save_as(os.path.join("test", "TestSetDefinedName.xlsx")))
self.assertIsNone(f.close())

Expand Down
5 changes: 3 additions & 2 deletions types_c.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2024 The excelize Authors. All rights reserved. Use of this source
// code is governed by a BSD-style license that can be found in the LICENSE file.
// Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package excelize-py is a Python port of Go Excelize library, providing a set
// of functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM
Expand Down
5 changes: 3 additions & 2 deletions types_go.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
code is governed by a BSD-style license that can be found in the LICENSE file.
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
source code is governed by a BSD-style license that can be found in the LICENSE
file.
Package excelize-py is a Python port of Go Excelize library, providing a set of
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
Expand Down
5 changes: 3 additions & 2 deletions types_py.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Copyright 2024 The excelize Authors. All rights reserved. Use of this source
code is governed by a BSD-style license that can be found in the LICENSE file.
"""Copyright 2024 - 2025 The excelize Authors. All rights reserved. Use of this
source code is governed by a BSD-style license that can be found in the LICENSE
file.
Package excelize-py is a Python port of Go Excelize library, providing a set of
functions that allow you to write and read from XLAM / XLSM / XLSX / XLTM / XLTX
Expand Down

0 comments on commit 3c9a9d2

Please sign in to comment.