1
1
require ( './index.less' ) ;
2
2
var locale = require ( './i18n' ) ;
3
3
var tpl = require ( './index.html' ) ;
4
- var XLSX = require ( 'xlsx' ) ;
4
+
5
5
Enhancer . registerWidget ( {
6
6
construct : function ( profile , zContext ) {
7
+ this . XLSX = require ( 'xlsx' ) ;
8
+
7
9
profile = $ . extend ( {
8
10
loadData : false ,
9
11
editExcel : true ,
10
12
outputExcel : true
11
13
} , profile ) ;
14
+
15
+ var that = this ;
12
16
var $container = this . getContainer ( ) ;
13
17
this . profile = profile ;
14
18
this . $container = $container ;
15
- var that = this ;
16
19
this . workbook = { } ;
17
20
this . excelName = 'download.xlsx' ;
18
21
$container . html ( tpl ( {
@@ -25,7 +28,7 @@ Enhancer.registerWidget({
25
28
function updateWb ( ) {
26
29
var $table = $container . find ( '.sheetWindow table' ) ;
27
30
var name = $table . attr ( 'id' ) ;
28
- var data = XLSX . utils . table_to_sheet ( $table [ 0 ] ) ;
31
+ var data = that . XLSX . utils . table_to_sheet ( $table [ 0 ] ) ;
29
32
$container . find ( '.sheets a[name="' + name + '"]' ) . data ( 'sheet' , data ) ;
30
33
$container . find ( '.sheets a' ) . map ( function ( ) {
31
34
var name = $ ( this ) . attr ( 'name' ) ;
@@ -62,7 +65,7 @@ Enhancer.registerWidget({
62
65
}
63
66
}
64
67
} )
65
- var table = XLSX . utils . sheet_to_html ( sheet , {
68
+ var table = that . XLSX . utils . sheet_to_html ( sheet , {
66
69
id : name ,
67
70
editable : true
68
71
} ) ;
@@ -79,7 +82,7 @@ Enhancer.registerWidget({
79
82
$container . find ( '#outputExcel' ) . click ( function ( ) {
80
83
if ( that . workbook . Sheets ) {
81
84
updateWb ( ) ;
82
- XLSX . writeFile ( that . workbook , that . excelName ) ;
85
+ that . XLSX . writeFile ( that . workbook , that . excelName ) ;
83
86
that . trig ( 'onExcelOutputed' ) ;
84
87
}
85
88
} )
@@ -140,7 +143,7 @@ Enhancer.registerWidget({
140
143
if ( CURR_SHEET_NAME ) {
141
144
CURR_SHEET = this . workbook . Sheets [ CURR_SHEET_NAME ]
142
145
}
143
- var CURR_SHEET_TO_JSON = XLSX . utils . sheet_to_json ( CURR_SHEET ) ;
146
+ var CURR_SHEET_TO_JSON = this . XLSX . utils . sheet_to_json ( CURR_SHEET ) ;
144
147
var CURR_SHEET_ROWS = CURR_SHEET_TO_JSON . length ;
145
148
var CURR_SHEET_COLS = 0 ;
146
149
CURR_SHEET_TO_JSON . forEach ( function ( val ) {
@@ -156,9 +159,9 @@ Enhancer.registerWidget({
156
159
CURR_CELL_CONTENT = CURR_SHEET [ CURR_CELL_POS ] . v ;
157
160
}
158
161
for ( var key in this . workbook . Sheets ) {
159
- SHEETS_TO_CSV [ key ] = XLSX . utils . sheet_to_csv ( this . workbook . Sheets [ key ] ) ;
160
- SHEETS_TO_JSON [ key ] = XLSX . utils . sheet_to_json ( this . workbook . Sheets [ key ] ) ;
161
- SHEETS_TO_FORMULAE [ key ] = XLSX . utils . sheet_to_formulae ( this . workbook . Sheets [ key ] ) ;
162
+ SHEETS_TO_CSV [ key ] = this . XLSX . utils . sheet_to_csv ( this . workbook . Sheets [ key ] ) ;
163
+ SHEETS_TO_JSON [ key ] = this . XLSX . utils . sheet_to_json ( this . workbook . Sheets [ key ] ) ;
164
+ SHEETS_TO_FORMULAE [ key ] = this . XLSX . utils . sheet_to_formulae ( this . workbook . Sheets [ key ] ) ;
162
165
}
163
166
var data = {
164
167
'EXCEL_DATA' : EXCEL_DATA ,
@@ -186,12 +189,13 @@ Enhancer.registerWidget({
186
189
var that = this ;
187
190
if ( profile . loadData ) {
188
191
this . getSourceData ( profile . srcId , { } , function ( data ) {
192
+ console . log ( data ) ;
189
193
if ( data . rows ) {
190
194
data = data . rows ;
191
195
}
192
- that . workbook = XLSX . utils . book_new ( ) ;
193
- var sheet = XLSX . utils . json_to_sheet ( data ) ;
194
- XLSX . utils . book_append_sheet ( that . workbook , sheet , 'Sheet1' ) ;
196
+ that . workbook = that . XLSX . utils . book_new ( ) ;
197
+ var sheet = that . XLSX . utils . json_to_sheet ( data ) ;
198
+ that . XLSX . utils . book_append_sheet ( that . workbook , sheet , 'Sheet1' ) ;
195
199
that . __initSheet ( ) ;
196
200
that . trig ( 'complete' ) ;
197
201
} )
@@ -224,7 +228,7 @@ Enhancer.registerWidget({
224
228
that . excelName = f . name ;
225
229
var reader = new FileReader ( ) ;
226
230
reader . onload = function ( e ) {
227
- that . workbook = XLSX . read ( new Uint8Array ( e . target . result ) , {
231
+ that . workbook = that . XLSX . read ( new Uint8Array ( e . target . result ) , {
228
232
type : 'array' ,
229
233
cellFormula : false ,
230
234
cellHTML : true ,
0 commit comments