【问题标题】:Reading Google Sheets in distributed Python application在分布式 Python 应用程序中阅读 Google 表格
【发布时间】:2020-11-19 17:01:31
【问题描述】:

我正在开发一个将分发给多人的应用程序。此应用程序需要做的一件事是读取某些 Google 电子表格,但是,我已阅读到要能够通过 API 读取 google 电子表格,您必须使用您的 Google 帐户进行身份验证。

此应用程序的目标用户可能没有 Google 帐户,或者可能不愿意在程序中输入他们的 Google 电子邮件和密码。

有没有一种方法可以在不输入您的私人 Google 凭据的情况下读取 Google 表格?也许某种只用于应用程序的公共帐户?或者在某些情况下您不需要任何帐户?

提前谢谢你!

附:我打算使用gspread 库,但如果我有更好的选择,请告诉我。

【问题讨论】:

    标签: python google-sheets google-api google-oauth


    【解决方案1】:

    您可以使用网络应用程序

    这不是向您开放的唯一选项,但它可能是最简单的入门选项。您可以有效地托管似乎可以向其发出 GET 和 POST 请求的端点。如果您将其设置为,则底层脚本会像您在运行它一样运行。当您将其部署为 Web 应用程序时,您只需将“谁有权访问该应用程序”设置为“任何人,甚至匿名”即可。

    Apps Script Web 应用的一些示例代码。 GET 返回简单的 HTML,如果请求的结构正确,POST 将使用信息更新电子表格并向您帐户提供的地址发送电子邮件:

    function doGet() {
        textOutput = ContentService.createTextOutput("Hello World! Welcome to the web app.")
        return textOutput
    }
    
    function doPost (e) {
      var doc = SpreadsheetApp.getActiveSpreadsheet() // works if container bound
      var sheet = doc.getSheetByName("Sheet1")
      var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0]
      var nextRow = sheet.getLastRow() + 1
      
      var newRow = headers.map(function(header) {
        return header === 'timestamp' ? new Date() : e.parameter[header]
      })
      
      var newRange = sheet.getRange(nextRow, 1, 1, newRow.length)
      newRange.setValues([newRow])
      
      MailApp.sendEmail(e.parameter["email"], "Hello", "Welcome");
    }
    

    参考文献

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-26
      • 2020-06-21
      • 1970-01-01
      • 2023-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多