TypeError:无法读取属性' getRow'未定义的

时间:2017-05-24 13:19:47

标签: protractor jasmine-node exceljs

试过 cellValue = worksheet.getRow(1).getCell(1).value;

和 cellValue =的console.log(worksheet.getCell(' A1'))

请在下面找到我的代码:

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


    } 

2 个答案:

答案 0 :(得分:0)

现有库代码存在问题。如果要使其工作,则必须对文件AppData\Roaming\npm\node_modules\exceljs\dist\es5\xlsx\xform\sheet\worksheet-xform.js中的现有代码进行少量更改。将第284行的代码替换为以下内容:

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

至于阅读文件的代码,请使用以下代码。

注意:我全局安装了exceljs库,我使用的是版本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();

答案 1 :(得分:0)

它可能是由于对象映射到excel工作表而不是文本数据而发生的。

请创建另一个Excel工作表,然后再次检查将解决此问题。'

下面给出了使用node.js读取Excel工作表的示例代码,

var Excel = require('exceljs');

var wb = new Excel.Workbook();
var path = require('path');
var filePath = path.resolve(__dirname,'sample.xlsx');

wb.xlsx.readFile(filePath).then(function(){

    var sh = wb.getWorksheet("Sheet1");

    sh.getRow(1).getCell(2).value = 32;
    wb.xlsx.writeFile("sample2.xlsx");
    console.log("Row-3 | Cell-2 - "+sh.getRow(3).getCell(2).value);

    console.log(sh.rowCount);
    //Get all the rows data [1st and 2nd column]
    for (i = 1; i <= sh.rowCount; i++) {
        console.log(sh.getRow(i).getCell(1).value);
        console.log(sh.getRow(i).getCell(2).value);
    }
});