I have written some code that appends row data into an excel worksheet using javascript and exceljs.
This file works perfectly fine independently without any HTML. However, my greater objective is to save form data into an excel file.
Here is the javascript code:
//Part1
const info = [];
function display(e){
e.preventDefault();
var nm = document.getElementById("nm").value;
var ag = document.getElementById("ag").value;
var data = [nm, ag];
info.push(data);
var text = document.createElement("h2");
text.innerHTML = info;
document.body.appendChild(text);
console.log(info);
// ----------------------------------------------
//Part2
var Excel = require('exceljs');
var workbook = new Excel.Workbook();
workbook.xlsx.readFile("AppendRow.xlsx").then(function(){
worksheet = workbook.getWorksheet("Main")
worksheet.addRows(data);
workbook.xlsx.writeFile("AppendRow.xlsx")
});
}
In Part1, I am formatting the input details into a list so that I can easily append the data into excel....it puts all the user credentials into a list.
In Part2, it fetches the excel file and appends the data into a row (at least this is what I expect it to do, as I had mentioned it works fine independently but not when combined this way.)
Here is the HTML part:
<head>
<h1 style="font-family: Broadway">HTML to Append Row Function :-)</h1>
</head>
<body>
<script type="text/javascript" src="trial2.js"></script>
<form method='POST' onsubmit = 'return(display())'>
<input type="text" id='nm' placeholder="Name">
<input type="number" id='ag' placeholder="Age">
<input type="submit" value="Submit">
</form>
</body>
On running the HTML file on the browser, I don't see any output. Nor do I see any new row appended into the excel file.
I assume the problem to be in linking the two files...so I copied the entire JavaScript code into the HTML script tag, but it still didn't work.