Skip to content

Commit

Permalink
7.2.19
Browse files Browse the repository at this point in the history
#### Fixed:
- Error when pasting into empty sheet
- Potential error if using percentage formatter with more than just `int`, `float` and wrong type hint [250](#250)
- Only show the selection box outline while the mouse is dragged if the control key is down
- `index` and `header` `Span` parameters were not working with function `readonly()`

#### Added:
- Shift + arrowkey bindings for expanding/contracting a selection box
- Functions for getting cell properties (options) [249](#249)
- Ability to edit index in treeview mode, if the binding is enabled

#### Improved:
- Very slight performance boost to treeview insert, inserting rows when rows are hidden
  • Loading branch information
ragardner committed Oct 26, 2024
1 parent 4f833a9 commit 8c8cf67
Show file tree
Hide file tree
Showing 10 changed files with 434 additions and 79 deletions.
14 changes: 13 additions & 1 deletion docs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
### Version 7.2.19
####
#### Fixed:
- Error when pasting into empty sheet
- Potential error if using percentage formatter with more than just `int`, `float` and wrong type hint [250](https://github.com/ragardner/tksheet/issues/250)
- Only show the selection box outline while the mouse is dragged if the control key is down
- `index` and `header` `Span` parameters were not working with function `readonly()`

#### Added:
- Shift + arrowkey bindings for expanding/contracting a selection box
- Functions for getting cell properties (options) [249](https://github.com/ragardner/tksheet/issues/249)
- Ability to edit index in treeview mode, if the binding is enabled

#### Improved:
- Very slight performance boost to treeview insert, inserting rows when rows are hidden

### Version 7.2.18
#### Fixed:
Expand Down
127 changes: 123 additions & 4 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
- [Data Formatting](https://github.com/ragardner/tksheet/wiki/Version-7#data-formatting)
- [Readonly Cells](https://github.com/ragardner/tksheet/wiki/Version-7#readonly-cells)
- [Text Font and Alignment](https://github.com/ragardner/tksheet/wiki/Version-7#text-font-and-alignment)
- [Getting Cell Properties](https://github.com/ragardner/tksheet/wiki/Version-7#getting-cell-properties)
---
- [Getting Selected Cells](https://github.com/ragardner/tksheet/wiki/Version-7#getting-selected-cells)
- [Modifying Selected Cells](https://github.com/ragardner/tksheet/wiki/Version-7#modifying-selected-cells)
Expand Down Expand Up @@ -1230,6 +1231,10 @@ up_bindings
right_bindings
down_bindings
left_bindings
shift_up_bindings
shift_right_bindings
shift_down_bindings
shift_left_bindings
prior_bindings
next_bindings
```
Expand Down Expand Up @@ -3942,6 +3947,108 @@ Column text alignments:
get_column_alignments() -> dict
```
---
# **Getting Cell Properties**
The below functions can be used to retrieve cell options/properties such as highlights, format, readonly etc.
#### **Get table cell properties**
Retrieve options for a single cell in the main table. Also retrieves any row/column options impacting that cell.
```Python
props(
row: int,
column: int | str,
key: None
| Literal[
"format",
"highlight",
"dropdown",
"checkbox",
"readonly",
"align",
] = None,
cellops: bool = True,
rowops: bool = True,
columnops: bool = True,
) -> dict
```
Parameters:
- `row` only `int`.
- `column` `int` or `str` e.g. `"A"` is index `0`.
- `key`:
- If left as `None` then all existing properties for that cell will be returned in a `dict`.
- If using a `str` e.g. `"highlight"` it will only look for highlight properties for that cell.
- `cellops` when `True` will look for cell options for the cell.
- `rowops` when `True` will look for row options for the cell.
- `columnops` when `True` will look for column options for the cell.
Example:
```python
# making column B, including header read only
sheet.readonly(sheet["B"].options(header=True))
# checking if row 0, column 1 (B) is readonly:
cell_is_readonly = sheet.props(0, 1, "readonly")
# can also use a string for the column:
cell_is_readonly = sheet.props(0, "b", "readonly")
```
___
#### **Get index cell properties**
Retrieve options for a single cell in the index.
```python
index_props(
row: int,
key: None
| Literal[
"format",
"highlight",
"dropdown",
"checkbox",
"readonly",
"align",
] = None,
) -> dict
```
Parameters:
- `row` only `int`.
- `key`:
- If left as `None` then all existing properties for that cell will be returned in a `dict`.
- If using a `str` e.g. `"highlight"` it will only look for highlight properties for that cell.
___
#### **Get header cell properties**
Retrieve options for a single cell in the header.
```python
header_props(
column: int | str,
key: None
| Literal[
"format",
"highlight",
"dropdown",
"checkbox",
"readonly",
"align",
] = None,
) -> dict
```
Parameters:
- `column` only `int`.
- `key`:
- If left as `None` then all existing properties for that cell will be returned in a `dict`.
- If using a `str` e.g. `"highlight"` it will only look for highlight properties for that cell.
---
# **Getting Selected Cells**
Expand Down Expand Up @@ -5535,6 +5642,10 @@ up_bindings
right_bindings
down_bindings
left_bindings
shift_up_bindings
shift_right_bindings
shift_down_bindings
shift_left_bindings
prior_bindings
next_bindings
```
Expand Down Expand Up @@ -5687,20 +5798,28 @@ The only bindings (using `enable_bindings()`) which **will not** cause issues ar
- `"paste"`
- `"delete"`
- `"edit_cell"`
- `"edit_header"`
- `"edit_index"`
**Note on undo:**
Take care if using `"undo"` because, for example, if a cell edit happens then an `.insert()` occurs then it will potentially make the saved cell coordinates in the undo stack relate to the wrong cells. This is because the treeview functions do not add to the undo stack.
### **Treeview function limitations**
Many functions designed for normal tksheet usage will cause issues with its treeview mode. The following relates to non-treeview mode functions.
Many functions designed for normal tksheet usage will cause issues when used with treeview mode.
**The following relates to non-treeview mode functions:**
**Okay:**
- Modifying headers or columns.
- Modifying table or header cell contents.
- Modifying any highlights, table data formatting, table/header dropdown boxes/checkboxes, etc.
**Not okay:**
- Modifying the row index.
- Modifying rows, e.g. moving or deleting rows.
- Using undo.
- Modifying the row index, unless it's through user input.
- Modifying rows, e.g. inserting, moving or deleting rows using non-treeview functions.
- Enabling and using `undo`, probably.
### **Treeview other limitations**
Expand Down
5 changes: 2 additions & 3 deletions tksheet/column_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,8 +290,7 @@ def ctrl_shift_b1_press(self, event: object) -> None:

def shift_b1_press(self, event: object) -> None:
self.mouseclick_outside_editor_or_dropdown_all_canvases(inside=True)
x = event.x
c = self.MT.identify_col(x=x)
c = self.MT.identify_col(x=event.x)
if (self.drag_and_drop_enabled or self.col_selection_enabled) and self.rsz_h is None and self.rsz_w is None:
if c < len(self.MT.col_positions) - 1:
c_selected = self.MT.col_selected(c)
Expand All @@ -316,7 +315,7 @@ def shift_b1_press(self, event: object) -> None:
)

def get_shift_select_box(self, c: int, min_c: int) -> tuple[int, int, int, int, str]:
if c > min_c:
if c >= min_c:
return 0, min_c, len(self.MT.row_positions) - 1, c + 1
elif c < min_c:
return 0, c, len(self.MT.row_positions) - 1, min_c + 1
Expand Down
7 changes: 4 additions & 3 deletions tksheet/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def to_str(o: object, **kwargs: dict) -> str:
return f"{o}"


def float_to_str(v: int | float, **kwargs: dict) -> str:
def float_to_str(v: object, **kwargs: dict) -> str:
if isinstance(v, float):
if v.is_integer():
return f"{int(v)}"
Expand All @@ -77,7 +77,7 @@ def float_to_str(v: int | float, **kwargs: dict) -> str:
return f"{v}"


def percentage_to_str(v: int | float, **kwargs: dict) -> str:
def percentage_to_str(v: object, **kwargs: dict) -> str:
if isinstance(v, (int, float)):
x = v * 100
if isinstance(x, float):
Expand All @@ -87,7 +87,8 @@ def percentage_to_str(v: int | float, **kwargs: dict) -> str:
if kwargs["decimals"]:
return f"{round(x, kwargs['decimals'])}%"
return f"{int(round(x, kwargs['decimals']))}%"
return f"{x}%"
return f"{x}%"
return f"{v}%"


def bool_to_str(v: object, **kwargs: dict) -> str:
Expand Down
19 changes: 8 additions & 11 deletions tksheet/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,16 +325,11 @@ def force_bool(o: object) -> bool:
return False


def str_to_coords(s: str) -> None | tuple[int]:
s = s.split(":")


def alpha2idx(a: str) -> int | None:
try:
a = a.upper()
n = 0
orda = ord("A")
for c in a:
for c in a.upper():
n = n * 26 + ord(c) - orda + 1
return n - 1
except Exception:
Expand Down Expand Up @@ -522,9 +517,7 @@ def add_to_displayed(displayed: list[int], to_add: Iterator[int]) -> list[int]:
# assumes to_add is sorted in reverse
for i in reversed(to_add):
ins = bisect_left(displayed, i)
displayed[ins:] = [e + 1 for e in islice(displayed, ins, None)]
for i in reversed(to_add):
displayed.insert(bisect_left(displayed, i), i)
displayed[ins:] = [i] + [e + 1 for e in islice(displayed, ins, None)]
return displayed


Expand Down Expand Up @@ -595,7 +588,11 @@ def get_new_indexes(
return new_idxs


def insert_items(seq: list | tuple, to_insert: dict, seq_len_func: Callable | None = None) -> list:
def insert_items(
seq: list[object] | tuple[object],
to_insert: dict[int, object],
seq_len_func: Callable | None = None,
) -> list:
# inserts many items into a list using a dict of reverse sorted order of
# {index: value, index: value, ...}
res = []
Expand Down Expand Up @@ -1425,7 +1422,7 @@ def mod_event_val(


def pop_positions(
itr: Iterator[int],
itr: Callable,
to_pop: dict[int, int], # displayed index: data index
save_to: dict[int, int],
) -> Iterator[int]:
Expand Down
Loading

0 comments on commit 8c8cf67

Please sign in to comment.