从 Google 电子表格触发 python 代码?

问题描述 投票:0回答:5

在 Excel 中,您可以使用 Python 创建用户定义的函数

pyxll
。我一直在转向 Google 电子表格并使用他们的 Google 应用程序脚本,但是 Python 中的库更大、更好,我希望有一种方法可以使用 Google 电子表格中的 Python 构建用户定义的函数。有多种方法可以将 python 与 Google 表格进行交互,例如
gspread
。有没有办法在 Google 应用程序引擎上运行 python 然后获取工作表来触发该代码?还有哪些其他方法可以从 Google 电子表格触发 python 代码?

python google-apps-script google-sheets
5个回答
9
投票

您应该在 GAE 中创建一个 Web 服务,然后可以使用 Google Apps Script

UrlFetch
类调用该服务。 这就是我通常将第三方应用程序与 Apps 脚本应用程序集成的方式。

在电子表格容器脚本中,您可以创建如下代码

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脚本中的时间驱动触发器触发


7
投票

将 Python 代码部署为云函数: https://cloud.google.com/functions/docs/writing/http

然后使用 URL Fetch 调用您的函数,如上所示。


2
投票

一种方法是使用一些代码始终读取电子表格,然后在满足条件时运行其他代码。

如果没有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 = '[email protected]'
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/(电子表格键)/od6/public/values?alt=json 可以通过应用程序访问该地址,可以读取单元格值,并且可以触发一些代码。

这两种想法的方法是相同的:一些代码监视电子表格,当满足某些条件时,会触发一些其他代码。我不确定当纯粹从 Google 电子表格满足条件时,如何运行代码(例如在 GAE 应用程序中)。


0
投票

您可以使用

APP SCRYPT
将文件保存在 Google 云端硬盘文件夹中 使用 Google Apps 脚本将文件上传到我的 Google 云端硬盘

并使用 python 来观看计算机中的文件夹,并使用 google Drive 桌面与此文件夹同步 使用 Google Drive Desktop 同步计算机中的驱动器文件夹 和 使用Watch Python来观察这个文件夹


© www.soinside.com 2019 - 2024. All rights reserved.