【问题标题】:Send form data to Google Spreadsheet from client side从客户端将表单数据发送到 Google 电子表格
【发布时间】:2016-05-31 14:50:14
【问题描述】:

我的纯 JS/HTML/CSS 网站上有一些表单(没有服务器端)。并拥有谷歌账号。我的目的是将填写好的表格从网络客户端直接发送到 Google 电子表格。有可能吗?

【问题讨论】:

    标签: html forms google-sheets google-spreadsheet-api


    【解决方案1】:

    是的,这是可能的。我会通过creating an Apps Scriptdeploying it as a web app 来做。这是一个示例脚本:填写Id of the spreadsheet 和其中一张表的名称。

    function doPost(event) {
      var params = event.parameter;
      var sheet = SpreadsheetApp.openById('..Id..').getSheetByName('..Name..');
      if (params.data !== undefined) { 
        sheet.getRange(1, 1).setValue(params.data);
        return ContentService.createTextOutput("Success");
      }
      else {
        return ContentService.createTextOutput("Oops");
      }  
    }
    

    将脚本发布为网络应用程序,允许所有人访问(除非您要处理身份验证)。这将为您提供一个 URL,例如 https://script.google.com/macros/s/..../exec

    在客户端,您向该 URL 发送 POST 请求。该脚本期望数据位于参数“data”中。

    var url = 'https://script.google.com/macros/s/..../exec';
    $.post(url, {data: 'hello world'});
    

    (我在这里使用 jQuery 只是为了有一个更简短的示例;您可以使用纯 JavaScript 发送 POST 请求。)

    您选择的工作表的单元格 A1 将显示“hello world”。

    【讨论】:

    • 这是我需要的。谢谢!
    • 我刚刚遇到了 CORS 问题。当我向外部域发送 POST 请求时,浏览器会限制这一点。如何解决这个问题?
    猜你喜欢
    • 2014-02-01
    • 2016-01-25
    • 1970-01-01
    • 1970-01-01
    • 2011-10-05
    • 2015-11-21
    • 1970-01-01
    • 1970-01-01
    • 2019-12-14
    相关资源
    最近更新 更多