从客户端将表单数据发送到 Google 电子表格
Send form data to Google Spreadsheet from client side
我的纯 JS/HTML/CSS 网站上有一些表格(没有服务器端)。并拥有 Google 个帐户。我的目的是将填写好的表格从 Web 客户端直接发送到 Google 电子表格。可能吗?
是的,这是可能的。我会通过 creating an Apps Script and deploying it as a web app. Here is a sample script: fill in the Id of the spreadsheet 和其中一个 sheet 的名称来完成。
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 请求。)
您选择的 sheet 的单元格 A1 将具有 "hello world"。
我的纯 JS/HTML/CSS 网站上有一些表格(没有服务器端)。并拥有 Google 个帐户。我的目的是将填写好的表格从 Web 客户端直接发送到 Google 电子表格。可能吗?
是的,这是可能的。我会通过 creating an Apps Script and deploying it as a web app. Here is a sample script: fill in the Id of the spreadsheet 和其中一个 sheet 的名称来完成。
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 请求。)
您选择的 sheet 的单元格 A1 将具有 "hello world"。