-1

I have a simple array, as shown in the image below

Excel

I want to get the office codes in an array. I am using the node exceljs module to achieve this

As per the example from the github page, I have this piece of code

 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);

However, the array returns []. If I print the values from the excel alone using a console statement, the I am able to see the values printed from the excel correctly.

What am I doing wrong in pushing the values to an array. I have also using the toString() method in the array push statement but the array was not populated.

demouser123
  • 4,108
  • 9
  • 50
  • 82

1 Answers1

0

array_is is placed outside the workbook.xlsx.readFile that's the main issue here. workbook.xlsx.readFile is an asynchronous function, so the code flow advances to the next statement without waiting for the completion of current line. So 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);
});
Ashwin Kumar
  • 1,228
  • 7
  • 24