简体   繁体   English

使用帖子自定义Google Apps脚本方法,以便将数据追加到工作表

[英]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.

相关问题 如何发送来自 Google Apps 脚本的 POST 数据? - How to send POST data coming from Google Apps Script? 如何使用 Sheet.Best API 从我的 GatsbyJS 网站将数据发布到 Google 表格? - How do I post data from my GatsbyJS website to Google Sheets using Sheet.Best API? 使用 Reactjs 将表单数据存储到谷歌表 - Storing Form Data to google sheet using Reactjs 在 Google Sheets 边栏中使用 React Hooks 时出错(Google Apps Script) - Error using React Hooks In Google Sheets Sidebar (Google Apps Script) 将反应表单数据存储到谷歌表 - Storing react form data to google sheet 如何在 REACT 的 Axios Post 请求中附加表单数据 - How to append form data in an Axios Post request in REACT 使用 Javascript(无 jquery)从 2 个 json 字符串追加数据 - 追加不是函数 - Using Javascript (no jquery) to append data from 2 json strings - append is not a function 从React App上的Google Picker中的选定工作表访问数据 - Access Data from Selected Sheet in Google Picker on React App 下一个js中没有在google sheet中写入数据 - didn't write data in google sheet in next js 用于将数据附加到 Google 表格的反应组件在 PC 上工作但在 iOS 或 Android 上不工作 - React Component for Appending Data to Google Sheet working on PC but not on iOS or Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM