简体   繁体   English

如何使用javascript从html表单中的excel上记录数据?

[英]how to record data on excel from a form in html using javascript?

I don't have access to our servers so I need to find a different way to make this work. 我无权访问我们的服务器,因此我需要找到其他方法来完成这项工作。 Javascript appears to be the only way since I have not yet learned jQuery yet. 自从我还没有学习jQuery以来,Javascript似乎是唯一的方法。

I slapped together this code below, but for the life of me I can't figure out what I'm missing. 我在下面拍了这段代码,但是对于我一生来说,我无法弄清我所缺少的。 It fires up fine and no errors pop up, but when I look up the file its supposed to be storing in, there is nothing there. 它可以正常启动并且不会弹出错误,但是当我查找应该存储在其中的文件时,那里什么也没有。 Any insight anyone may have would be greatly appreciated. 任何人可能拥有的任何见解将不胜感激。

Please ignore the useless code to my issue, just wanted to show the whole idea behind it. 请忽略对我的问题无用的代码,只想显示其背后的整个想法。 I know its really rough around the edges... okay, its pretty messed up, but its what I am working with at the moment. 我知道它的边缘非常粗糙...好吧,它看起来很混乱,但是我现在正在使用它。 Promise it will get better with time as I learn more updated codes. 保证随着时间的推移,随着我了解更多更新的代码,它将变得更好。

<!DOCTYPE html>
<html>
<head>
<style>
  #right {
    text-align:right;
  }
</style>

<script type="text/javascript">

function write_to_excel()   {

  str="";

  var mytable = document.getElementsByTagName("table")[0];
  var row_Count = mytable.rows.length;
  var col_Count = mytable.getElementsByTagName("tr")[0].getElementsByTagName("td").length;    

  var ExcelApp = new ActiveXObject("Excel.Application");
  var excel_file = excel.Workbooks.Open("pathwaytoexcel.xlsx");
  var ExcelSheet = excel.Worksheets("Sheet1");
  ExcelSheet.Application.Visible = false;

  for(var i=0; i < row_count ; i++) {   
    for(var j=0; j < col_Count; j++) {           
      str = mytable.getElementsByTagName("tr")[i].getElementsByTagName("td")[j].innerHTML;
      ExcelSheet.ActiveSheet.Cells(i+1,j+1).Value = str;
    }
  }
}
</script>
</head>
<body>
<form action="">

<h2>Call tracking</h2>
<table border="0" cellpadding="5">

<tr>
    <td><div id="right">Positive Response?</div></td>
    <td><input type="checkbox" id="Yes1" >Yes 
        <input type="checkbox" id="No1"  >No</td>
</tr>                           
<tr>                            
    <td><div id="right">Negative Response?</div></td>
    <td><input type="checkbox" id="Yes2" >Yes
        <input type="checkbox" id="No2"  >No</td>
</tr>                           
<tr>                            
    <td><div id="right">Paid with Representative</div></td>
    <td><input type="checkbox" id="Yes3" >Yes
        <input type="checkbox" id="No3"  >No</td>
</tr>                           
<tr>                            
    <td><div id="right">Previously made Payment<br /> <i>(Add ID number if yes)</i></div></td>
    <td><input type="checkbox" id="Yes4" onclick="promptForID()" >Yes
                <input type="checkbox" id="No4"  >No</td>
    <td><input type="text" id="text4" value="Enter ID" style="display:none"></td>
</tr>                           
<tr>                            
    <td><div id="right">Would like to wait to make Payment?</div></td>
    <td><input type="checkbox" id="Yes5" >Yes
        <input type="checkbox" id="No5"  >No</td>
</tr>                           
<tr>                            
    <td><div id="right">Can't Pay No card or check</div></td>
    <td><input type="checkbox" id="Yes6" >Yes
        <input type="checkbox" id="NA6"  >N/A</td>
</tr>                           
<tr>                            
    <td><div id="right">Requesting Cancellation?</div></td>
    <td><input type="checkbox" id="Yes7" onclick="promptForComment()">Yes
        <input type="checkbox" id="No7"  >No</td>
    <td><input type="text" id="text7" value="Enter comments" style="display:none"></td>
</tr>
<tr>                            
    <td><div id="right">Requested a callback?</div></td>
    <td><input type="checkbox" id="Yes8" onclick="promptForTel()">Yes 
        <input type="checkbox" id="No8"  >No </td>
    <td><input type="text" id="text8" value="Contact Number" style="display:none"></td>
</tr>                           

</div>  
</table>

<input type="button" value="Restart" onclick="reloadPage()">
<input type="submit" value="Export to EXCEL" onclick="write_to_excel();"/>

<script>
function promptForID () {
  var checkedYes = document.getElementById("Yes4").checked;
  if (checkedYes == true) {
    document.getElementById("text4").style.display = "block";
  } else {                              
    document.getElementById("text4").style.display = "none";
  }
}
</script>

<script>
function promptForComment () {
  var checkedYes = document.getElementById("Yes7").checked;

  if (checkedYes == true) {
    document.getElementById("text7").style.display = "block";
  } else {
    document.getElementById("text7").style.display = "none";
  }
}
</script>

<script>
function promptForTel() {
    var checkedYes = document.getElementById("Yes8").checked;

if(checkedYes == true){
    document.getElementById("text8").style.display = "block";
}
else    {
    document.getElementById("text8").style.display = "none";
}

}
</script>

<script> 
  function reloadPage () {
    location.reload();
  }
</script>
</body>
</html>

Sorry, 抱歉,
It isn't possible with just HTML / JS / JQuery, etc. 仅HTML / JS / JQuery等是不可能的。
These languages are on the client side, so if you wanted to read / write to a file, you would need a serverside language ex: PHP. 这些语言在客户端,因此,如果您想读取/写入文件,则需要服务器端语言,例如:PHP。 There is a thing called NodeJS, but it is really just JavaScript ported as a server-side language. 有一个叫做NodeJS的东西,但实际上它只是将JavaScript移植为服务器端语言。

TL;DR TL; DR

Nope, PHP / another server-side language is the only way 不,PHP /另一种服务器端语言是唯一的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 JavaScript 将数据从 HTML 表单发送到 Google 电子表格? - How to send data from HTML form to Google Spreadsheet using JavaScript? 如何使用javascript从html表单读取数组数据 - how to read array data from html form using javascript 如何从javascript中的html表单获取数据 - how to get data from html form in javascript 如何使用HTML / JavaScript表单在Excel文件中进行搜索 - How to search inside excel file using HTML/JavaScript form How to store data from a form written in HTML and JavaScript in a session or cookie using PHP to preload whenever the form is accessed - How to store data from a form written in HTML and JavaScript in a session or cookie using PHP to preload whenever the form is accessed 如何仅使用JavaScript将表单数据从一个HTML页面传递到另一HTML页面? - How do I pass form data from one HTML page to another HTML page using ONLY javascript? 使用JavaScript将HTML表格中的数据保存到excel - Save Data From Html Table to excel using JavaScript 单击表单的“提交”按钮,如何将数据从HTML保存到Excel? - How to save data from HTML to excel, on click on “submit” button of form? 如何使用纯/本地JavaScript和html将文本框中的输入数据保存到excel? - How to save input data from text box to excel using pure/native JavaScript and html? 如何使用 javascript 将数据从一个 excel 单元拉入 html 页面? - How to pull data from one excel cell into html page using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM