簡體   English   中英

從 Google 電子表格觸發 python 代碼?

[英]Trigger python code from Google spreadsheets?

在 excel 中,您可以使用pyxll使用 python 創建用戶定義的函數。 我一直在轉向 Google 電子表格並使用他們的 Google 應用程序腳本,但是 Python 中的庫更大更好,我希望有一種方法可以使用 Google 電子表格中的 Python 構建用戶定義的函數。 有很多方法可以將 python 與 Google 表格進行交互,例如gspread 有沒有辦法在 Google 應用引擎上運行 python 然后獲取工作表來觸發該代碼? 還有什么其他方法可以從 Google 電子表格中觸發 python 代碼?

您應該在 GAE 中創建一個網絡服務,然后可以使用 Google Apps Script UrlFetch類調用它。 這就是我通常將第三方應用程序與 Apps Script App 集成的方式。

在電子表格容器腳本中,您可以創建類似的代碼

function myFunction(){
  //your code
  //Call the webservice
 var response = UrlFetchApp.fetch('my_webservice_url', {payload:'...', method:'POST'});
 Logger.log(response.getContentText());
 // your code based on response
}

以上代碼可以根據一些條件由Apps Script中的時間驅動觸發器觸發

一種方法是讓一些代碼始終讀取電子表格,然后在滿足條件時運行一些其他代碼。

如果沒有 GAE,您可以使用以下代碼:

#http://code.google.com/p/gdata-python-client/downloads/list
import gdata.spreadsheet.service as s

spreadsheet_key = 'spreadsheetkey'# https://docs.google.com/spreadsheet/ccc?key=<spreadsheet key>&usp=sharing#gid=0

worksheet_key = 'od6' #first tab
gd_client = s.SpreadsheetsService(spreadsheet_key, worksheet_key)
gd_client.email = 'user@gmail.com'
gd_client.password = 'password'
gd_client.ProgrammaticLogin()
list_feed = gd_client.GetListFeed(spreadsheet_key, worksheet_key)
for entry in list_feed.entry:
    #read cell values and then do something if the condition is met

如果您想讓電子表格在 GAE 應用程序中運行代碼,那么您可以發布電子表格並構建電子表格的 URL (JSON),如下所示: https : //spreadsheets.google.com/feeds/list/ (spreadsheetkey ) /od6/public/values?alt=json這個地址可以通過app訪問,可以讀取單元格值,可以觸發一些代碼。

這兩種想法的方法相同:一些代碼監視電子表格,當滿足某些條件時,觸發其他一些代碼。 當完全從 Google 電子表格滿足條件時,我不確定您如何運行代碼(例如在 GAE 應用程序中)。

將您的 Python 代碼部署為雲函數: https : //cloud.google.com/functions/docs/writing/http

然后使用 URL Fetch 調用您的函數,如上所示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM