Array Not Receiving Excel Data

In my current project, I am dealing with a basic array structure, which can be seen in the visual representation below

https://i.sstatic.net/gNbXa.png

My goal is to extract the office codes contained in this array. To accomplish this task, I have opted to utilize the exceljs module within Node.js environment

Following an illustration provided on the official GitHub page, I have implemented the following code snippet

 let extractedValues =[];
    let workbook = new excel.Workbook();
    workbook.xlsx.readFile("path_to_file").then(()=>{
        var worksheet = workbook.getWorksheet('Sheet1');
        var column = worksheet.getColumn(1);
        console.log(column.values);
        extractedValues = extractedValues.push(column.values);
    });

    console.log(extractedValues);

Despite my efforts, the resulting array appears empty []. When inspecting the individual values from the Excel document via a console statement, I observe the correct values being displayed.

I'm perplexed as to what mistake I may be making in populating the array. I even attempted using the toString() method during the push operation but encountered the same issue of an empty array

Answer №1

The main issue here is that array_is is being placed outside the workbook.xlsx.readFile function. Since workbook.xlsx.readFile is asynchronous, the code flow moves on to the next statement before the current line is completed. To fix this, log your array_is inside the readFile function.

let array_is = [];
let workbook = new excel.Workbook();
workbook.xlsx.readFile("path_to_file").then(() => {
    var worksheet = workbook.getWorksheet('Sheet1');
    var col = worksheet.getColumn(1);
    console.log(col.values);
    array_is = array_is.push(col.values);
    console.log(array_is);
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Mapping drop-downs based on the length of a JSON array

Looking to generate dropdowns in a React Native mobile application based on the length of values in an API JSON array. Here's an example of the desired output: eg:- Choice 1 (Label of the Drop Down) -Sub Choice 1 . (Value Data) ...

Create an array of dynamically calculated properties from the Vuex state array, which can then be utilized in the v-model

In my Vue 3 setup, I have a Vuex store with an array in the state: const store = createStore({ state: { questions: [ { text: 'A', value: false }, { text: 'B', value: false }, { text: 'C', value: true }, ...

Master the art of using Laravel arrays in conjunction with database queries

Greetings everyone! I am currently in the process of integrating Stripe into my Laravel 5.2 framework-based website. When issuing a bill, we have to choose between it being a recurring bill or not. If it is recurring, a dropdown menu appears with all avail ...

javascript transforming a DOM layout into a hash data structure

I am looking to create a meaningful hash from specific elements within a DOM structure: <div name="stuff" class="category"> <div class="category" name="Person"> <div class="option selected" >Kennedy</div> ...

Angular JS encountered an issue with executing 'removeChild' on 'Node' for HTMLScriptElement.callback, leading to an uncaught DOMException

I am currently using Angular's JSON HTTP call. When making a post request, I experience an error during runtime: Uncaught TypeError: Cannot read property 'parentElement' of undefined at checklistFunc (masterlowerlibs.67785a6….js:42972 ...

Get the results of a MongoDB projection query in Javascript/Node.js by converting the output to

When using a query with projection to retrieve specific fields from a MongoDB collection, do I need to output the results of the query to an array? I haven't come across any examples that don't involve converting the results to an array. db.coll ...

Is there a way to determine the duration that a click was held down for?

When triggering an onClick event, I want to determine whether it was a single click or if the mouse button was held down for some time before releasing. This way, I can call the myTest() function within onClick="myTest()", which will log "mouse was click ...

"Troubleshoot the issue of a Meteor (Node.js) service becoming unresponsive

Currently running a Meteor (Node.js) app in production that is experiencing unexplained hang-ups. Despite implementing various log statements, I have pinpointed the issue to a specific method where the server consistently freezes. Are there any tools beyo ...

Error caused by the shouldDisableDate prop in MUI DatePicker's functionality

<Controller name="toDate" control={control} defaultValue={null} render={({ field }) => ( <DatePicker format="DD/MM/yyyy" value={field.value} onChange={(e) => { setToDate(e); field.onC ...

Utilizing Express.js: A Guide to Fetching File Downloads with a POST Method

Although GET requests are successful, I am facing challenges when using POST to achieve the same results. Below are the different code snippets I have attempted: 1. app.post("/download", function (req, res) { res.download("./path"); }); 2. app.post ...

Ways to invoke a specific component within ReactDOM.render in React

Currently, I am facing an issue where 2 components need to be rendered present in a single div using myProject-init.js, but both are getting called at the same time. In myProject-init.js file: ReactDOM.render( <div> <component1>in compone ...

Tips for preventing scrolling on iOS in Chrome or Safari using CSS, JavaScript, or jQuery

I've successfully implemented an input element with a click event listener that triggers a function to make another element visible using the CSS rule "display:block;". The element in question has the following styling rules: .elementExample { d ...

Creating session variables in Joomla using checkboxes and AJAX

I'm currently working on implementing session variables in Joomla with AJAX when checkboxes are selected. Below is the code snippet from select_thumb.ajax.php file: $_SESSION['ss'] = $value; $response = $_SESSION['ss']; echo ...

Adding the Edit action in React-Redux is a crucial step towards creating a dynamic

I am looking to add an edit action to my blog page in addition to the existing ADD, DELETE and GET actions. Any suggestions on how I can implement the EDIT action to make my blog editable with just a button click? Your help would be greatly appreciated. ...

Mapping various sets of latitudes and longitudes on Google Maps

I am working with multiple latitude and longitude coordinates. var latlngs = [ {lat:25.774252,lng:-80.190262}, {lat:18.466465,lng:-66.118292}, {lat:32.321384,lng:-64.757370}, {lat:25.774252,lng:-80.190262}, ]; The coordinates were ret ...

Securing data in the browser using JavaScript encryption and decrypting on the server side using Node.js

After hours of trying to encrypt a message using AES256 in the browser, send it to the server, and then decrypt it, I keep encountering this server-side error: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt Despite using crypto- ...

Searching the database to find if the username is already in use with MEAN

Help needed with signup controller code! app.controller('SignupController', function ($scope, $http, $window) { $scope.submitSignup = function () { var newUser = { username: $scope.username, ...

"Encountering a 400 Error While Attempting to Edit Requests in NodeJS/A

Currently, I am building an application using Ionic/Angular with a NodeJS backend. Within this project, I have created an update form that allows users to modify or delete a specific row. While the deletion function is working fine, I am facing some challe ...

Combining click and change events in JQuery

Is it possible to listen for both click and change events in one code block? $(document).on("click", "button.options_buy",function(event) { // code for click event } $(document).on("change", "select.options_buy",function(event) { // code for chan ...

When attempting to transfer data to a CSV file from my Firebase database, I encounter an issue where the

I am facing an issue with exporting data from my Firebase Firestore to a .csv file. I have followed all the necessary steps, but whenever I try to add the values for export, they show up as undefined. While I am not an expert in React and consider myself ...