我从 html [链接] Add basic data to a Google Spreadsheet via a simple HTML Form 中找到了这种存储数据的方法, 但此方法发送的数据不按顺序。我想按顺序发送数据,只发送一些变量 我有这个 html 文件
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<form id="form" method="get" action="https://script.google.com/macros/s/AKfycbzhVAFaP7tFTbSzgeO0y9rUBl6ptVRCbqpS9wi12yFmoboYjw/exec" accept-charset="UTF-8">
Name and age!
<input type=text name=name id=name>
<input type="text" name="direccion" placeholder="Direccion completa">
<input type=number name=age id=age>
<input type="Apellido" name="Apellido" id="Apellido">
<input type=submit onclick=google.script.run.addProduct()>
</form>
</body>
</html>
我的电子表格中有这个 code.gs
function doGet(e){
var vals=[];
vals.push(new Date());
for(var i in e.parameter){
vals.push(e.parameter[i]);
}
SpreadsheetApp.openById("1KYR7SefLy5P-JP3LqdKsRRd_bkwTHFGFQQ0rnD3JfUM").appendRow(vals);
return ContentService.createTextOutput("Información enviada correctamente");
}
有什么方法可以按顺序发送变量?
最佳答案
当通过 form 标记中的操作提交表单时,表单元素将提交到 URL。表单元素是一个对象。一个对象会改变元素的顺序。数组保证数据的顺序,对象不保证对象中数据的顺序。
但是,您可以通过其键名引用对象中的每个元素,在本例中为 name 属性。
function doGet(e){
var vals=[];
vals.push(new Date());
vals.push(e.parameter.name);
vals.push(e.parameter.direccion);
vals.push(e.parameter.age);
vals.push(e.parameter.Apellido);
Logger.log('vals: ' + vals);
SpreadsheetApp.openById("").appendRow(vals);
//return ContentService.createTextOutput("");
};
保证顺序的唯一方法,是在发送数据之前设置顺序,或者在检索数据之后设置顺序。上面的例子设置了数据处理时的顺序。
发送一些输入值的唯一方法是在发送之前处理表单。显然,您可以在服务器端省略一些值。如果您打算将策略与表单标记中的操作一起使用,那么您不需要:
onclick=google.script.run.addProduct()
在提交按钮中。
关于html - 仅将一些变量从 html 表单发送到电子表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34475860/