【问题标题】:Trigger python code from Google spreadsheets?从 Google 电子表格触发 python 代码?
【发布时间】:2014-04-08 02:25:36
【问题描述】:

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

【问题讨论】:

    标签: python google-apps-script google-sheets


    【解决方案1】:

    您应该在 GAE 中创建一个 Web 服务,然后可以使用 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 脚本中的时间驱动触发器触发

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

      • 拯救了我的一天!从字面上看不到5分钟。请注意不要意外:pip install google-cloud-storage。 (在第一步指南中)。这在部署到 gcloud 时出错。
      【解决方案3】:

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

      如果没有 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 应用程序中)。

      【讨论】:

        猜你喜欢
        • 2013-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-23
        • 1970-01-01
        相关资源
        最近更新 更多