forked from brexis/angular-js-xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-js-xlsx.ts
More file actions
84 lines (71 loc) · 3.39 KB
/
angular-js-xlsx.ts
File metadata and controls
84 lines (71 loc) · 3.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
'use strict';
/// <reference path="./node_modules/@types/angular/index.d.ts" />
/// <reference path="./node_modules/@types/jquery/index.d.ts" />
declare var XLS: any;
angular.module('angular-js-xlsx', [])
.directive('jsXls', function () {
return {
restrict: 'E',
template: '<input type="file" />',
replace: true,
scope: {
xlsxReadOptions: '=',
onread: '&'
},
link: function (scope: angular.IScope, element: angular.IRootElementService, attrs: angular.IAttributes) {
function handleSelect() {
var files = this.files;
for (var i = 0, f = files[i]; i != files.length; ++i) {
let reader = new FileReader();
reader.result
var name = f.name;
reader.onload = function (e) {
let data = this.result;
/* if binary string, read with type 'binary' */
try {
let options = scope.xlsxReadOptions ? scope.xlsxReadOptions : {};
options.type = 'binary';
var workbook = XLSX.read(data, options);
if (attrs.onread) {
if (scope.onread) {
scope.onread({ workbook: workbook });
}
}
} catch (e) {
if (attrs.onerror) {
var handleError = scope[attrs.onerror];
if (typeof handleError === "function") {
handleError(e);
// test
}
}
}
// Clear input file
element.val('');
};
//extend FileReader
if (!FileReader.prototype.readAsBinaryString) {
FileReader.prototype.readAsBinaryString = function (fileData) {
var binary = "";
var pt = this;
var reader = new FileReader();
reader.onload = function (e) {
var bytes = new Uint8Array(reader.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
//pt.result - readonly so assign binary
pt.content = binary;
$(pt).trigger('onload');
}
reader.readAsArrayBuffer(fileData);
}
}
reader.readAsBinaryString(f);
}
}
element.on('change', handleSelect);
}
};
});