0

I want to store the data collected from the HTML form into an excel spreadsheet. When I run the JavaScript file on anaconda prompt it works perfectly fine and when the HTML file is executed on the browser, there are no error. But when I try to link them(using a script tag), I get an error "Uncaught ReferenceError: require is not defined".

Here is the JavaScript code:

const info = [];

function display(e){
    //Part1
    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")
    });

    alert('Row has been Appended!')
}

In Part1, the input data is formatted into a list so that it can be easily appended into the excel file.

In Part2, the list is being appended to the excel file.

Here is the HTML part of the code:

<script src="script.js"></script>
<form onsubmit="display(event)"> 
  <input type="text" placeholder="Name" id="nm">
  <input type="text" placeholder="Age" id="ag">
  <input type="submit" value="Submit">
</form>

I can't really figure what could be wrong....According to another source online, I installed browserify and tried to execute it using this module but it didn't work.

Diya Giriyan
  • 27
  • 1
  • 5
  • 1
    Browsers do not support reading files specified by file name, so you can't get that code working in a browser. – Quentin Dec 01 '21 at 21:15
  • `require` is not defined in vanilla JavaScript. You will have to use a library like require.js (https://requirejs.org/) in order to use it. Also the comment above. – Jesse Dec 01 '21 at 21:16

0 Answers0