Search code examples
pythonexceldatanitro

Interactive sheet using Datanitro


I'm trying to create an interactive sheet that can load some data according to a cell value. For example, if the cell "A1" changes to "estructura" the range(C2:E4) will load data3 list. Is the code below a good approach or there is a better way?

data1 = [[1, 2, 3], [4, 5, None], [6, 7, 8]]
data2 = [[10, 20, 30], [40, 50, 60], [70, 80, 90]]
data3 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

static = Cell("A1").value

while True:
    if static != Cell("A1").value:
        if Cell("A1").value == "mamposteria":
            CellRange("C2:E4").clear()
            Cell("C2").table = data1
        elif Cell("A1").value == "mortero":
           CellRange("C2:E4").clear()
           Cell("C2").table = data2
        elif Cell("A1").value == "estructura":
           CellRange("C2:E4").clear()
           Cell("C2").table = data3
        static = Cell("A1").value

Solution

  • That code works. (The while loop isn't indented properly, but I assume that's a typo.)

    Here's a more efficient version of the loop:

    The main difference is that you're reading in the value of A1 once per loop - this'll be faster than reading it for each comparison.

    while True:
        new = Cell("A1").value
        if static != new:
            CellRange("C2:E4").clear()
            Cell("C2").table = {"mamposteria": data1,
                                "moretero": data2,
                                "estructura": data3}[new]
            static = new