0

The code :

    var workbook = XLSX.read(data, {type: 'binary'});
    var first_sheet_name = workbook.SheetNames[0];
    var worksheet = workbook.Sheets[first_sheet_name];
    var k=1
    var desired_cell = worksheet['A'+k].w;

The above code runs fine But :

       for (var i = 1; i <= 50; i++) {
        var obj = {};
        var cell = (worksheet[('A'+i)]).w;
        obj.PickUpDate = cell;
        obj.PickUpTimeSlot = cell;
        obj.PickUpAddress = cell;
        obj.DeliveryAddress = cell;
        obj.BoxType = cell;
        array.push(obj);
    }

this code gives an error:

Uncaught TypeError: Cannot read property 'w' of undefined
at FileReader.reader.onload 

The line:

var cell = (worksheet[('A'+i)]).w;

is fine as I can print the 'cell' to console. Both the snippet are in FileReader.reader.onload function. I am using xlsx package https://github.com/SheetJS/js-xlsx

What is wrong with that code?

Kewl
  • 3,327
  • 5
  • 26
  • 45

1 Answers1

0

From the SheetJS/js-xlsx docs :

Built-in export utilities (such as the CSV exporter) will use the w text if it is available. To change a value, be sure to delete cell.w (or set it to undefined) before attempting to export. The utilities will regenerate the w text from the number format (cell.z) and the raw value if possible.

You can't be sure that (worksheet[('A'+i)]).w is defined.

I don't know what your worksheet data is but maybe that could do the job :

var cell = (typeof (worksheet[('A'+i)]).w !== 'undefined') ? worksheet[('A'+i)]).w : worksheet[('A'+i)]).v;

Hope it helps.

Pintouch
  • 2,630
  • 1
  • 15
  • 22