Angular can't pass value from read xlsx file #447

Closed
opened 2016-07-27 03:51:36 +00:00 by channara · 1 comment
channara commented 2016-07-27 03:51:36 +00:00 (Migrated from github.com)

Hello all,
i coded controller in angularjs to to call "$scope.readExcel" after upload xlsx file. Then the controller will call "ReadExcelService.ColH(file)" functions from factory. but when return the data to "$scope.colHeaders" in array. I saw "undefined". could you help me what problem?
<script> var app=angular.module('myApp', []); app.factory('ReadExcelService',function(){ return{ ColH:function(f){ var cols=[]; var reader=new FileReader(); reader.onload=function(e){ var data = e.target.result; var workbook=XLSX.read(data, {type: 'binary'}); workbook.SheetNames.forEach(function(sheetName){ var headerNames=XLSX.utils.sheet_to_json(workbook.Sheets[sheetName],{header:1})[0]; headerNames.forEach(function(f){ cols.push(f); }); var dataSheet=XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]); }) return cols; }; reader.onerror=function(ex){console.log(ex);}; reader.readAsBinaryString(f); } //End CoH function }; }); //End ReadExcelService app.controller('myCtrl',['$scope','ReadExcelService',function($scope,ReadExcelService){ $scope.colHeaders=[]; $scope.readExcel=function(f){ var file = f.files[0]; switch(file.type){ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : $scope.$apply(function(){ $scope.colHeaders=ReadExcelService.ColH(file); }); //End $apply break default : alert("Unsupported Format"); break } }; }]); //End Controller </script>

Hello all, i coded controller in angularjs to to call "$scope.readExcel" after upload xlsx file. Then the controller will call "ReadExcelService.ColH(file)" functions from factory. but when return the data to "$scope.colHeaders" in array. I saw "undefined". could you help me what problem? `<script> var app=angular.module('myApp', []); app.factory('ReadExcelService',function(){ return{ ColH:function(f){ var cols=[]; var reader=new FileReader(); reader.onload=function(e){ var data = e.target.result; var workbook=XLSX.read(data, {type: 'binary'}); workbook.SheetNames.forEach(function(sheetName){ var headerNames=XLSX.utils.sheet_to_json(workbook.Sheets[sheetName],{header:1})[0]; headerNames.forEach(function(f){ cols.push(f); }); var dataSheet=XLSX.utils.sheet_to_json(workbook.Sheets[sheetName]); }) return cols; }; reader.onerror=function(ex){console.log(ex);}; reader.readAsBinaryString(f); } //End CoH function }; }); //End ReadExcelService app.controller('myCtrl',['$scope','ReadExcelService',function($scope,ReadExcelService){ $scope.colHeaders=[]; $scope.readExcel=function(f){ var file = f.files[0]; switch(file.type){ case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" : $scope.$apply(function(){ $scope.colHeaders=ReadExcelService.ColH(file); }); //End $apply break default : alert("Unsupported Format"); break } }; }]); //End Controller </script>`
SheetJSDev commented 2017-03-23 17:12:19 +00:00 (Migrated from github.com)

@channara we are adding an angular demo based on https://github.com/brexis/angular-js-xlsx -- please test and let us know if there are any issues

@channara we are adding an angular demo based on https://github.com/brexis/angular-js-xlsx -- please test and let us know if there are any issues
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: sheetjs/sheetjs#447
No description provided.