Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -1727,6 +1727,22 @@ def get_tables(self, sheet: str) -> Tuple[List[Table], Optional[Exception]]:
err = res.Err.decode(ENCODE)
return tables if tables else [], None if err == "" else Exception(err)

def get_workbook_props(self) -> Tuple[WorkbookPropsOptions, Optional[Exception]]:
"""
Get all tables in a worksheet by given worksheet name.

Returns:
Tuple[List[WorkbookPropsOptions], Optional[Exception]]: A tuple
containing the workbook property options and an exception if an
error occurred, otherwise None.
"""
lib.GetWorkbookProps.restype = types_go._GetWorkbookPropsResult
res = lib.GetWorkbookProps(self.file_index)
err = res.err.decode(ENCODE)
return c_value_to_py(res.opts, WorkbookPropsOptions()) if err == "" else None, (
None if err == "" else Exception(err)
)

def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]:
"""
Insert new columns before the given column name and number of columns.
Expand Down Expand Up @@ -1760,6 +1776,39 @@ def insert_cols(self, sheet: str, col: str, n: int) -> Optional[Exception]:
).decode(ENCODE)
return None if err == "" else Exception(err)

def insert_rows(self, sheet: str, row: int, n: int) -> Optional[Exception]:
"""
Insert new rows after the given Excel row number starting from 1 and
number of rows. 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
n (int): The rows

Returns:
Optional[Exception]: Returns None if no error occurred,
otherwise returns an Exception with the message.

Example:
For example, create two rows before row 3 in Sheet1:

.. code-block:: python

err = f.insert_rows("Sheet1", 3, 2)
"""
lib.InsertRows.restype = c_char_p
err = lib.InsertRows(
self.file_index,
sheet.encode(ENCODE),
c_int(row),
c_int(n),
).decode(ENCODE)
return None if err == "" else Exception(err)

def merge_cell(
self, sheet: str, top_left_cell: str, bottom_right_cell: str
) -> Optional[Exception]:
Expand Down Expand Up @@ -3244,7 +3293,7 @@ def set_workbook_props(self, opts: WorkbookPropsOptions) -> Optional[Exception]:
Sets workbook properties.

Args:
opts (WorkbookPropsOptions): TThe workbook property options
opts (WorkbookPropsOptions): The workbook property options

Returns:
Optional[Exception]: Returns None if no error occurred,
Expand Down
35 changes: 34 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,25 @@ func GetTables(idx int, sheet *C.char) C.struct_GetTablesResult {
return C.struct_GetTablesResult{TablesLen: C.int(len(tables)), Tables: (*C.struct_Table)(cArray), Err: C.CString(emptyString)}
}

// GetWorkbookProps provides a function to gets workbook properties.
//
//export GetWorkbookProps
func GetWorkbookProps(idx int) C.struct_GetWorkbookPropsResult {
f, ok := files.Load(idx)
if !ok {
return C.struct_GetWorkbookPropsResult{err: C.CString(errFilePtr)}
}
opts, err := f.(*excelize.File).GetWorkbookProps()
if err != nil {
return C.struct_GetWorkbookPropsResult{err: C.CString(err.Error())}
}
cVal, err := goValueToC(reflect.ValueOf(opts), reflect.ValueOf(&C.struct_WorkbookPropsOptions{}))
if err != nil {
return C.struct_GetWorkbookPropsResult{err: C.CString(err.Error())}
}
return C.struct_GetWorkbookPropsResult{opts: cVal.Elem().Interface().(C.struct_WorkbookPropsOptions), err: C.CString(emptyString)}
}

// InsertCols provides a function to insert new columns before the given column
// name and number of columns.
//
Expand All @@ -1330,6 +1349,21 @@ func InsertCols(idx int, sheet, col *C.char, n int) *C.char {
return C.CString(emptyString)
}

// InsertRows provides a function to insert new rows after the given Excel row
// number starting from 1 and number of rows.
//
//export InsertRows
func InsertRows(idx int, sheet *C.char, row, n int) *C.char {
f, ok := files.Load(idx)
if !ok {
return C.CString(emptyString)
}
if err := f.(*excelize.File).InsertRows(C.GoString(sheet), row, n); err != nil {
return C.CString(err.Error())
}
return C.CString(emptyString)
}

// MergeCell provides a function to merge cells by given range reference and
// sheet name. Merging cells only keeps the upper-left cell value, and
// discards the other values.
Expand Down Expand Up @@ -1800,7 +1834,6 @@ func SaveAs(idx int, name *C.char, opts *C.struct_Options) *C.char {
return C.CString(emptyString)
}


// SearchSheet provides a function to get cell reference by given worksheet name,
// cell value, and regular expression. The function doesn't support searching
// on the calculated result, formatted numbers and conditional lookup
Expand Down
4 changes: 4 additions & 0 deletions test_excelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ def test_style(self):
self.assertIsNone(f.duplicate_row("Sheet1", 20))
self.assertIsNone(f.duplicate_row_to("Sheet1", 20, 20))
self.assertIsNone(f.insert_cols("Sheet1", "C", 2))
self.assertIsNone(f.insert_rows("Sheet1", 20, 2))
self.assertIsNone(f.merge_cell("Sheet1", "A1", "B2"))
self.assertIsNone(f.unmerge_cell("Sheet1", "A1", "B2"))

Expand Down Expand Up @@ -1219,6 +1220,9 @@ def test_workbook_props(self):
date1904=True, filter_privacy=True, code_name="code"
)
self.assertIsNone(f.set_workbook_props(expected))
opts, err = f.get_workbook_props()
self.assertEqual(opts, expected)
self.assertIsNone(err)
self.assertIsNone(f.save_as(os.path.join("test", "TestWorkbookProps.xlsx")))
self.assertIsNone(f.close())

Expand Down
6 changes: 6 additions & 0 deletions types_c.h
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,9 @@ struct GetTablesResult
struct Table *Tables;
char *Err;
};

struct GetWorkbookPropsResult
{
struct WorkbookPropsOptions opts;
char *err;
};
9 changes: 9 additions & 0 deletions types_go.py
Original file line number Diff line number Diff line change
Expand Up @@ -704,13 +704,15 @@ class _BoolErrorResult(Structure):
("err", c_char_p),
]


class _StringArrayErrorResult(Structure):
_fields_ = [
("ArrLen", c_int),
("Arr", POINTER(POINTER(c_char))),
("Err", c_char_p),
]


class _CellNameToCoordinatesResult(Structure):
_fields_ = [
("col", c_int),
Expand Down Expand Up @@ -770,3 +772,10 @@ class _GetTablesResult(Structure):
("Tables", POINTER(_Table)),
("Err", c_char_p),
]


class _GetWorkbookPropsResult(Structure):
_fields_ = [
("opts", _WorkbookPropsOptions),
("err", c_char_p),
]