多次填写相同表单的不同数据并自动提交到谷歌表

时间:2017-11-10 17:37:05

标签: javascript ajax google-sheets

我有一些数据需要转移到数组,并将其发送到谷歌表 数据如下所示 - | PO:2005 | 12121211212121,|数量:45 | BIN:110 | 易趣| 11/6 / 2017- | PO:2165 | 333333333,|数量:54 | BIN:20 | 谷歌| 11/6 / 2017- |

首先我将用户JS转移到数组,然后将数组中的所有数据放到表单中,然后单击提交表单。

这样的数组

(6)[“PO:2005”,“12121211212121”,“数量:45”,“BIN:110”,“eBay”,“11/6/2017”] index.html的:62 (6)[“PO:2165”,“333333333”,“数量:54”,“BIN:20”,“google”,“11/6/2017”]

表单应该多次提交,但在Google表格中我只获得了第一个表格的数据

这是我的主要HTML

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//  
$(document).ready(function() {
    //
    $('#googleSheetInsert').bootstrapValidator({
        //submitButtons: '#postForm',
        // To use feedback icons, ensure that you use Bootstrap v3.1.0 or later
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },  
    })
    .on('success.form.bv', function(e) {
        // Prevent form submission
        e.preventDefault();

        // Get the form instance
        var $form = $(e.target);

        // Get the BootstrapValidator instance
        var bv = $form.data('bootstrapValidator');

        // Use Ajax to submit form data
        var url = ' ';
        var redirectUrl = 'index.html';
        // show the loading 
        $('#postForm').prepend($('<span></span>').addClass('glyphicon glyphicon-refresh glyphicon-refresh-animate'));
        var jqxhr = $.post(url, $form.serialize(), function(data) {
            console.log("Success! Data: " + data.statusText);
           // $(location).attr('href',redirectUrl); relocation
        })
            .fail(function(data) {
                console.warn("Error! Data: " + data.statusText);
                // HACK - check if browser is Safari - and redirect even if fail b/c we know the form submits.
                if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
                    //alert("Browser is Safari -- we get an error, but the form still submits -- continue.");
                    $(location).attr('href',redirectUrl);                
                }
            });
    });
});
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <link href="http://cdn.jsdelivr.net/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet"/>
    <link rel="stylesheet" href="http://cdn.jsdelivr.net/fontawesome/4.1.0/css/font-awesome.min.css" />
    
    <link rel="stylesheet" href="http://cdn.jsdelivr.net/jquery.bootstrapvalidator/0.5.0/css/bootstrapValidator.min.css"/>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery/1.11.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    
    <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.bootstrapvalidator/0.5.0/js/bootstrapValidator.min.js"></script>
    <style type="text/css">
      body {
        margin: 10px;
        white-space: nowrap;
      }

      h1 {
        font-size: 15px;
      }

      #container {
        align-items: center;
        display: flex;
        justify-content: space-between;
      }
    </style>
    <!--
      - JavaScript and HTML must be in separate files: see our Content Security
      - Policy documentation[1] for details and explanation.
      -
      - [1]: https://developer.chrome.com/extensions/contentSecurityPolicy
    -->
