Skip to content

Commit 8a96280

Browse files
committed
Add new function: auto_fit_col_width
- Remove SelectedItems fields in PivotTableOptions to fix build breaks
1 parent 8e43c9e commit 8a96280

File tree

5 files changed

+90
-3
lines changed

5 files changed

+90
-3
lines changed

excelize.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2603,6 +2603,57 @@ def auto_filter(
26032603
if err != "":
26042604
raise RuntimeError(err)
26052605

2606+
def auto_fit_col_width(self, sheet: str, columns: str) -> None:
2607+
"""
2608+
Auto fit columns width according to their text content with font format.
2609+
If the selected range contains hidden columns and those columns have
2610+
content, this function will unhide the hidden columns. Not that this
2611+
function calculates the width of the text approximately based on the
2612+
font format, currently does not support merged cells. the actual width
2613+
may be different when you open the workbook in Office applications. This
2614+
process can be relatively slow on large worksheets, so this should
2615+
normally only be called once per column, at the end of your processing.
2616+
2617+
Args:
2618+
sheet (str): The worksheet name
2619+
columns (str): The columns range
2620+
2621+
Returns:
2622+
None: Return None if no error occurred, otherwise raise a
2623+
RuntimeError with the message.
2624+
2625+
Example:
2626+
Auto fit column width for column `D` on `Sheet1`:
2627+
2628+
```python
2629+
try:
2630+
f.auto_fit_col_width("Sheet1", "D")
2631+
except (RuntimeError, TypeError) as err:
2632+
print(err)
2633+
```
2634+
2635+
Auto fit column width for columns `D` to `F` on `Sheet1`:
2636+
2637+
```python
2638+
try:
2639+
f.auto_fit_col_width("Sheet1", "D:F")
2640+
except (RuntimeError, TypeError) as err:
2641+
print(err)
2642+
```
2643+
"""
2644+
prepare_args(
2645+
[sheet, columns],
2646+
[argsRule("sheet", [str]), argsRule("columns", [str])],
2647+
)
2648+
lib.AutoFitColWidth.restype = c_char_p
2649+
err = lib.AutoFitColWidth(
2650+
self.file_index,
2651+
sheet.encode(ENCODE),
2652+
columns.encode(ENCODE),
2653+
).decode(ENCODE)
2654+
if err != "":
2655+
raise RuntimeError(err)
2656+
26062657
def calc_cell_value(self, sheet: str, cell: str, *opts: Options) -> str:
26072658
"""
26082659
Get calculated cell value. This feature is currently in working

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,28 @@ func AutoFilter(idx int, sheet, rangeRef *C.char, opts *C.struct_AutoFilterOptio
851851
return C.CString(emptyString)
852852
}
853853

854+
// AutoFitColWidth provides a function to auto fit columns width according to
855+
// their text content with font format. If the selected range contains hidden
856+
// columns and those columns have content, this function will unhide the hidden
857+
// columns. Not that this function calculates the width of the text
858+
// approximately based on the font format, currently does not support merged
859+
// cells. the actual width may be different when you open the workbook in Office
860+
// applications. This process can be relatively slow on large worksheets, so
861+
// this should normally only be called once per column, at the end of your
862+
// processing.
863+
//
864+
//export AutoFitColWidth
865+
func AutoFitColWidth(idx int, sheet, columns *C.char) *C.char {
866+
f, ok := files.Load(idx)
867+
if !ok {
868+
return C.CString(errFilePtr)
869+
}
870+
if err := f.(*excelize.File).AutoFitColWidth(C.GoString(sheet), C.GoString(columns)); err != nil {
871+
return C.CString(err.Error())
872+
}
873+
return C.CString(emptyString)
874+
}
875+
854876
// CalcCellValue provides a function to get calculated cell value. This feature
855877
// is currently in working processing. Iterative calculation, implicit
856878
// intersection, explicit intersection, array formula, table formula and some

test_excelize.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,9 @@ def test_none_file_pointer(self):
11801180
"Sheet1", "A1", excelize.IgnoredErrorsType.IgnoredErrorsEvalError
11811181
)
11821182
self.assertEqual(str(context.exception), expected)
1183+
with self.assertRaises(RuntimeError) as context:
1184+
f.auto_fit_col_width("Sheet1", "D")
1185+
self.assertEqual(str(context.exception), expected)
11831186
with self.assertRaises(RuntimeError) as context:
11841187
f.delete_form_control("Sheet1", "A1")
11851188
self.assertEqual(str(context.exception), expected)
@@ -2890,6 +2893,20 @@ def test_set_sheet_col(self):
28902893
str(context.exception),
28912894
"expected type str for argument 'sheet', but got int",
28922895
)
2896+
2897+
self.assertEqual(f.get_col_width("Sheet1", "B"), 9.140625)
2898+
self.assertIsNone(f.auto_fit_col_width("Sheet1", "B"))
2899+
with self.assertRaises(RuntimeError) as context:
2900+
f.auto_fit_col_width("SheetN", "D")
2901+
self.assertEqual(str(context.exception), "sheet SheetN does not exist")
2902+
with self.assertRaises(TypeError) as context:
2903+
f.auto_fit_col_width("Sheet1", 1)
2904+
self.assertEqual(
2905+
str(context.exception),
2906+
"expected type str for argument 'columns', but got int",
2907+
)
2908+
self.assertEqual(f.get_col_width("Sheet1", "B"), 14.61)
2909+
28932910
self.assertIsNone(f.set_sheet_name("Sheet1", "SheetN"))
28942911
with self.assertRaises(RuntimeError) as context:
28952912
f.set_sheet_name(

types_go.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,6 @@ class _PivotTableOptions(Structure):
614614
("FieldPrintTitles", c_bool),
615615
("ItemPrintTitles", c_bool),
616616
("PivotTableStyleName", c_char_p),
617-
("SelectedItemsLen", c_int),
618-
("SelectedItems", POINTER(POINTER(c_char))),
619617
]
620618

621619

types_py.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1042,7 +1042,6 @@ class PivotTableOptions:
10421042
field_print_titles: bool = False
10431043
item_print_titles: bool = False
10441044
pivot_table_style_name: str = ""
1045-
selected_items: Optional[List[str]] = None
10461045

10471046

10481047
@dataclass

0 commit comments

Comments
 (0)