Class: Spreadsheet::Excel::Writer::Format
- Inherits:
-
Format
- Object
- Format
- Spreadsheet::Excel::Writer::Format
show all
- Includes:
- Internals
- Defined in:
- lib/spreadsheet/excel/writer/format.rb
Overview
This class encapsulates everything that is needed to write an XF record.
Constant Summary
Constants included
from Internals
Internals::BINARY_FORMATS, Internals::BUILTIN_FORMATS, Internals::BUILTIN_STYLES, Internals::CODEPAGES, Internals::COLOR_CODES, Internals::EIGHT_BYTE_DOUBLE, Internals::ESCAPEMENT_TYPES, Internals::FONT_ENCODINGS, Internals::FONT_FAMILIES, Internals::FONT_WEIGHTS, Internals::LEAP_ERROR, Internals::NGILA_H_FX, Internals::NGILA_V_FX, Internals::NOITCERID_TXET_FX, Internals::OPCODES, Internals::OPCODE_SIZE, Internals::ROW_HEIGHT, Internals::SEDOCPO, Internals::SEDOC_ROLOC, Internals::SEGAPEDOC, Internals::SEILIMAF_TNOF, Internals::SEITILIBISIV_TEEHSKROW, Internals::SELYTS_ENIL_REDROB_FX, Internals::SEPYT_ENILREDNU, Internals::SEPYT_TNEMEPACSE, Internals::SGNIDOCNE_TNOF, Internals::SST_CHUNKSIZE, Internals::TWIPS, Internals::UNDERLINE_TYPES, Internals::WORKSHEET_VISIBILITIES, Internals::XF_BORDER_LINE_STYLES, Internals::XF_H_ALIGN, Internals::XF_TEXT_DIRECTION, Internals::XF_V_ALIGN
Instance Attribute Summary collapse
Attributes inherited from Format
#font, #name, #number_format, #pattern, #rotation, #used_merge
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Internals
#binfmt, #opcode
Methods inherited from Format
#align=, #border, #border=, #border_color, #border_color=, #bottom_color, #center_across!, #cross_down, #date?, #date_or_time?, #datetime?, #indent_level, #number?, #time?, #update_format
Methods included from Datatypes
append_features
#ivar_name, #method_name
Constructor Details
#initialize(writer, workbook, format = workbook.default_format, opts = {}) ⇒ Format
Returns a new instance of Format.
43
44
45
46
47
48
49
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 43
def initialize writer, workbook, format=workbook.default_format, opts={}
@opts = { :type => :format }.merge opts
@format = format
@writer = writer
@workbook = workbook
super format
end
|
Instance Attribute Details
Returns the value of attribute format.
42
43
44
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 42
def format
@format
end
|
Class Method Details
.boolean(*args) ⇒ Object
12
13
14
15
16
17
18
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 12
def Format.boolean *args
args.each do |key|
define_method key do
@format.send("#{key}?") ? 1 : 0
end
end
end
|
.color(key, default) ⇒ Object
19
20
21
22
23
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 19
def Format.color key, default
define_method key do
color_code(@format.send(key) || default)
end
end
|
.line_style(key, default) ⇒ Object
24
25
26
27
28
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 24
def Format.line_style key, default
define_method key do
style_code(@format.send(key) || default)
end
end
|
Instance Method Details
#color_code(color) ⇒ Object
50
51
52
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 50
def color_code color
SEDOC_ROLOC[color]
end
|
#font_index ⇒ Object
56
57
58
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 56
def font_index
@writer.font_index @workbook, font.key
end
|
#horizontal_align ⇒ Object
59
60
61
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 59
def horizontal_align
XF_H_ALIGN.fetch @format.horizontal_align, 0
end
|
62
63
64
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 62
def num_format
@writer.number_format_index @workbook, @format.number_format
end
|
#style_code(style) ⇒ Object
53
54
55
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 53
def style_code style
SELYTS_ENIL_REDROB_FX[style]
end
|
#text_direction ⇒ Object
65
66
67
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 65
def text_direction
XF_TEXT_DIRECTION.fetch @format.text_direction, 0
end
|
#vertical_align ⇒ Object
68
69
70
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 68
def vertical_align
XF_V_ALIGN.fetch @format.vertical_align, 2
end
|
#write_op(writer, op, *args) ⇒ Object
71
72
73
74
75
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 71
def write_op writer, op, *args
data = args.join
writer.write [op,data.size].pack("v2")
writer.write data
end
|
#write_xf(writer, type = ) ⇒ Object
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
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 76
def write_xf writer, type=@opts[:type]
xf_type = xf_type_prot type
data = [
font_index, num_format, xf_type, xf_align, xf_rotation, xf_indent, xf_used_attr, xf_borders, xf_brdcolors, xf_pattern ]
write_op writer, 0x00e0, data.pack(binfmt(:xf))
end
|
#xf_align ⇒ Object
177
178
179
180
181
182
183
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 177
def xf_align
align = horizontal_align
align |= text_wrap << 3
align |= vertical_align << 4
align |= text_justlast << 7
align
end
|
#xf_borders ⇒ Object
184
185
186
187
188
189
190
191
192
193
194
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 184
def xf_borders
border = left
border |= right << 4
border |= top << 8
border |= bottom << 12
border |= left_color << 16
border |= right_color << 23
border |= cross_down << 30
border |= cross_up << 31
border
end
|
#xf_brdcolors ⇒ Object
195
196
197
198
199
200
201
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 195
def xf_brdcolors
border = top_color
border |= bottom_color << 7
border |= diagonal_color << 14
border |= pattern << 26
border
end
|
#xf_indent ⇒ Object
202
203
204
205
206
207
208
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 202
def xf_indent
indent = indent_level & 0x0f
indent |= shrink << 4
indent |= merge_range << 5
indent |= text_direction << 6
indent
end
|
#xf_pattern ⇒ Object
209
210
211
212
213
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 209
def xf_pattern
ptrn = pattern_fg_color
ptrn |= pattern_bg_color << 7
ptrn
end
|
#xf_rotation ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 214
def xf_rotation
rot = @format.rotation
if @format.rotation_stacked?
rot = 255
elsif rot >= -90 or rotation <= 90
rot = -rot + 90 if rot < 0
else
warn "rotation outside -90..90; rotation set to 0"
rot = 0
end
rot
end
|
#xf_type_prot(type) ⇒ Object
226
227
228
229
230
231
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 226
def xf_type_prot type
type = type.to_s.downcase == 'style' ? 0xfff4 : 0x0000
type |= locked
type |= hidden << 1
type
end
|
#xf_used_attr ⇒ Object
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
# File 'lib/spreadsheet/excel/writer/format.rb', line 232
def xf_used_attr
atr_num = num_format & 1
atr_fnt = font_index & 1
atr_fnt = 1 unless @format.font.color == :text
atr_alc = 0
if horizontal_align != 0 \
|| vertical_align != 2 \
|| indent_level > 0 \
|| shrink? || merge_range? || text_wrap?
then
atr_alc = 1
end
atr_bdr = 1
atr_pat = 0
if @format.pattern_fg_color != :border \
|| @format.pattern_bg_color != :pattern_bg \
|| pattern != 0x00
then
atr_pat = 1
end
atr_prot = hidden? || locked? ? 1 : 0
attrs = atr_num
attrs |= atr_fnt << 1
attrs |= atr_alc << 2
attrs |= atr_bdr << 3
attrs |= atr_pat << 4
attrs |= atr_prot << 5
attrs << 2
end
|