forked from iwiznia/Ext.ux.Exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorksheet.js
183 lines (154 loc) · 5.37 KB
/
Worksheet.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/**
* @class Ext.ux.Exporter.ExcelFormatter.Worksheet
* @extends Object
* Represents an Excel worksheet
* @cfg {Ext.data.Store} store The store to use (required)
*/
Ext.define("Ext.ux.exporter.excelFormatter.Worksheet", {
constructor: function(store, config) {
config = config || {};
this.store = store;
Ext.applyIf(config, {
hasTitle : true,
hasHeadings: true,
stripeRows : true,
title : "Workbook",
columns : store.fields == undefined ? {} : store.fields.items
});
Ext.apply(this, config);
Ext.ux.exporter.excelFormatter.Worksheet.superclass.constructor.apply(this, arguments);
},
/**
* @property dateFormatString
* @type String
* String used to format dates (defaults to "Y-m-d"). All other data types are left unmolested
*/
dateFormatString: "Y-m-d",
worksheetTpl: new Ext.XTemplate(
'<ss:Worksheet ss:Name="{title}">',
'<ss:Names>',
'<ss:NamedRange ss:Name="Print_Titles" ss:RefersTo="=\'{title}\'!R1:R2" />',
'</ss:Names>',
'<ss:Table x:FullRows="1" x:FullColumns="1" ss:ExpandedColumnCount="{colCount}" ss:ExpandedRowCount="{rowCount}">',
'{columns}',
'<ss:Row ss:Height="38">',
'<ss:Cell ss:StyleID="title" ss:MergeAcross="{colCount - 1}">',
'<ss:Data xmlns:html="http://www.w3.org/TR/REC-html40" ss:Type="String">',
'<html:B><html:U><html:Font html:Size="15">{title}',
'</html:Font></html:U></html:B></ss:Data><ss:NamedCell ss:Name="Print_Titles" />',
'</ss:Cell>',
'</ss:Row>',
'<ss:Row ss:AutoFitHeight="1">',
'{header}',
'</ss:Row>',
'{rows}',
'</ss:Table>',
'<x:WorksheetOptions>',
'<x:PageSetup>',
'<x:Layout x:CenterHorizontal="1" x:Orientation="Landscape" />',
'<x:Footer x:Data="Page &P of &N" x:Margin="0.5" />',
'<x:PageMargins x:Top="0.5" x:Right="0.5" x:Left="0.5" x:Bottom="0.8" />',
'</x:PageSetup>',
'<x:FitToPage />',
'<x:Print>',
'<x:PrintErrors>Blank</x:PrintErrors>',
'<x:FitWidth>1</x:FitWidth>',
'<x:FitHeight>32767</x:FitHeight>',
'<x:ValidPrinterInfo />',
'<x:VerticalResolution>600</x:VerticalResolution>',
'</x:Print>',
'<x:Selected />',
'<x:DoNotDisplayGridlines />',
'<x:ProtectObjects>False</x:ProtectObjects>',
'<x:ProtectScenarios>False</x:ProtectScenarios>',
'</x:WorksheetOptions>',
'</ss:Worksheet>'
),
/**
* Builds the Worksheet XML
* @param {Ext.data.Store} store The store to build from
*/
render: function(store) {
return this.worksheetTpl.apply({
header : this.buildHeader(),
columns : this.buildColumns().join(""),
rows : this.buildRows().join(""),
colCount: this.columns.length,
rowCount: this.store.getCount() + 2,
title : this.title
});
},
buildColumns: function() {
var cols = [];
Ext.each(this.columns, function(column) {
cols.push(this.buildColumn());
}, this);
return cols;
},
buildColumn: function(width) {
return Ext.String.format('<ss:Column ss:AutoFitWidth="1" ss:Width="{0}" />', width || 164);
},
buildRows: function() {
var rows = [];
this.store.each(function(record, index) {
rows.push(this.buildRow(record, index));
}, this);
return rows;
},
buildHeader: function() {
var cells = [];
Ext.each(this.columns, function(col) {
var title;
//if(col.dataIndex) {
if (col.text != undefined) {
title = col.text;
} else if(col.name) {
//make columns taken from Record fields (e.g. with a col.name) human-readable
title = col.name.replace(/_/g, " ");
title = Ext.String.capitalize(title);
}
cells.push(Ext.String.format('<ss:Cell ss:StyleID="headercell"><ss:Data ss:Type="String">{0}</ss:Data><ss:NamedCell ss:Name="Print_Titles" /></ss:Cell>', title));
//}
}, this);
return cells.join("");
},
buildRow: function(record, index) {
var style,
cells = [];
if (this.stripeRows === true) style = index % 2 == 0 ? 'even' : 'odd';
Ext.each(this.columns, function(col) {
var name = col.name || col.dataIndex;
if(name) {
//if given a renderer via a ColumnModel, use it and ensure data type is set to String
if (Ext.isFunction(col.renderer)) {
var value = col.renderer(record.get(name), null, record),
type = "String";
} else {
var value = record.get(name),
type = this.typeMappings[col.type || record.fields.get(name).type.type];
}
cells.push(this.buildCell(value, type, style).render());
}
}, this);
return Ext.String.format("<ss:Row>{0}</ss:Row>", cells.join(""));
},
buildCell: function(value, type, style) {
if (type == "DateTime" && Ext.isFunction(value.format)) value = value.format(this.dateFormatString);
return new Ext.ux.exporter.excelFormatter.Cell({
value: value,
type : type,
style: style
});
},
/**
* @property typeMappings
* @type Object
* Mappings from Ext.data.Record types to Excel types
*/
typeMappings: {
'int' : "Number",
'string': "String",
'float' : "Number",
'date' : "DateTime"
}
});