-1

Now, I am trying to let my JS program deal with Excel files(.xlsx). But, errors, as is written in Title above, get my way. I would ask you for help.

[HTML]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>ENtoJP</title>
    <link rel="stylesheet" href="enjp.css">
</head>
<body>
    <div id="question">
        <h2>Questions are here</h2>
    </div>
    <div class="button_1">
        <button>Answer_1</button>
        <button>Answer_2</button>
    </div>
    <div class="button_2">
        <button>Answer_3</button>
        <button>Answer_4</button>
    </div>
    <script src="words.js" type="module"></script>
</body>
</html>

UPDATED on 6/19 JST JS file

import xlsx from 'xlsx';

fetch("/Word_List_1.xlsx")
  .then(response => response.arrayBuffer())
  .then(data => {
    let workbook = xlsx.read(data, { type: 'array' });

    let sheets = workbook.SheetNames;
    console.log(sheets);

    let workSheet = workbook.Sheets['Sheet1'];

    let range = workSheet['!ref'];
    console.log('range = ' + range);

    let itemA1 = workSheet['A1'];
    console.log(itemA1);
  })
  .catch(error => {
    console.error('An error occurred while reading the Excel file:', error);
  });

Could you give me an idea to have my code work?

UPDATE on my USQ; With your kind support, I changed the JS codes, but could not did it. Here is an error from the Browser (MS EDGE). enter image description here

And attached are pics of an Excel file and a vsc screen.

I would appreciate you if you could kindly give any idea to let the js file read an Excel file.

th168
  • 43
  • 3
  • Does this answer your question? [What is this JavaScript "require"?](https://stackoverflow.com/questions/9901082/what-is-this-javascript-require) – MTCoster Jun 18 '23 at 14:45
  • MTCoster : You mean that "require" does not work in the browser? – th168 Jun 18 '23 at 14:48
  • Is this a nodeJs app? – Aniket Pandey Jun 18 '23 at 14:50
  • `require()` is something that runs in the nodejs environment, NOT in a browser - so you don't use it in a browser. You would use ` – jfriend00 Jun 18 '23 at 16:13
  • Thank you, everyone for your kind supports. I understand that another way is necessary, not using require function. – th168 Jun 19 '23 at 01:48

1 Answers1

0

The error you're encountering is because you're attempting to use the require function in a browser environment. The require function is typically used in Node.js for importing modules, but it's not natively available in browsers.

To resolve this issue, you'll need to use a different approach to read the Excel file in the browser. One option is to use a JavaScript library like xlsx that provides client-side functionality for working with Excel files.

Can yoou try this

fetch('Word_List_1.xlsx')
  .then(response => response.arrayBuffer())
  .then(data => {
    let workbook = xlsx.read(data, { type: 'array' });

    let sheets = workbook.SheetNames;
    console.log(sheets);

    let workSheet = workbook.Sheets['Sheet1'];

    let range = workSheet['!ref'];
    console.log('range = ' + range);

    let itemA1 = workSheet['A1'];
    console.log(itemA1);
  })
  .catch(error => {
    console.error('An error occurred while reading the Excel file:', error);
  });

Replace this if it's not worked

<script src="words.js" type="module"></script>
Shanu
  • 124
  • 4
  • Or he can simply use import function of javascript – Aniket Pandey Jun 18 '23 at 14:57
  • Thank you for your kind advice. Currently, I got lost in JS errors, which I told in the Question fields above. I used "import xlsx from 'xlsx'" and "fetch()", but it did not work. – th168 Jun 19 '23 at 14:00