Skip to content

How to filter data and highlight discontinuous cells? #272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
chanwj opened this issue Feb 21, 2025 · 3 comments
Open

How to filter data and highlight discontinuous cells? #272

chanwj opened this issue Feb 21, 2025 · 3 comments

Comments

@chanwj
Copy link

chanwj commented Feb 21, 2025

Hello, is there any way to achieve those two effect?

  1. Filter data based on conditions(with no dropdown box). For example, get all the cells in the first column which cell content equals to "target string".
  2. Highlight a collection of specifc cells which are not continuous(not like span). For example, after filtering data(just like the above situation), I got two cells(A1 and A6), then I need to change their background into green.

Thanks!

@chanwj
Copy link
Author

chanwj commented Mar 11, 2025

The second situation could be solved by highlighting cells one by one, but how can I solve the first problem? There is a simple example below:

A B
1 B1
2 B2
1 B3

How to get cells that column A is "1" (which result in A1 and A3) quickly? (If doing it through get the whole table and iterate them everytime, it seems too tedious.)

Would you like to give some advice? @ragardner Thanks!

@ragardner
Copy link
Owner

ragardner commented Mar 12, 2025

Hello,

I can't think of any way except iteration in this case, if I had to make this library again today I would probably choose a different data structure rather than a list of lists to save time iterating when there are a lot of empty cells.

If your table doesn't change you could save the cell coordinates in a variable such as a set as shown below so you can access them later:

from tksheet import Sheet
import tkinter as tk
import random

x = [1, 2]


class demo(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.grid_columnconfigure(0, weight=1)
        self.grid_rowconfigure(0, weight=1)
        self.sheet = Sheet(
            self,
            data=[[random.choice(x) for _ in range(2)] for _ in range(10)],
        )
        self.sheet.enable_bindings()
        self.sheet.popup_menu_add_command("Get cells", self.get_cells)
        self.sheet.grid(row=0, column=0, sticky="nswe")

    def get_cells(self):
        # cell coordinates as a set
        self.cells_as_set = {
            (rn, 0) for rn, row in enumerate(self.sheet.data) if row[0] == 1
        }
        print(self.cells_as_set)

        # cell coordinates as a generator
        self.cells_as_generator = (
            (rn, 0) for rn, row in enumerate(self.sheet.data) if row[0] == 1
        )


app = demo()
app.mainloop()

Let me know if this doesn't help your situation

@chanwj
Copy link
Author

chanwj commented Mar 13, 2025

If your table doesn't change you could save the cell coordinates in a variable such as a set as shown below so you can access them later...

@ragardner

Recording cells coordinates is certainly feasible. I will use this method to meet the need currently. Thank you for your solution and I have to say that tksheet has already helped my work a lot.

But I also wonder how dropdown box filter data? Is it do the similar iteration?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants