2014-01-25 90 views
1

我正在嘗試創建一個交互式工作表,可以根據單元格值加載一些數據。例如,如果單元格「A1」更改爲「estructura」,則範圍(C2:E4)將加載data3列表。下面的代碼是一個好方法還是有更好的方法?使用Datanitro的交互式工作表

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 

回答

1

該代碼有效。 (while循環沒有正確縮進,但我認爲這是一個錯字。)

這裏的循環更高效的版本:

的主要區別是,你在A1的值讀數一次每個循環 - 這比每次比較讀取都要快。

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