2017-08-14 36 views
0

我有一個類更新的實時值:xlwings蟒蛇不斷更新返回數據,UDF

class LiveUpdate(): 

    def __init__(self): 

     self.x=0 

     self.t1 = threading.Thread(target=self._refresh_value) 
     self.t1.start() 

    def _refresh_value(self): 

     while True: 
      self.x=self.x+1 


updater=LiveUpdate() 

@xw.func 
def return_live_x(): 
    return updater.x 

當我打電話return_live_x作爲UDF,我怎麼得到它不斷返回1,2,3,4等在Excel中?

+0

很可能你會得到'1000,3000,5000'上return_live_x'的'調用。閱讀[隊列](https://docs.python.org/3/library/queue.html),請考慮本網站上的示例。 – stovfl

+0

我不介意讓1,2,5,7,8等它的更多關於讓excel自動刷新excel中的值,當excel – Dudey007

回答

0

問題:......其Excel是否決定重新計算或鍵按下,以人工手動做更多有關Excel

UDF的獲取UDF自動刷新的值被調用。

不知道是否可以使用xlwings在後臺啓動並運行永遠的Python腳本。

我可以IMAGIN的follwoing,未經測試的代碼

注意:此代碼可能會導致Excel來搪塞。

class LiveUpdate(): 
    def __init__(self, wb): 
     self.wb = wb 
     ... 

    def _refresh_value(self): 
     while True: 
      self.x += 1 
      self.wb.sheets[0].range('A1').value = self.x 
      time.sleep(1) 

@xw.func 
def start_LiveUpdate(): 
    LiveUpdate(xw.Book.caller()) 
    return 'started' 
+0

中使用self.x更新和return_live_x()時,您可以讓Python啓動並如果在vba模塊中使用optimzed_connection = True,則會不斷更新。 – Dudey007

+0

我更感興趣,看看我是否可以得到start_LiveUpdate以返回最新的self.x值 – Dudey007