jas*_*son 5 python google-sheets google-apps-script
在excel中,您可以使用python创建用户定义的函数pyxll
。我一直在使用Google电子表格并使用其Google应用脚本,但是这些库在python中变得更大,更好,我希望有一种方法可以使用Google电子表格中的python构建用户定义的函数。有多种方法可以将python与Google表格进行交互gspread
。有没有一种方法可以在Google App Engine上运行python,然后获取表格来触发该代码?还有什么其他方法可以从Google电子表格中触发python代码?
将 python 代码部署为云函数: https: //cloud.google.com/functions/docs/writing/http。
然后使用 URL Fetch 调用您的函数,如上所示。
您应该在 GAE 中创建一个网络服务,然后可以使用 Google Apps ScriptUrlFetch
类调用它。这就是我通常将第三方应用程序与 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
}
Run Code Online (Sandbox Code Playgroud)
以上代码可以根据一些条件由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
Run Code Online (Sandbox Code Playgroud)
如果您想让电子表格在 GAE 应用程序中运行代码,那么您可以发布电子表格并构建电子表格的 URL (JSON),如下所示:https: //spreadsheets.google.com/feeds/list/(spreadsheetkey) /od6/public/values?alt=json 该地址可以通过应用程序访问,可以读取单元格值,并且可以触发一些代码。
这两种想法的方法是相同的:一些代码监视电子表格,当满足某些条件时,会触发一些其他代码。我不确定当纯粹从 Google 电子表格满足条件时如何运行代码(例如在 GAE 应用程序中)。