rbxl 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +81 -0
- data/README.md +196 -43
- data/ext/rbxl_native/native.c +25 -4
- data/lib/rbxl/date_format.rb +82 -0
- data/lib/rbxl/editable_cell.rb +106 -22
- data/lib/rbxl/editable_styles.rb +273 -0
- data/lib/rbxl/editable_workbook.rb +371 -21
- data/lib/rbxl/editable_worksheet.rb +143 -17
- data/lib/rbxl/errors.rb +6 -3
- data/lib/rbxl/read_only_workbook.rb +3 -15
- data/lib/rbxl/style.rb +78 -0
- data/lib/rbxl/style_registry.rb +159 -0
- data/lib/rbxl/style_xml.rb +109 -0
- data/lib/rbxl/version.rb +1 -1
- data/lib/rbxl/write_only_cell.rb +18 -6
- data/lib/rbxl/write_only_workbook.rb +53 -4
- data/lib/rbxl/write_only_worksheet.rb +268 -5
- data/lib/rbxl/xml_edit.rb +77 -0
- data/lib/rbxl.rb +6 -0
- data/sig/rbxl.rbs +27 -4
- metadata +11 -4
data/lib/rbxl/errors.rb
CHANGED
|
@@ -54,8 +54,11 @@ module Rbxl
|
|
|
54
54
|
class CellValueError < WorksheetFormatError; end
|
|
55
55
|
|
|
56
56
|
# Raised by {Rbxl::EditableCell#value=} when the assigned Ruby object is
|
|
57
|
-
# not one of the supported types (+nil+, +String+, +
|
|
58
|
-
# +
|
|
59
|
-
# {Rbxl::EditableCell} for the rationale.
|
|
57
|
+
# not one of the supported types (+nil+, +String+, +Numeric+, +true+,
|
|
58
|
+
# +false+, +Date+, +Time+, +DateTime+).
|
|
60
59
|
class EditableCellTypeError < Error; end
|
|
60
|
+
|
|
61
|
+
# Raised by {Rbxl::EditableWorkbook#defined_name} when the name does not
|
|
62
|
+
# exist, is ambiguous, or does not refer to a single cell.
|
|
63
|
+
class DefinedNameError < Error; end
|
|
61
64
|
end
|
|
@@ -191,13 +191,8 @@ module Rbxl
|
|
|
191
191
|
end
|
|
192
192
|
|
|
193
193
|
# Built-in numFmtId values that Excel resolves to date/time formats.
|
|
194
|
-
#
|
|
195
|
-
|
|
196
|
-
# tokens. See ECMA-376 part 1 §18.8.30.
|
|
197
|
-
BUILTIN_DATE_FMT_IDS = Set.new([14, 15, 16, 17, 18, 19, 20, 21, 22,
|
|
198
|
-
27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
|
|
199
|
-
45, 46, 47, 50, 51, 52, 53, 54, 55, 56,
|
|
200
|
-
57, 58]).freeze
|
|
194
|
+
# Kept for backward compatibility; see {Rbxl::DateFormat::BUILTIN_IDS}.
|
|
195
|
+
BUILTIN_DATE_FMT_IDS = DateFormat::BUILTIN_IDS
|
|
201
196
|
|
|
202
197
|
def date_styles
|
|
203
198
|
return nil unless @date_conversion
|
|
@@ -245,15 +240,8 @@ module Rbxl
|
|
|
245
240
|
date_styles.freeze
|
|
246
241
|
end
|
|
247
242
|
|
|
248
|
-
# Quoted literals, bracketed directives (e.g. [Red], [$-409]), and
|
|
249
|
-
# backslash-escaped characters never introduce date tokens, so strip
|
|
250
|
-
# them before looking for +y/m/d/h/s+.
|
|
251
243
|
def date_format_code?(code)
|
|
252
|
-
|
|
253
|
-
stripped.gsub!(/\[[^\]]*\]/, "")
|
|
254
|
-
stripped.gsub!(/"[^"]*"/, "")
|
|
255
|
-
stripped.gsub!(/\\./, "")
|
|
256
|
-
stripped.match?(/[ymdhs]/i)
|
|
244
|
+
DateFormat.date_format_code?(code)
|
|
257
245
|
end
|
|
258
246
|
|
|
259
247
|
def load_sheet_entries
|
data/lib/rbxl/style.rb
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
module Rbxl
|
|
2
|
+
# Frozen value object describing a cell style registered via
|
|
3
|
+
# {Rbxl::WriteOnlyWorkbook#add_style}.
|
|
4
|
+
#
|
|
5
|
+
# Each component is normalized to a frozen Hash with symbol keys (or
|
|
6
|
+
# +nil+ when not specified) so that two styles built from equivalent input
|
|
7
|
+
# hash equal and can share +fonts+/+fills+/+borders+/+numFmts+ entries in
|
|
8
|
+
# the emitted +styles.xml+.
|
|
9
|
+
#
|
|
10
|
+
# Rbxl::Style.new(
|
|
11
|
+
# font: { name: "Yu Gothic UI", size: 11, bold: true, color: "333333" },
|
|
12
|
+
# fill: { color: "EEEEEE" },
|
|
13
|
+
# )
|
|
14
|
+
#
|
|
15
|
+
# Colors are RRGGBB hex strings (a leading +#+ is accepted); they are
|
|
16
|
+
# written as opaque 8-digit ARGB (+FFRRGGBB+). 8-digit ARGB input is
|
|
17
|
+
# passed through as-is.
|
|
18
|
+
class Style
|
|
19
|
+
# @return [Hash{Symbol=>Object}, nil] font attributes (+name+, +size+, +bold+, +italic+, +underline+, +color+)
|
|
20
|
+
attr_reader :font
|
|
21
|
+
|
|
22
|
+
# @return [Hash{Symbol=>Object}, nil] fill attributes (+color+ for solid pattern, or +pattern_type+)
|
|
23
|
+
attr_reader :fill
|
|
24
|
+
|
|
25
|
+
# @return [Hash{Symbol=>Object}, nil] border attributes; supports +all+ for four-sided
|
|
26
|
+
# shorthand or per-side +top+/+right+/+bottom+/+left+ keys, each as
|
|
27
|
+
# <tt>{ style:, color: }</tt>
|
|
28
|
+
attr_reader :border
|
|
29
|
+
|
|
30
|
+
# @return [Hash{Symbol=>Object}, nil] alignment attributes (+horizontal+, +vertical+, +wrap_text+, +text_rotation+)
|
|
31
|
+
attr_reader :alignment
|
|
32
|
+
|
|
33
|
+
# @return [String, nil] Excel number format code
|
|
34
|
+
attr_reader :number_format
|
|
35
|
+
|
|
36
|
+
# @param font [Hash, nil]
|
|
37
|
+
# @param fill [Hash, nil]
|
|
38
|
+
# @param border [Hash, nil]
|
|
39
|
+
# @param alignment [Hash, nil]
|
|
40
|
+
# @param number_format [String, nil]
|
|
41
|
+
def initialize(font: nil, fill: nil, border: nil, alignment: nil, number_format: nil)
|
|
42
|
+
@font = normalize_hash(font, :font)
|
|
43
|
+
@fill = normalize_hash(fill, :fill)
|
|
44
|
+
@border = normalize_border(border)
|
|
45
|
+
@alignment = normalize_hash(alignment, :alignment)
|
|
46
|
+
@number_format = number_format.nil? ? nil : number_format.to_s.dup.freeze
|
|
47
|
+
freeze
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def normalize_hash(value, label)
|
|
53
|
+
return nil if value.nil?
|
|
54
|
+
raise ArgumentError, "#{label}: expected Hash, got #{value.class}" unless value.is_a?(Hash)
|
|
55
|
+
|
|
56
|
+
value.each_with_object({}) { |(k, v), out| out[k.to_sym] = v }.freeze
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def normalize_border(value)
|
|
60
|
+
return nil if value.nil?
|
|
61
|
+
raise ArgumentError, "border: expected Hash, got #{value.class}" unless value.is_a?(Hash)
|
|
62
|
+
|
|
63
|
+
sym_keyed = value.each_with_object({}) { |(k, v), out| out[k.to_sym] = v }
|
|
64
|
+
|
|
65
|
+
sides =
|
|
66
|
+
if sym_keyed.key?(:all)
|
|
67
|
+
spec = sym_keyed[:all]
|
|
68
|
+
%i[top right bottom left].each_with_object({}) { |side, out| out[side] = spec }
|
|
69
|
+
else
|
|
70
|
+
%i[top right bottom left].each_with_object({}) do |side, out|
|
|
71
|
+
out[side] = sym_keyed[side] if sym_keyed.key?(side)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
sides.transform_values { |spec| spec.nil? ? nil : normalize_hash(spec, :border).freeze }.freeze
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
module Rbxl
|
|
2
|
+
# Collects {Rbxl::Style} entries registered via
|
|
3
|
+
# {Rbxl::WriteOnlyWorkbook#add_style} and emits the corresponding
|
|
4
|
+
# +xl/styles.xml+ document.
|
|
5
|
+
#
|
|
6
|
+
# Each call to {#register} appends a new <tt>cellXf</tt> entry and returns
|
|
7
|
+
# the 1-based +style_id+ to use on cells (index 0 is reserved for the
|
|
8
|
+
# default xf so authored +style_id+ values match what Excel sees on disk).
|
|
9
|
+
# Sub-tables (+fonts+, +fills+, +borders+, +numFmts+) are deduplicated by
|
|
10
|
+
# content while keeping the +cellXfs+ ordering deterministic.
|
|
11
|
+
class StyleRegistry
|
|
12
|
+
# First custom +numFmtId+ allocated to user-supplied +number_format+
|
|
13
|
+
# strings; OOXML reserves ids below 164 for built-in formats.
|
|
14
|
+
NUMFMT_CUSTOM_BASE = 164
|
|
15
|
+
|
|
16
|
+
def initialize
|
|
17
|
+
@styles = []
|
|
18
|
+
@fonts = []
|
|
19
|
+
@fills = []
|
|
20
|
+
@borders = []
|
|
21
|
+
@numfmts = []
|
|
22
|
+
@font_index = {}
|
|
23
|
+
@fill_index = {}
|
|
24
|
+
@border_index = {}
|
|
25
|
+
@numfmt_index = {}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# @return [Boolean] true if no styles have been registered
|
|
29
|
+
def empty?
|
|
30
|
+
@styles.empty?
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Registers a style and returns its +cellXfs+ index.
|
|
34
|
+
#
|
|
35
|
+
# @param style [Rbxl::Style]
|
|
36
|
+
# @return [Integer] 1-based style id
|
|
37
|
+
def register(style)
|
|
38
|
+
@styles << resolve(style)
|
|
39
|
+
@styles.length
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @return [String] full +xl/styles.xml+ document
|
|
43
|
+
def to_xml
|
|
44
|
+
buf = +'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
|
45
|
+
buf << "\n"
|
|
46
|
+
buf << '<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">'
|
|
47
|
+
|
|
48
|
+
append_numfmts(buf)
|
|
49
|
+
append_fonts(buf)
|
|
50
|
+
append_fills(buf)
|
|
51
|
+
append_borders(buf)
|
|
52
|
+
|
|
53
|
+
buf << '<cellStyleXfs count="1"><xf numFmtId="0" fontId="0" fillId="0" borderId="0"/></cellStyleXfs>'
|
|
54
|
+
|
|
55
|
+
append_cell_xfs(buf)
|
|
56
|
+
|
|
57
|
+
buf << '<cellStyles count="1"><cellStyle name="Normal" xfId="0" builtinId="0"/></cellStyles>'
|
|
58
|
+
buf << '</styleSheet>'
|
|
59
|
+
buf
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
private
|
|
63
|
+
|
|
64
|
+
def resolve(style)
|
|
65
|
+
{
|
|
66
|
+
font_id: style.font ? intern(@fonts, @font_index, style.font) : 0,
|
|
67
|
+
fill_id: style.fill ? intern_fill(style.fill) : 0,
|
|
68
|
+
border_id: style.border ? intern_border(style.border) : 0,
|
|
69
|
+
numfmt_id: style.number_format ? intern_numfmt(style.number_format) : 0,
|
|
70
|
+
alignment: style.alignment
|
|
71
|
+
}
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def intern(table, index, key)
|
|
75
|
+
cached = index[key]
|
|
76
|
+
return cached if cached
|
|
77
|
+
|
|
78
|
+
table << key
|
|
79
|
+
index[key] = table.length
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def intern_fill(fill_hash)
|
|
83
|
+
cached = @fill_index[fill_hash]
|
|
84
|
+
return cached if cached
|
|
85
|
+
|
|
86
|
+
@fills << fill_hash
|
|
87
|
+
# +2 because fillId 0 (none) and fillId 1 (gray125) are conventionally reserved
|
|
88
|
+
slot = @fills.length + 1
|
|
89
|
+
@fill_index[fill_hash] = slot
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def intern_border(border_hash)
|
|
93
|
+
cached = @border_index[border_hash]
|
|
94
|
+
return cached if cached
|
|
95
|
+
|
|
96
|
+
@borders << border_hash
|
|
97
|
+
@border_index[border_hash] = @borders.length
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def intern_numfmt(format_code)
|
|
101
|
+
cached = @numfmt_index[format_code]
|
|
102
|
+
return cached if cached
|
|
103
|
+
|
|
104
|
+
@numfmts << format_code
|
|
105
|
+
slot = NUMFMT_CUSTOM_BASE + @numfmts.length - 1
|
|
106
|
+
@numfmt_index[format_code] = slot
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
def append_numfmts(buf)
|
|
110
|
+
return if @numfmts.empty?
|
|
111
|
+
|
|
112
|
+
buf << %(<numFmts count="#{@numfmts.length}">)
|
|
113
|
+
@numfmts.each_with_index do |code, i|
|
|
114
|
+
id = NUMFMT_CUSTOM_BASE + i
|
|
115
|
+
buf << %(<numFmt numFmtId="#{id}" formatCode="#{escape_attr(code)}"/>)
|
|
116
|
+
end
|
|
117
|
+
buf << '</numFmts>'
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def append_fonts(buf)
|
|
121
|
+
total = @fonts.length + 1 # +1 for default font 0
|
|
122
|
+
buf << %(<fonts count="#{total}">)
|
|
123
|
+
buf << '<font><sz val="11"/><name val="Calibri"/></font>'
|
|
124
|
+
@fonts.each { |f| buf << render_font(f) }
|
|
125
|
+
buf << '</fonts>'
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def append_fills(buf)
|
|
129
|
+
total = @fills.length + 2 # +2 for built-in none and gray125
|
|
130
|
+
buf << %(<fills count="#{total}">)
|
|
131
|
+
buf << '<fill><patternFill patternType="none"/></fill>'
|
|
132
|
+
buf << '<fill><patternFill patternType="gray125"/></fill>'
|
|
133
|
+
@fills.each { |f| buf << render_fill(f) }
|
|
134
|
+
buf << '</fills>'
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def append_borders(buf)
|
|
138
|
+
total = @borders.length + 1 # +1 for default empty border 0
|
|
139
|
+
buf << %(<borders count="#{total}">)
|
|
140
|
+
buf << '<border><left/><right/><top/><bottom/><diagonal/></border>'
|
|
141
|
+
@borders.each { |b| buf << render_border(b) }
|
|
142
|
+
buf << '</borders>'
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def append_cell_xfs(buf)
|
|
146
|
+
total = @styles.length + 1 # +1 for default xf 0
|
|
147
|
+
buf << %(<cellXfs count="#{total}">)
|
|
148
|
+
buf << '<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0"/>'
|
|
149
|
+
@styles.each { |xf| buf << render_xf(xf) }
|
|
150
|
+
buf << '</cellXfs>'
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
def render_font(font) = StyleXml.render_font(font)
|
|
154
|
+
def render_fill(fill) = StyleXml.render_fill(fill)
|
|
155
|
+
def render_border(border) = StyleXml.render_border(border)
|
|
156
|
+
def render_xf(xf) = StyleXml.render_xf(xf)
|
|
157
|
+
def escape_attr(value) = StyleXml.escape_attr(value)
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
module Rbxl
|
|
2
|
+
# @api private
|
|
3
|
+
#
|
|
4
|
+
# Renders the individual +styles.xml+ entries (+<font>+, +<fill>+,
|
|
5
|
+
# +<border>+, +<xf>+) for a normalized {Rbxl::Style} component. Shared by
|
|
6
|
+
# the write-only {StyleRegistry} (which emits a whole +styles.xml+) and the
|
|
7
|
+
# edit-mode {EditableStyles} (which appends entries to an existing one), so
|
|
8
|
+
# both modes produce identical markup for identical input.
|
|
9
|
+
module StyleXml
|
|
10
|
+
module_function
|
|
11
|
+
|
|
12
|
+
def render_font(font)
|
|
13
|
+
attrs = +'<font>'
|
|
14
|
+
attrs << '<b val="1"/>' if font[:bold]
|
|
15
|
+
attrs << '<i val="1"/>' if font[:italic]
|
|
16
|
+
attrs << '<u/>' if font[:underline]
|
|
17
|
+
attrs << %(<sz val="#{font[:size]}"/>) if font[:size]
|
|
18
|
+
attrs << %(<color rgb="#{escape_attr(argb(font[:color]))}"/>) if font[:color]
|
|
19
|
+
attrs << %(<name val="#{escape_attr(font[:name])}"/>) if font[:name]
|
|
20
|
+
attrs << '</font>'
|
|
21
|
+
attrs
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def render_fill(fill)
|
|
25
|
+
pattern = fill[:pattern_type] || (fill[:color] ? 'solid' : 'none')
|
|
26
|
+
buf = +'<fill><patternFill patternType="'
|
|
27
|
+
buf << escape_attr(pattern)
|
|
28
|
+
buf << '"'
|
|
29
|
+
if fill[:color] && pattern != 'none'
|
|
30
|
+
buf << '><fgColor rgb="'
|
|
31
|
+
buf << escape_attr(argb(fill[:color]))
|
|
32
|
+
buf << '"/></patternFill></fill>'
|
|
33
|
+
else
|
|
34
|
+
buf << '/></fill>'
|
|
35
|
+
end
|
|
36
|
+
buf
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def render_border_side(side, spec)
|
|
40
|
+
return %(<#{side}/>) unless spec
|
|
41
|
+
|
|
42
|
+
buf = %(<#{side} style="#{escape_attr(spec[:style] || 'thin')}">)
|
|
43
|
+
buf << %(<color rgb="#{escape_attr(argb(spec[:color]))}"/>) if spec[:color]
|
|
44
|
+
buf << %(</#{side}>)
|
|
45
|
+
buf
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def render_border(border)
|
|
49
|
+
buf = +'<border>'
|
|
50
|
+
%i[left right top bottom].each { |side| buf << render_border_side(side, border[side]) }
|
|
51
|
+
buf << '<diagonal/></border>'
|
|
52
|
+
buf
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def render_xf(xf)
|
|
56
|
+
attrs = +'<xf'
|
|
57
|
+
attrs << %( numFmtId="#{xf[:numfmt_id]}")
|
|
58
|
+
attrs << %( fontId="#{xf[:font_id]}")
|
|
59
|
+
attrs << %( fillId="#{xf[:fill_id]}")
|
|
60
|
+
attrs << %( borderId="#{xf[:border_id]}")
|
|
61
|
+
attrs << ' xfId="0"'
|
|
62
|
+
attrs << ' applyFont="1"' if xf[:font_id].positive?
|
|
63
|
+
attrs << ' applyFill="1"' if xf[:fill_id].positive?
|
|
64
|
+
attrs << ' applyBorder="1"' if xf[:border_id].positive?
|
|
65
|
+
attrs << ' applyNumberFormat="1"' if xf[:numfmt_id].positive?
|
|
66
|
+
attrs << ' applyAlignment="1"' if xf[:alignment]
|
|
67
|
+
|
|
68
|
+
if xf[:alignment]
|
|
69
|
+
attrs << '>'
|
|
70
|
+
attrs << render_alignment(xf[:alignment])
|
|
71
|
+
attrs << '</xf>'
|
|
72
|
+
else
|
|
73
|
+
attrs << '/>'
|
|
74
|
+
end
|
|
75
|
+
attrs
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def render_alignment(alignment)
|
|
79
|
+
buf = +'<alignment'
|
|
80
|
+
alignment_attributes(alignment).each { |name, value| buf << %( #{name}="#{escape_attr(value)}") }
|
|
81
|
+
buf << '/>'
|
|
82
|
+
buf
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# @return [Array<Array(String, String)>] OOXML attribute name/value pairs
|
|
86
|
+
# for an alignment Hash, in a stable order
|
|
87
|
+
def alignment_attributes(alignment)
|
|
88
|
+
pairs = []
|
|
89
|
+
pairs << ["horizontal", alignment[:horizontal].to_s] if alignment[:horizontal]
|
|
90
|
+
pairs << ["vertical", alignment[:vertical].to_s] if alignment[:vertical]
|
|
91
|
+
pairs << ["wrapText", "1"] if alignment[:wrap_text]
|
|
92
|
+
pairs << ["textRotation", alignment[:text_rotation].to_i.to_s] if alignment[:text_rotation]
|
|
93
|
+
pairs
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Normalizes a user color to the 8-digit ARGB form OOXML specifies:
|
|
97
|
+
# a leading +#+ is dropped and 6-digit +RRGGBB+ gains an opaque +FF+
|
|
98
|
+
# alpha. Other lengths pass through unchanged.
|
|
99
|
+
def argb(color)
|
|
100
|
+
hex = color.to_s.upcase.delete_prefix("#")
|
|
101
|
+
hex.length == 6 ? "FF#{hex}" : hex
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def escape_attr(value)
|
|
105
|
+
str = value.to_s
|
|
106
|
+
str.gsub(/[&<>"]/) { |c| { '&' => '&', '<' => '<', '>' => '>', '"' => '"' }[c] }
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
end
|
data/lib/rbxl/version.rb
CHANGED
data/lib/rbxl/write_only_cell.rb
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
module Rbxl
|
|
2
|
-
# Wraps a write-side cell value so that a style id can be
|
|
3
|
-
# it without widening every call site to a Hash or Array.
|
|
2
|
+
# Wraps a write-side cell value so that a style id and/or hyperlink can be
|
|
3
|
+
# associated with it without widening every call site to a Hash or Array.
|
|
4
4
|
#
|
|
5
5
|
# Instances are passed transparently to {Rbxl::WriteOnlyWorksheet#append}
|
|
6
6
|
# (or +<<+) in place of a plain value:
|
|
7
7
|
#
|
|
8
|
-
#
|
|
9
|
-
#
|
|
8
|
+
# styled = Rbxl::WriteOnlyCell.new(42, style_id: 1)
|
|
9
|
+
# linked = Rbxl::WriteOnlyCell.new("repo", style_id: 1, hyperlink: "https://...")
|
|
10
|
+
# sheet << ["id", styled, linked]
|
|
10
11
|
#
|
|
11
12
|
# The value is serialized using the same type rules as a bare value; the
|
|
12
|
-
# +style_id+, when present, is emitted as the cell's +s+ attribute.
|
|
13
|
+
# +style_id+, when present, is emitted as the cell's +s+ attribute. The
|
|
14
|
+
# +hyperlink+, when present, is collected into the worksheet's
|
|
15
|
+
# +<hyperlinks>+ element at save time. Targets starting with +#+ are
|
|
16
|
+
# treated as internal sheet anchors (e.g. +"#'Detail'!A1"+); anything
|
|
17
|
+
# else is treated as an external URL and gets a per-sheet
|
|
18
|
+
# +_rels/sheetN.xml.rels+ entry.
|
|
13
19
|
class WriteOnlyCell
|
|
14
20
|
# @return [Object] underlying Ruby value (String, Numeric, Boolean,
|
|
15
21
|
# Date/DateTime/Time, or +nil+)
|
|
@@ -18,11 +24,17 @@ module Rbxl
|
|
|
18
24
|
# @return [Integer, nil] style index into the workbook's +cellXfs+ table
|
|
19
25
|
attr_reader :style_id
|
|
20
26
|
|
|
27
|
+
# @return [String, nil] hyperlink target. Strings beginning with +#+
|
|
28
|
+
# are internal anchors; everything else is treated as an external URL.
|
|
29
|
+
attr_reader :hyperlink
|
|
30
|
+
|
|
21
31
|
# @param value [Object] Ruby value to serialize into the cell
|
|
22
32
|
# @param style_id [Integer, nil] optional style index
|
|
23
|
-
|
|
33
|
+
# @param hyperlink [String, nil] optional hyperlink target
|
|
34
|
+
def initialize(value, style_id: nil, hyperlink: nil)
|
|
24
35
|
@value = value
|
|
25
36
|
@style_id = style_id
|
|
37
|
+
@hyperlink = hyperlink
|
|
26
38
|
end
|
|
27
39
|
end
|
|
28
40
|
end
|
|
@@ -13,9 +13,10 @@ module Rbxl
|
|
|
13
13
|
# sheet.append([1, "alice"])
|
|
14
14
|
# book.save("report.xlsx")
|
|
15
15
|
#
|
|
16
|
-
#
|
|
17
|
-
#
|
|
18
|
-
#
|
|
16
|
+
# Cell styles can be registered up-front via {#add_style}; the returned
|
|
17
|
+
# +style_id+ is then attached to {Rbxl::WriteOnlyCell} instances. Workbooks
|
|
18
|
+
# that never call +add_style+ emit the same minimal +xl/styles.xml+ as
|
|
19
|
+
# earlier releases, byte-for-byte.
|
|
19
20
|
class WriteOnlyWorkbook
|
|
20
21
|
# @return [Array<Rbxl::WriteOnlyWorksheet>] worksheets in insertion order
|
|
21
22
|
attr_reader :worksheets
|
|
@@ -25,6 +26,35 @@ module Rbxl
|
|
|
25
26
|
@worksheets = []
|
|
26
27
|
@closed = false
|
|
27
28
|
@saved = false
|
|
29
|
+
@style_registry = StyleRegistry.new
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Registers a cell style and returns its +style_id+ for use on
|
|
33
|
+
# {Rbxl::WriteOnlyCell} instances.
|
|
34
|
+
#
|
|
35
|
+
# header = book.add_style(
|
|
36
|
+
# font: { name: "Yu Gothic UI", size: 11, bold: true, color: "333333" },
|
|
37
|
+
# fill: { color: "EEEEEE" },
|
|
38
|
+
# )
|
|
39
|
+
# sheet << [Rbxl::WriteOnlyCell.new("Path", style_id: header)]
|
|
40
|
+
#
|
|
41
|
+
# Calls are not deduplicated by content: two equivalent registrations
|
|
42
|
+
# return distinct +style_id+ values. Callers wanting reuse should hold
|
|
43
|
+
# on to the integer themselves. Sub-tables (+fonts+, +fills+, +borders+,
|
|
44
|
+
# +numFmts+) are deduplicated internally to keep +styles.xml+ compact.
|
|
45
|
+
#
|
|
46
|
+
# @param font [Hash, nil] +name+, +size+, +bold+, +italic+, +underline+, +color+
|
|
47
|
+
# @param fill [Hash, nil] +color+ (RRGGBB hex) for solid fills, or +pattern_type+
|
|
48
|
+
# @param border [Hash, nil] +all+ for four-sided shorthand, or +top+/+right+/+bottom+/+left+
|
|
49
|
+
# @param alignment [Hash, nil] +horizontal+, +vertical+, +wrap_text+, +text_rotation+
|
|
50
|
+
# @param number_format [String, nil] Excel format code
|
|
51
|
+
# @return [Integer] 1-based style id
|
|
52
|
+
def add_style(font: nil, fill: nil, border: nil, alignment: nil, number_format: nil)
|
|
53
|
+
ensure_writable!
|
|
54
|
+
|
|
55
|
+
@style_registry.register(
|
|
56
|
+
Style.new(font: font, fill: fill, border: border, alignment: alignment, number_format: number_format)
|
|
57
|
+
)
|
|
28
58
|
end
|
|
29
59
|
|
|
30
60
|
# Creates and returns a new worksheet appended to this workbook.
|
|
@@ -67,7 +97,13 @@ module Rbxl
|
|
|
67
97
|
write_entry(zip, "xl/styles.xml", styles_xml)
|
|
68
98
|
|
|
69
99
|
worksheets.each_with_index do |sheet, index|
|
|
70
|
-
|
|
100
|
+
sheet_index = index + 1
|
|
101
|
+
write_entry(zip, "xl/worksheets/sheet#{sheet_index}.xml", sheet.to_xml)
|
|
102
|
+
|
|
103
|
+
rels = sheet.external_hyperlink_relationships
|
|
104
|
+
unless rels.empty?
|
|
105
|
+
write_entry(zip, "xl/worksheets/_rels/sheet#{sheet_index}.xml.rels", sheet_rels_xml(rels))
|
|
106
|
+
end
|
|
71
107
|
end
|
|
72
108
|
end
|
|
73
109
|
ensure
|
|
@@ -159,6 +195,8 @@ module Rbxl
|
|
|
159
195
|
end
|
|
160
196
|
|
|
161
197
|
def styles_xml
|
|
198
|
+
return @style_registry.to_xml unless @style_registry.empty?
|
|
199
|
+
|
|
162
200
|
<<~XML.chomp
|
|
163
201
|
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
|
|
164
202
|
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
|
|
@@ -187,5 +225,16 @@ module Rbxl
|
|
|
187
225
|
def escape(value)
|
|
188
226
|
CGI.escapeHTML(value.to_s)
|
|
189
227
|
end
|
|
228
|
+
|
|
229
|
+
def sheet_rels_xml(rels)
|
|
230
|
+
buf = +'<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'
|
|
231
|
+
buf << "\n"
|
|
232
|
+
buf << '<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">'
|
|
233
|
+
rels.each do |r|
|
|
234
|
+
buf << %(<Relationship Id="#{r[:rid]}" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink" Target="#{escape(r[:target])}" TargetMode="External"/>)
|
|
235
|
+
end
|
|
236
|
+
buf << '</Relationships>'
|
|
237
|
+
buf
|
|
238
|
+
end
|
|
190
239
|
end
|
|
191
240
|
end
|