<script>
        
        function submitDataToGoogleSheet(){
            var dataFromInput = document.getElementById("SOS_POData").value;
            // Split by -| and put to array
            var filter0 = dataFromInput.split("-|");
            // Remove Empty Element
            var unitArray =[]; 
            for(var i=0;i<filter0.length;i++){
                if(filter0[i]!==""){
                    unitArray.push(filter0[i].split("|")); 
                }
            }
            // insert data to google sheet
            
            for(j=0;j<unitArray.length;j++){
            doSubmite(unitArray,j);
            }
         
        }
        
        function doSubmite(unitArray,j){
            console.log(unitArray[j]);
            document.getElementById('PO_Number').value = ((unitArray[j][0]).substring(4)).replace(/(^\s*)|(\s*$)/g,"");
            document.getElementById('Part_Number').value = (unitArray[j][1]).replace(/(^\s*)|(\s*$)/g,"");
            document.getElementById('Qty').value = ((unitArray[j][2]).substring(5)).replace(/(^\s*)|(\s*$)/g,"");
            document.getElementById('BIN').value = (unitArray[j][3]).substring(5);
            document.getElementById('Receiver_Name').value = (unitArray[j][4]).replace(/(^\s*)|(\s*$)/g,"");
            document.getElementById('Receiver_Data').value = (unitArray[j][5]).replace(/(^\s*)|(\s*$)/g,"");
            document.getElementById('postForm').click();
            
            
        }
         
          
    
        
        
    </script>
  </head>

  <body>
    <h1>Please copy all information from laeb in there.</h1>
    <div id="container">
      
      <form id="dataFromSOS" action="#" method="post">
          <input type="text" name="SOS_POData" id="SOS_POData" required="" placeholder="Please copy all label information in there">
          <input type="button" name="submit_form" value="Submite" onclick="submitDataToGoogleSheet()"> 
      </form >
    </div>
    
    <div>
        <form id="googleSheetInsert">
            <label>PO</label>
            <input id='PO_Number' name='PO_Number' type='text'>  
            <label>PartNumber</label>
            <input id='Part_Number' name='Part_Number' type='text'>
            <label>Qty</label>
            <input id='Qty' name='Qty' type='text'>
            <label>BIN</label>
            <input id='BIN' name='BIN' type='text'>
            <label>Receiver_Name</label>
            <input id='Receiver_Name' name='Receiver_Name' type='text'>
            <label>Receiver_Data</label>
            <input id='Receiver_Data' name='Receiver_Data' type='text'>
            <input type="submit"   name="submit" id="postForm"  />
        </form>
        
        
    </div>
    
    <script src="js/sendDataToGoogleSheedAjax.js"></script>
     
  </body>
</html>

1 个答案:

答案 0 :(得分:0)

您是否设置了Google表格来处理同时提交的表单?

您可以按照以下示例:
https://medium.com/@dmccoy/how-to-submit-an-html-form-to-google-sheets-without-google-forms-b833952cc175

就你的多次提交而言,它们似乎正在尽我所能地工作(没有要提交的表格)。如果我单步执行代码,我可以看到正在使用值更新的表单,所有内容似乎都已解析好了。让我相信问题出在接收方的原因是什么。

为了让它适合我,我做了以下事情:

  1. 在Google表格列中输入您的姓名并与<input name="">匹配。
  2. 在您的Google工作表中,转到工具 - &gt;脚本编辑器并从此处插入脚本:https://gist.github.com/mhawksey/1276293
  3. 在脚本编辑器中运行 - >运行功能 - >设置
  4. 在脚本编辑器中执行发布 - &gt;部署为Web应用程序...
    • 设置安全级别并启用服务(最有可能以'我'身份执行并访问'任何人,甚至匿名)
    • 请务必记下网址,因为我们将发布到
    • 它应该看起来像https://script.google.com/macros/s/<GIBERISH>/exec
  5. 更新你如何调用ajax POST函数(我无法让bootstrap验证器工作。实际上它只导致发送一个POST,所以我只是删除了它,一切都按预期运行。)
  6. 代码:

    $('#googleSheetInsert').on('submit', function(e) {
          // Prevent form submission
          e.preventDefault();
    
          // Get the form instance
          var $form = $(e.target);
    
          // Use Ajax to submit form data
          var url = 'https://script.google.com/macros/s/<GIBERISH>/exec';
          var redirectUrl = 'index.html';
          // show the loading 
          $('#postForm').prepend($('<span></span>').addClass('glyphicon glyphicon-refresh glyphicon-refresh-animate'));
          var jqxhr = $.post(url, $form.serialize(), function(data) {
              console.log("Success! Data: " + data.statusText);
            // $(location).attr('href',redirectUrl); relocation
          })
              .fail(function(data) {
                  console.warn("Error! Data: " + data.statusText);
                  // HACK - check if browser is Safari - and redirect even if fail b/c we know the form submits.
                  if (navigator.userAgent.search("Safari") >= 0 && navigator.userAgent.search("Chrome") < 0) {
                      //alert("Browser is Safari -- we get an error, but the form still submits -- continue.");
                      $(location).attr('href',redirectUrl);                
                  }
              });
      });
    

    图片显示工作代码:

    Submitting your test data a couple times
    Form updated