protractorjasmine-nodeexceljs

TypeError: Cannot read property 'getRow' of undefined


Tried with cellValue= worksheet.getRow(1).getCell(1).value;

and cellValue=console.log(worksheet.getCell('A1'))

please find below my code :

cellread2=function(){
        var Excel; 
        var filePath = path.resolve(__dirname,'E:/excel.xlsx');
         if (typeof require !== 'undefined') {
                    Excel = require('C:/Users/user/AppData/Roaming/npm/node_modules/exceljs/dist/exceljs'); 
         }
        var wb = new Excel.Workbook();
        console.log(wb);
        wb.xlsx.readFile(filePath);
        var worksheet = wb.getWorksheet('Sheet1');
        //cellValue= worksheet.getRow(1).getCell(1).value;//Error :TypeError: Cannot read property 'getRow' of undefined
        cellValue=console.log(worksheet.getCell('A1'))// TypeError: Cannot read property 'getCell' of undefined
         console.log(cellValue);


    } 

Solution

  • There is a problem with the existing library code. If you want to make it work you will have to make few changes in the existing code in file AppData\Roaming\npm\node_modules\exceljs\dist\es5\xlsx\xform\sheet\worksheet-xform.js. Replace the code at line 284 with the following :

    if (drawing.anchors && drawing.anchors.length > 0) {
            drawing.anchors.forEach(function (anchor) {              
              if (anchor.medium && anchor.range) {
                var image = {
                  type: 'image',
                  imageId: anchor.medium.index,
                  range: anchor.range
                };
                model.media.push(image);
              }
            });
          }
    

    And as for the code for reading the file use the following code.

    Note: I installed exceljs library globally and I am using version 4.6.1

    var path = require('path');
    var Excel = require('exceljs');
    var cellread2 = function () {
      var filePath = path.resolve('E:', 'excel.xlsx');
      var wb = new Excel.Workbook();
      wb.xlsx.readFile(filePath).then(function (data) {
        var worksheet = wb.getWorksheet('Sheet1');
        var cellValue = worksheet.getRow(1).getCell(1).value;
        console.log('cellValue', cellValue);
      }).catch(function (e) {
        console.log(e);
      });
    }
    cellread2();