EmberJs error for excel parser

时间:2017-04-10 01:34:31

标签: ember.js electron

I am trying to integrate ember with electron and used XLSX parser inside services to read my local excel file, but when I am injecting it to my route, I am getting this error. Please help.

My service file:

   import Ember from 'ember';
const remote=require('electron').remote;
const fs=remote.require('fs');
const XLSX = require('xlsx');
var f = "c:\\Users\\rajeev\\Downloads\\Tableau\\SuperStoreUS_2015.xlsx";


export default Ember.Service.extend({


    ExcelToJson:function(f){

        var workbook = XLSX.readfile(f.toString());
        var worksheet=workbook.Sheets['Orders'];
        alert(worksheet);
                            }

});

My Route file where I am injecting the service:

      import Ember from 'ember';
    export default Ember.Route.extend({
    sheet: Ember.inject.service('sheet'),
    message: 'test',
    actions: {

        pressButton: function() {
            let sheet=Ember.get(this,'sheet').ExcelToJson();
            var testText = this.get('sheet').ExcelToJson();
            this.set('message',testText);
        }

        }
});

My template file:

<button class="button" {{action "pressButton"}}>press</button>

Error when running on electron:

   TypeError: Cannot read property 'toString' of undefined
    at Class.ExcelToJson (serviceeg.js:544)
    at Class.pressButton (serviceeg.js:516)
    at Router.triggerEvent (vendor.js:39675)
    at trigger (vendor.js:67017)
    at Router.trigger (vendor.js:68617)
    at Class.send (vendor.js:38863)
    at Class.send (vendor.js:42875)
    at vendor.js:22521
    at Object.flaggedInstrument (vendor.js:29424)
    at vendor.js:22520

2 个答案:

答案 0 :(得分:0)

我认为xlsx文件包含许多带数据的xml文件。获取数据的一种方法是提取这些xml并解析它们。我找到了这个xlsx解析器 xlsx parser

xlsxParser = (function() {
function extractFiles(file) {
    var deferred = $.Deferred();

    zip.createReader(new zip.BlobReader(file), function(reader) {
        reader.getEntries(function(entries) {
            async.reduce(entries, {}, function(memo, entry, done) {
                var files = ['xl/worksheets/sheet1.xml', 'xl/sharedStrings.xml'];
                if (files.indexOf(entry.filename) == -1) return done(null, memo);

                entry.getData(new zip.TextWriter(), function(data) {
                    memo[entry.filename.split('/').pop()] = data;
                    done(null, memo);
                });
            }, function(err, files) {
                if (err) deferred.reject(err);
                else deferred.resolve(files);
            });
        });
    }, function(error) { deferred.reject(error); });

    return deferred.promise();
}

function extractData(files) {
    var sheet = $(files['sheet1.xml']),
        strings = $(files['sharedStrings.xml']),
        data = [];

    var colToInt = function(col) {
        var letters = ["", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"];
        var col = $.trim(col).split('');

        var n = 0;

        for (var i = 0; i < col.length; i++) {
            n *= 26;
            n += letters.indexOf(col[i]);
        }

        return n;
    };

    var Cell = function(cell) {
        cell = cell.split(/([0-9]+)/);
        this.row = parseInt(cell[1]);
        this.column = colToInt(cell[0]);
    };

    var d = sheet.find('dimension').attr('ref').split(':');
    d = _.map(d, function(v) { return new Cell(v); });

    var cols = d[1].column - d[0].column + 1,
        rows = d[1].row - d[0].row + 1;

    _(rows).times(function() {
        var _row = [];
        _(cols).times(function() { _row.push(''); });
        data.push(_row);
    });

    sheet.find('sheetData row c').each(function(i, c) {
        var $cell = $(c),
            cell = new Cell($cell.attr('r')),
            type = $cell.attr('t'),
            value = $cell.find('v').text();

        if (type == 's') value = strings.find('si t').eq(parseInt(value)).text();

        data[cell.row - d[0].row][cell.column - d[0].column] = value;
    });

    return data;
}

return {
    parse: function(file) {
        return extractFiles(file).pipe(function(files) {
            return extractData(files);
        });
    }
}})();

答案 1 :(得分:0)

你看到的错误是由这一行抛出的:

Template.myDocuments.helpers({
    'files': function(){
        var tmp = getFiles(0,[]);
        tmp.then(function(val){
        console.log(val);
        return val;
    });
    }
});

那是因为包含该行的方法声明了一个与您的测试文件名的服务级变量var workbook = XLSX.readfile(f.toString()); 冲突的局部变量f。如果您将该测试文件名移动到f方法中,则应设置该方法。