-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
index.html
163 lines (147 loc) · 6.26 KB
/
index.html
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#drop_xlsx {
border: 2px dashed #CCC;
padding: 10px;
margin: 10px;
}
.selected {
border: 1px solid #00F;
}
</style>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.8.0/xlsx.full.min.js"></script>
<script type="text/javascript" src="xlsx-calc.js"></script>
</head>
<body ng-app="xlsxApp">
<div id="drop_xlsx">
Arraste o arquivo aqui
</div>
<div ng-controller="xlsxCtrl">
<table>
<tr ng-repeat="row in sheet track by $index">
<td ng-repeat="cell in row track by $index">
<input type="text" ng-model="cell.v" onkeydown="excel()" ng-change="recalc()"/>
</td>
</tr>
</table>
</div>
<script type="text/javascript">
/* global angular, XLSX_CALC, XLSX */
var tdSelection, originalValue;
function setSelected(td) {
td.classList.add('selected');
tdSelection.classList.remove('selected');
tdSelection = td;
}
function excel(e) {
var event = window.event ? window.event : e;
var td = tdSelection || (tdSelection = event.target.parentNode);
var tr = td.parentNode;
var tdIndex = td.cellIndex;
var table = tr.parentNode;
var trIndex = tr.rowIndex;
if (event.keyCode === 38) {
var tdUp = table.children[trIndex-1].children[tdIndex];
setSelected(tdUp);
}
if (event.keyCode === 37) {
var tdLeft = table.children[trIndex].children[tdIndex-1];
setSelected(tdLeft);
}
if (event.keyCode === 39) {
var tdRight = table.children[trIndex].children[tdIndex+1];
setSelected(tdRight);
}
if (event.keyCode === 40) {
var tdDown = table.children[trIndex+1].children[tdIndex];
setSelected(tdDown);
}
if (isArrow(event)) {
event.preventDefault();
var val = originalValue || (originalValue = event.target.value);
var cel = XLSX_CALC.int_2_col_str(tdSelection.cellIndex) + (tdSelection.parentNode.rowIndex + 1);
val = val + cel;
event.target.value = val;
}
if (event.keyCode === 13 || !isArrow(event)) {
if (tdSelection) {
tdSelection.classList.remove('selected');
}
tdSelection = null;
originalValue = null;
}
}
function isArrow(event) {
return 37 <= event.keyCode && event.keyCode <= 40;
}
angular
.module('xlsxApp', [])
.controller('xlsxCtrl', function($scope) {
$scope.recalc = function() {
XLSX_CALC($scope.workbook);
};
function max_row(str_range) {
return parseInt(str_range.split(':')[1].replace(/^[A-Z]+/, ''), 10);
}
function max_col(str_range) {
return XLSX_CALC.col_str_2_int(str_range.split(':')[1]);
}
function convert2matrix(sheet) {
var matrix = [];
var mr = max_row(sheet['!ref']);
var mc = max_col(sheet['!ref']);
for (var i = 0; i <= mr; i++) {
var row = [];
for (var j = 0; j <= mc; j++) {
var cell_name = XLSX_CALC.int_2_col_str(j) + (i+1);
row.push(sheet[cell_name] || (sheet[cell_name] = {v:''}));
}
matrix.push(row);
}
return matrix;
}
function handleDrop(e) {
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
var i, f;
for (i = 0, f = files[i]; i != files.length; ++i) {
var reader = new FileReader();
//var name = f.name;
reader.onload = function(e) {
var data = e.target.result;
/* if binary string, read with type 'binary' */
$scope.workbook = XLSX.read(data, {
type: 'binary'
});
/* DO SOMETHING WITH workbook HERE */
var sheet_name = $scope.workbook.SheetNames[0];
var start = new Date().getTime();
XLSX_CALC($scope.workbook);
console.log($scope.workbook.Sheets['Planilha2']);
console.log('calculada em', (new Date().getTime() - start) + 'ms');
$scope.sheet = convert2matrix($scope.workbook.Sheets[sheet_name]);
$scope.ref = $scope.sheet['!ref'];
$scope.$apply();
};
reader.readAsBinaryString(f);
}
}
var drop = document.getElementById('drop_xlsx');
function handleDragover(e) {
e.stopPropagation();
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
}
if(drop.addEventListener) {
drop.addEventListener('dragenter', handleDragover, false);
drop.addEventListener('dragover', handleDragover, false);
drop.addEventListener('drop', handleDrop, false);
}
});
</script>
</body>
</html>