1
我在纯JS/HTML/CSS网站上有一些表单(没有服务器端)。并拥有Google帐户。我的目的是从Web客户端直接发送填充表单到Google Spreadsheet。可能吗?从客户端发送表格数据到Google Spreadsheet
我在纯JS/HTML/CSS网站上有一些表单(没有服务器端)。并拥有Google帐户。我的目的是从Web客户端直接发送填充表单到Google Spreadsheet。可能吗?从客户端发送表格数据到Google Spreadsheet
是的,这是可能的。我会这样做creating an Apps Script和deploying 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");
}
}
将脚本发布为Web应用程序,允许访问每个人(除非您想处理认证)。这会给你一个URL,如https://script.google.com/macros/s/..../exec
在客户端,你发送POST请求到该URL。脚本希望数据在参数“数据”中。
var url = 'https://script.google.com/macros/s/..../exec';
$.post(url, {data: 'hello world'});
(我使用jQuery这里只是为了有一个较短的例子,你可以发送普通的JavaScript POST请求。)
细胞您选择纸张的A1将有“世界你好” 。
这是我需要的。谢谢! – Erik
我刚刚得到CORS问题。当我向外域发送POST请求时,浏览器会限制这一点。如何解决这个问题? – Erik