@@ -38,7 +38,7 @@ export class CoreFile implements IExcelFile {
38
38
*/
39
39
export class WorkbookRelsFile implements IExcelFile {
40
40
public writeElement ( folder : JSZip , worksheetData : WorksheetData ) {
41
- const hasSharedStrings = worksheetData . isEmpty === false ;
41
+ const hasSharedStrings = ! worksheetData . isEmpty || worksheetData . options . alwaysExportHeaders ;
42
42
folder . file ( 'workbook.xml.rels' , ExcelStrings . getWorkbookRels ( hasSharedStrings ) ) ;
43
43
}
44
44
}
@@ -71,11 +71,11 @@ export class WorksheetFile implements IExcelFile {
71
71
public async writeElementAsync ( folder : JSZip , worksheetData : WorksheetData ) {
72
72
return new Promise < void > ( resolve => {
73
73
this . prepareDataAsync ( worksheetData , ( cols , rows ) => {
74
- const hasTable = ! worksheetData . isEmpty && worksheetData . options . exportAsTable ;
75
- const isHierarchicalGrid = worksheetData . data [ 0 ] ?. type === ExportRecordType . HierarchicalGridRecord ;
74
+ const hasTable = ( ! worksheetData . isEmpty || worksheetData . options . alwaysExportHeaders )
75
+ && worksheetData . options . exportAsTable ;
76
76
77
77
folder . file ( 'sheet1.xml' , ExcelStrings . getSheetXML (
78
- this . dimension , this . freezePane , cols , rows , hasTable , this . maxOutlineLevel , isHierarchicalGrid ) ) ;
78
+ this . dimension , this . freezePane , cols , rows , hasTable , this . maxOutlineLevel , worksheetData . isHierarchical ) ) ;
79
79
resolve ( ) ;
80
80
} ) ;
81
81
} ) ;
@@ -87,14 +87,15 @@ export class WorksheetFile implements IExcelFile {
87
87
const dictionary = worksheetData . dataDictionary ;
88
88
this . rowIndex = 0 ;
89
89
90
- if ( worksheetData . isEmpty ) {
90
+ if ( worksheetData . isEmpty && ( ! worksheetData . options . alwaysExportHeaders || worksheetData . owner . columns . length === 0 ) ) {
91
91
sheetData += '<sheetData/>' ;
92
92
this . dimension = 'A1' ;
93
93
done ( '' , sheetData ) ;
94
94
} else {
95
95
const owner = worksheetData . owner ;
96
- const isHierarchicalGrid = worksheetData . data [ 0 ] . type === ExportRecordType . HierarchicalGridRecord ;
96
+ const isHierarchicalGrid = worksheetData . isHierarchical ;
97
97
const hasMultiColumnHeader = worksheetData . hasMultiColumnHeader ;
98
+
98
99
const hasUserSetIndex = owner . columns . some ( col => col . exportIndex !== undefined ) ;
99
100
100
101
const height = worksheetData . options . rowHeight ;
@@ -182,7 +183,7 @@ export class WorksheetFile implements IExcelFile {
182
183
const indexOfLastPinnedColumn = worksheetData . indexOfLastPinnedColumn ;
183
184
const frozenColumnCount = indexOfLastPinnedColumn + 1 ;
184
185
let firstCell = ExcelStrings . getExcelColumn ( frozenColumnCount ) + freezeHeaders ;
185
- if ( indexOfLastPinnedColumn !== - 1 &&
186
+ if ( indexOfLastPinnedColumn !== undefined && indexOfLastPinnedColumn !== - 1 &&
186
187
! worksheetData . options . ignorePinning &&
187
188
! worksheetData . options . ignoreColumnsOrder ) {
188
189
this . freezePane =
@@ -209,7 +210,7 @@ export class WorksheetFile implements IExcelFile {
209
210
sheetData += rows ;
210
211
sheetData += '</sheetData>' ;
211
212
212
- if ( hasMultiColumnHeader ) {
213
+ if ( hasMultiColumnHeader && this . mergeCellsCounter > 0 ) {
213
214
sheetData += `<mergeCells count="${ this . mergeCellsCounter } ">${ this . mergeCellStr } </mergeCells>` ;
214
215
}
215
216
@@ -223,40 +224,41 @@ export class WorksheetFile implements IExcelFile {
223
224
const height = worksheetData . options . rowHeight ;
224
225
this . rowHeight = height ? ' ht="' + height + '" customHeight="1"' : '' ;
225
226
226
- const isHierarchicalGrid =
227
- worksheetData . data . some ( r => r . type === ExportRecordType . HeaderRecord || r . type === ExportRecordType . HierarchicalGridRecord ) ;
227
+ const isHierarchicalGrid = worksheetData . isHierarchical ;
228
228
const hasUserSetIndex = worksheetData . owner . columns . some ( c => c . exportIndex !== undefined ) ;
229
229
230
230
let recordHeaders = [ ] ;
231
231
232
232
yieldingLoop ( worksheetData . rowCount - 1 , 1000 ,
233
233
( i ) => {
234
- if ( ! isHierarchicalGrid ) {
235
- if ( hasUserSetIndex ) {
236
- recordHeaders = worksheetData . rootKeys ;
234
+ if ( ! worksheetData . isEmpty ) {
235
+ if ( ! isHierarchicalGrid ) {
236
+ if ( hasUserSetIndex ) {
237
+ recordHeaders = worksheetData . rootKeys ;
238
+ } else {
239
+ recordHeaders = worksheetData . owner . columns
240
+ . filter ( c => c . headerType !== HeaderType . MultiColumnHeader && ! c . skip )
241
+ . sort ( ( a , b ) => a . startIndex - b . startIndex )
242
+ . sort ( ( a , b ) => a . pinnedIndex - b . pinnedIndex )
243
+ . map ( c => c . field ) ;
244
+ }
237
245
} else {
238
- recordHeaders = worksheetData . owner . columns
239
- . filter ( c => c . headerType !== HeaderType . MultiColumnHeader && ! c . skip )
240
- . sort ( ( a , b ) => a . startIndex - b . startIndex )
241
- . sort ( ( a , b ) => a . pinnedIndex - b . pinnedIndex )
242
- . map ( c => c . field ) ;
243
- }
244
- } else {
245
- const record = worksheetData . data [ i ] ;
246
+ const record = worksheetData . data [ i ] ;
246
247
247
- if ( record . type === ExportRecordType . HeaderRecord ) {
248
- const recordOwner = worksheetData . owners . get ( record . owner ) ;
249
- const hasMultiColumnHeaders = recordOwner . columns . some ( c => ! c . skip && c . headerType === HeaderType . MultiColumnHeader ) ;
248
+ if ( record . type === ExportRecordType . HeaderRecord ) {
249
+ const recordOwner = worksheetData . owners . get ( record . owner ) ;
250
+ const hasMultiColumnHeaders = recordOwner . columns . some ( c => ! c . skip && c . headerType === HeaderType . MultiColumnHeader ) ;
250
251
251
- if ( hasMultiColumnHeaders ) {
252
- this . hGridPrintMultiColHeaders ( worksheetData , rowDataArr , record , recordOwner ) ;
252
+ if ( hasMultiColumnHeaders ) {
253
+ this . hGridPrintMultiColHeaders ( worksheetData , rowDataArr , record , recordOwner ) ;
254
+ }
253
255
}
256
+
257
+ recordHeaders = Object . keys ( worksheetData . data [ i ] . data ) ;
254
258
}
255
259
256
- recordHeaders = Object . keys ( worksheetData . data [ i ] . data ) ;
260
+ rowDataArr . push ( this . processRow ( worksheetData , i , recordHeaders , isHierarchicalGrid ) ) ;
257
261
}
258
-
259
- rowDataArr . push ( this . processRow ( worksheetData , i , recordHeaders , isHierarchicalGrid ) ) ;
260
262
} ,
261
263
( ) => {
262
264
done ( rowDataArr . join ( '' ) ) ;
@@ -404,7 +406,8 @@ export class WorkbookFile implements IExcelFile {
404
406
*/
405
407
export class ContentTypesFile implements IExcelFile {
406
408
public writeElement ( folder : JSZip , worksheetData : WorksheetData ) {
407
- folder . file ( '[Content_Types].xml' , ExcelStrings . getContentTypesXML ( ! worksheetData . isEmpty , worksheetData . options . exportAsTable ) ) ;
409
+ const hasSharedStrings = ! worksheetData . isEmpty || worksheetData . options . alwaysExportHeaders ;
410
+ folder . file ( '[Content_Types].xml' , ExcelStrings . getContentTypesXML ( hasSharedStrings , worksheetData . options . exportAsTable ) ) ;
408
411
}
409
412
}
410
413
@@ -436,7 +439,10 @@ export class TablesFile implements IExcelFile {
436
439
public writeElement ( folder : JSZip , worksheetData : WorksheetData ) {
437
440
const columnCount = worksheetData . columnCount ;
438
441
const lastColumn = ExcelStrings . getExcelColumn ( columnCount - 1 ) + worksheetData . rowCount ;
439
- const dimension = 'A1:' + lastColumn ;
442
+ const autoFilterDimension = 'A1:' + lastColumn ;
443
+ const tableDimension = worksheetData . isEmpty
444
+ ? 'A1:' + ExcelStrings . getExcelColumn ( columnCount - 1 ) + ( worksheetData . rowCount + 1 )
445
+ : autoFilterDimension ;
440
446
const hasUserSetIndex = worksheetData . owner . columns . some ( c => c . exportIndex !== undefined ) ;
441
447
const values = hasUserSetIndex
442
448
? worksheetData . rootKeys
@@ -463,7 +469,7 @@ export class TablesFile implements IExcelFile {
463
469
sortString = `<sortState ref="A2:${ lastColumn } "><sortCondition descending="${ dir } " ref="${ sc } 1:${ sc } 15"/></sortState>` ;
464
470
}
465
471
466
- folder . file ( 'table1.xml' , ExcelStrings . getTablesXML ( dimension , tableColumns , sortString ) ) ;
472
+ folder . file ( 'table1.xml' , ExcelStrings . getTablesXML ( autoFilterDimension , tableDimension , tableColumns , sortString ) ) ;
467
473
}
468
474
}
469
475
0 commit comments