[英]customizing google apps script methods using post for append data to sheet
currently i trying to send/append data to spreadsheet using google apps script, but if i click the submit
button i want to the page not redirect as https://www.selly.id/ if you try to send data from it form , it only change the UI and the data is sended to sheet, so do i must change it using JS , or other methods 目前,我正在尝试使用Google Apps脚本将数据发送/追加到电子表格,但是如果我单击“ submit
按钮,则我希望页面不以https://www.selly.id/的身份重定向,如果您尝试从表单发送数据,它仅更改UI并将数据发送到工作表,所以我必须使用JS或其他方法进行更改
here is my current website https://selly-midtrans.herokuapp.com/ , but it now working correctly yet 这是我当前的网站https://selly-midtrans.herokuapp.com/ ,但现在可以正常使用了
function doPost(e){
var vals=[];
vals.push(new Date());
for(var i in e.parameter){
vals.push(e.parameter[i]);
}
SpreadsheetApp.openById("1gMjG4MWTLzoPncVkJ_nGimYdiXULzCvYt5nvqCf4Z4o").appendRow(vals);
// return ContentService.createTextOutput("added");
}
function doGet(e){
var vals=[];
vals.push(new Date());
for(var i in e.parameter){
vals.push(e.parameter[i]);
}
SpreadsheetApp.openById("1gMjG4MWTLzoPncVkJ_nGimYdiXULzCvYt5nvqCf4Z4o").appendRow(vals);
return ContentService.createTextOutput("added");
}
here's my form 这是我的表格
<form id="form" method="post" action="https://script.google.com/a/41studio.com/macros/s/AKfycbwlP_LSQSRHI0wIoJtfwFXiAJttawlYV5dN1PFpAOMtdZ2D-Iz7/exec" accept-charset="UTF-8">
<input type="text" name="newletter_name" placeholder="Name"/>
<input type="text" name="newletter_email" placeholder="Email"/>
<input type="submit" value="Subscribe"/>
</form>
I'm Kind Of Confused 我有点困惑
FYI : im develop it using react JS 仅供参考:我使用react JS开发它
Instead of making a POST request from your form, you can use Javascript to do it for you and prevent the page from reloading. 您可以使用Javascript代替您的表单发出POST请求,并阻止页面重新加载。
Since you are using react the trigger is onSubmit={this.handleSubmit}
由于您使用的是onSubmit={this.handleSubmit}
触发器是onSubmit={this.handleSubmit}
Then create the method to handle the submit, something like below. 然后创建处理提交的方法,如下所示。 YO YO
handleSubmit = (e) => {
e.preventDefault(); //this prevents the page from reloading
const opts = {
method: 'POST',
body: ... //fix this to whatever parameters you are passing
};
fetch(URL, opts)
.then(response => console.log(response))
.catch(error => console.log(error));
}
The documentation for using the fetch API is here 使用fetch API的文档在这里
You can use a package like axios to handle the fetch request much more easily. 您可以使用axios之类的包来更轻松地处理提取请求。
I made a blog post about using the doPost(e)
in Google Apps Script however, it doesn't use ReactJS so it might not help. 我写了一篇有关在Google Apps脚本中使用doPost(e)
的博客文章 ,但是它不使用ReactJS,因此可能无济于事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.