Skip to content

Commit

Permalink
Add 3 new functions: set_sheet_view, set_sheet_visible and set_workbo…
Browse files Browse the repository at this point in the history
…ok_props

- Upgrade the dependencies package version
  • Loading branch information
xuri committed Dec 18, 2024
1 parent 27b7ea2 commit 32bf4f7
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 9 deletions.
69 changes: 67 additions & 2 deletions excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from types_py import *
from ctypes import (
byref,
c_bool,
c_char_p,
c_char,
c_int,
Expand Down Expand Up @@ -1635,6 +1634,72 @@ def set_sheet_row(
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_sheet_view(
self, sheet: str, view_index: int, opts: ViewOptions
) -> Optional[Exception]:
"""
Sets sheet view options. The viewIndex may be negative and if so is
counted backward (-1 is the last view).
Args:
sheet (str): The worksheet name
view_index (int): The sheet view index
opts (ViewOptions): The sheet view options
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.SetSheetView.restype = c_char_p
options = py_value_to_c(opts, types_go._ViewOptions())
err = lib.SetSheetView(
self.file_index, sheet.encode(ENCODE), view_index, byref(options)
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_sheet_visible(
self, sheet: str, visible: bool, *very_hidden: bool
) -> Optional[Exception]:
"""
Set worksheet visible by given worksheet name. A workbook must contain
at least one visible worksheet. If the given worksheet has been
activated, this setting will be invalidated. The third optional
very_hidden parameter only works when visible was false.
Args:
sheet (str): The worksheet name
visible (bool): The worksheet visibility
*very_hidden (bool): Optional boolean very hidden parameter
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.SetSheetVisible.restype = c_char_p
vh = False
if len(very_hidden) > 0:
vh = very_hidden[0]
err = lib.SetSheetVisible(
self.file_index, sheet.encode(ENCODE), visible, vh
).decode(ENCODE)
return None if err == "" else Exception(err)

def set_workbook_props(self, opts: WorkbookPropsOptions) -> Optional[Exception]:
"""
Sets workbook properties.
Args:
opts (WorkbookPropsOptions): TThe workbook property options
Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.
"""
lib.SetWorkbookProps.restype = c_char_p
options = py_value_to_c(opts, types_go._WorkbookPropsOptions())
err = lib.SetWorkbookProps(self.file_index, byref(options)).decode(ENCODE)
return None if err == "" else Exception(err)


def cell_name_to_coordinates(cell: str) -> Tuple[int, int, Optional[Exception]]:
"""
Expand Down Expand Up @@ -1709,7 +1774,7 @@ def coordinates_to_cell_name(
options = False
if len(abs) > 0:
options = abs[0]
res = lib.CoordinatesToCellName(col, row, c_bool(options))
res = lib.CoordinatesToCellName(col, row, options)
err = res.err.decode(ENCODE)
return res.cell.decode(ENCODE), None if err == "" else Exception(err)

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/xuri/excelize-py

go 1.18
go 1.20

require (
github.com/xuri/excelize/v2 v2.9.1-0.20241208033954-3ca60f8d2382
Expand All @@ -11,9 +11,9 @@ require (
github.com/richardlehane/mscfb v1.0.4 // indirect
github.com/richardlehane/msoleps v1.0.4 // indirect
github.com/tiendc/go-deepcopy v1.2.0 // indirect
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d // indirect
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 // indirect
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
golang.org/x/crypto v0.30.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.32.0 // indirect
golang.org/x/text v0.21.0 // indirect
)
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ github.com/richardlehane/msoleps v1.0.4/go.mod h1:BWev5JBpU9Ko2WAgmZEuiz4/u3ZYTK
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/tiendc/go-deepcopy v1.2.0 h1:6vCCs+qdLQHzFqY1fcPirsAWOmrLbuccilfp8UzD1Qo=
github.com/tiendc/go-deepcopy v1.2.0/go.mod h1:toXoeQoUqXOOS/X4sKuiAoSk6elIdqc0pN7MTgOOo2I=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d h1:llb0neMWDQe87IzJLS4Ci7psK/lVsjIS2otl+1WyRyY=
github.com/xuri/efp v0.0.0-20240408161823-9ad904a10d6d/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6 h1:8m6DWBG+dlFNbx5ynvrE7NgI+Y7OlZVMVTpayoW+rCc=
github.com/xuri/efp v0.0.0-20241211021726-c4e992084aa6/go.mod h1:ybY/Jr0T0GTCnYjKqmdwxyxn2BQf2RcQIIvex5QldPI=
github.com/xuri/excelize/v2 v2.9.1-0.20241208033954-3ca60f8d2382 h1:51czxsTwHg9JHFIiKuookS9dUtLUeCL7xoXTqF6BI6A=
github.com/xuri/excelize/v2 v2.9.1-0.20241208033954-3ca60f8d2382/go.mod h1:wFKkKpd4PMjXhJVhlEnMxKAxFlT+wMhayUoEMp1LR1g=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 h1:hPVCafDV85blFTabnqKgNhDCkJX25eik94Si9cTER4A=
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7/go.mod h1:WwHg+CVyzlv/TX9xqBFXEZAuxOPxn2k1GNHwG41IIUQ=
golang.org/x/crypto v0.30.0 h1:RwoQn3GkWiMkzlX562cLB7OxWvjH1L8xutO2WoJcRoY=
golang.org/x/crypto v0.30.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/image v0.23.0 h1:HseQ7c2OpPKTPVzNjG5fwJsOTCiiwS4QdsYi5XU6H68=
golang.org/x/image v0.23.0/go.mod h1:wJJBTdLfCCf3tiHa1fNxpZmUI4mmoZvwMCPP0ddoNKY=
golang.org/x/net v0.32.0 h1:ZqPmj8Kzc+Y6e0+skZsuACbx+wzMgo5MQsJh9Qd6aYI=
Expand Down
56 changes: 56 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1423,5 +1423,61 @@ func SetSheetRow(idx int, sheet, cell *C.char, row *C.struct_Interface, length i
return C.CString(errNil)
}

// SetSheetView sets sheet view options. The viewIndex may be negative and if
// so is counted backward (-1 is the last view).
//
//export SetSheetView
func SetSheetView(idx int, sheet *C.char, viewIndex int, opts *C.struct_ViewOptions) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.ViewOptions{}))
if err != nil {
return C.CString(err.Error())
}
options := goVal.Elem().Interface().(excelize.ViewOptions)
if err := f.(*excelize.File).SetSheetView(C.GoString(sheet), viewIndex, &options); err != nil {
C.CString(err.Error())
}
return C.CString(errNil)
}

// SetSheetVisible provides a function to set worksheet visible by given
// worksheet name. A workbook must contain at least one visible worksheet. If
// the given worksheet has been activated, this setting will be invalidated.
// The third optional veryHidden parameter only works when visible was false.
//
//export SetSheetVisible
func SetSheetVisible(idx int, sheet *C.char, visible, veryHidden bool) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
if err := f.(*excelize.File).SetSheetVisible(C.GoString(sheet), visible, veryHidden); err != nil {
C.CString(err.Error())
}
return C.CString(errNil)
}

// SetWorkbookProps provides a function to sets workbook properties.
//
//export SetWorkbookProps
func SetWorkbookProps(idx int, opts *C.struct_WorkbookPropsOptions) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(errFilePtr)
}
goVal, err := cValueToGo(reflect.ValueOf(*opts), reflect.TypeOf(excelize.WorkbookPropsOptions{}))
if err != nil {
return C.CString(err.Error())
}
options := goVal.Elem().Interface().(excelize.WorkbookPropsOptions)
if err := f.(*excelize.File).SetWorkbookProps(&options); err != nil {
return C.CString(err.Error())
}
return C.CString(errNil)
}

func main() {
}
35 changes: 35 additions & 0 deletions test_excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,41 @@ def test_add_picture(self):
self.assertIsNone(f.save_as("TestAddPicture.xlsx"))
self.assertIsNone(f.close())

def test_sheet_view(self):
f = excelize.new_file()
expected = excelize.ViewOptions(
default_grid_color=False,
right_to_left=False,
show_formulas=False,
show_grid_lines=False,
show_row_col_headers=False,
show_ruler=False,
show_zeros=False,
top_left_cell="A1",
view="normal",
zoom_scale=120,
)
self.assertIsNone(f.set_sheet_view("Sheet1", 0, expected))
self.assertIsNone(f.save_as("TestSheetView.xlsx"))
self.assertIsNone(f.close())

def test_sheet_visible(self):
f = excelize.new_file()
_, err = f.new_sheet("Sheet2")
self.assertIsNone(err)
self.assertIsNone(f.set_sheet_visible("Sheet2", False, True))
self.assertIsNone(f.save_as("TestSheetVisible.xlsx"))
self.assertIsNone(f.close())

def test_workbook_props(self):
f = excelize.new_file()
expected = excelize.WorkbookPropsOptions(
date1904=True, filter_privacy=True, code_name="code"
)
self.assertIsNone(f.set_workbook_props(expected))
self.assertIsNone(f.save_as("TestWorkbookProps.xlsx"))
self.assertIsNone(f.close())

def test_type_convert(self):
class _T2(Structure):
_fields_ = [
Expand Down
42 changes: 42 additions & 0 deletions types_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,48 @@ struct Table
bool *ShowRowStripes;
};

// ViewOptions directly maps the settings of sheet view.
struct ViewOptions
{
bool *DefaultGridColor;
bool *RightToLeft;
bool *ShowFormulas;
bool *ShowGridLines;
bool *ShowRowColHeaders;
bool *ShowRuler;
bool *ShowZeros;
char **TopLeftCell;
char **View;
double *ZoomScale;
};

// DefinedName directly maps the name for a cell or cell range on a
// worksheet.
struct DefinedName
{
char *Name;
char *Comment;
char *RefersTo;
char *Scope;
};

// WorkbookPropsOptions directly maps the settings of workbook proprieties.
struct WorkbookPropsOptions
{
bool *Date1904;
bool *FilterPrivacy;
char **CodeName;
};

// WorkbookProtectionOptions directly maps the settings of workbook protection.
struct WorkbookProtectionOptions
{
char *AlgorithmName;
char *Password;
bool LockStructure;
bool LockWindows;
};

struct GetCellFormulaResult
{
char *val;
Expand Down
41 changes: 41 additions & 0 deletions types_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,47 @@ class _Table(Structure):
]


class _ViewOptions(Structure):
_fields_ = [
("DefaultGridColor", POINTER(c_bool)),
("RightToLeft", POINTER(c_bool)),
("ShowFormulas", POINTER(c_bool)),
("ShowGridLines", POINTER(c_bool)),
("ShowRowColHeaders", POINTER(c_bool)),
("ShowRuler", POINTER(c_bool)),
("ShowZeros", POINTER(c_bool)),
("TopLeftCell", POINTER(c_char_p)),
("View", POINTER(c_char_p)),
("ZoomScale", POINTER(c_double)),
]


class _DefinedName(Structure):
_fields_ = [
("Name", c_char_p),
("Comment", c_char_p),
("RefersTo", c_char_p),
("Scope", c_char_p),
]


class _WorkbookPropsOptions(Structure):
_fields_ = [
("Date1904", POINTER(c_bool)),
("FilterPrivacy", POINTER(c_bool)),
("CodeName", POINTER(c_char_p)),
]


class _WorkbookProtectionOptions(Structure):
_fields_ = [
("AlgorithmName", c_char_p),
("Password", c_char_p),
("LockStructure", c_bool),
("LockWindows", c_bool),
]


class _CalcCellValueResult(Structure):
_fields_ = [
("val", c_char_p),
Expand Down
37 changes: 37 additions & 0 deletions types_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,40 @@ class Table:
show_header_row: Optional[bool] = None
show_last_column: bool = False
show_row_stripes: Optional[bool] = None


@dataclass
class ViewOptions:
default_grid_color: Optional[bool] = None
right_to_left: Optional[bool] = None
show_formulas: Optional[bool] = None
show_grid_lines: Optional[bool] = None
show_row_col_headers: Optional[bool] = None
show_ruler: Optional[bool] = None
show_zeros: Optional[bool] = None
top_left_cell: Optional[str] = None
view: Optional[str] = None
zoom_scale: Optional[float] = None


@dataclass
class DefinedName:
name: str = ""
comment: str = ""
refers_to: str = ""
scope: str = ""


@dataclass
class WorkbookPropsOptions:
date1904: Optional[bool] = None
filter_privacy: Optional[bool] = None
code_name: Optional[str] = None


@dataclass
class WorkbookProtectionOptions:
algorithm_name: str = ""
password: str = ""
lock_structure: Optional[bool] = None
lock_windows: Optional[bool] = None

0 comments on commit 32bf4f7

Please sign in to comment.