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
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2cb085cc020f3040a8106a8ee707bd20287fecab3c3cbddad356cfb790771293
|
|
4
|
+
data.tar.gz: 9ec879dd0a9872ad8eaac8d29782f13501e175c29d4e8f5c384cd5068b6a228f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a712a64bb7495118457aa35ead5b9abe254fe961f3abd2eeb2b91cd448e8af6a8d8b038d3cdda569e7dc58c7d3d8115123c61971b93a18e27cf13e5b7ac11e18
|
|
7
|
+
data.tar.gz: ef38677ba807c8fecad4c1d55d8501ab8ac1378178df4cf037ee287b1a21b4ba5a530340723268e484b85ece3e9335b3af03d0ff0d2a08586a9ebb476353c62a
|
data/CHANGELOG.md
CHANGED
|
@@ -4,6 +4,87 @@ All notable changes to this project are documented here. The format is based
|
|
|
4
4
|
on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project
|
|
5
5
|
follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
6
6
|
|
|
7
|
+
## [1.6.0] - 2026-11-10
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `Rbxl::WriteOnlyWorkbook#add_style` registers a workbook-level cell style
|
|
12
|
+
(font, fill, border, alignment, number_format) and returns a `style_id`
|
|
13
|
+
that can be attached to `Rbxl::WriteOnlyCell` instances. Workbooks that
|
|
14
|
+
never call `add_style` emit the same minimal `xl/styles.xml` as before,
|
|
15
|
+
byte-for-byte.
|
|
16
|
+
- `Rbxl::WriteOnlyWorksheet#column_width` accepts an integer index or a
|
|
17
|
+
Range plus a width; consecutive same-width columns merge into one
|
|
18
|
+
`<col>` element on save.
|
|
19
|
+
- `Rbxl::WriteOnlyWorksheet#row_height` accepts an integer index or a
|
|
20
|
+
Range plus a height. Native fast path supports row heights via an
|
|
21
|
+
optional second argument to `Rbxl::Native.generate_sheet`, so heights
|
|
22
|
+
ride the C path instead of forcing a Ruby fallback.
|
|
23
|
+
- `Rbxl::WriteOnlyWorksheet#freeze!` emits a `<sheetView>` `<pane>` with
|
|
24
|
+
`topLeftCell`, `activePane`, and `state="frozen"` derived from `rows:`
|
|
25
|
+
and `cols:` keyword arguments.
|
|
26
|
+
- `Rbxl::WriteOnlyWorksheet#merge` records merge ranges (either as
|
|
27
|
+
`"A1:C1"` or as `"A1", "C1"`) and emits them in a `<mergeCells>` block
|
|
28
|
+
after `</sheetData>`.
|
|
29
|
+
- `Rbxl::WriteOnlyCell` now accepts a `hyperlink:` keyword. Strings
|
|
30
|
+
starting with `#` are emitted as internal sheet anchors via the
|
|
31
|
+
`<hyperlink location="..."/>` form; everything else becomes an
|
|
32
|
+
external URL with a per-sheet
|
|
33
|
+
`xl/worksheets/_rels/sheetN.xml.rels` entry referenced through
|
|
34
|
+
`r:id`. The `xmlns:r` namespace declaration is added to `<worksheet>`
|
|
35
|
+
only when external links exist.
|
|
36
|
+
- Edit mode (`Rbxl.open(path, edit: true)`) grows the pieces a template
|
|
37
|
+
fill-in needs:
|
|
38
|
+
- `EditableCell#value=` accepts `Date`, `Time`, and `DateTime`. Values
|
|
39
|
+
are written as Excel serials honoring the workbook's 1900/1904 date
|
|
40
|
+
system. A cell that already has a date format keeps it; otherwise a
|
|
41
|
+
copy of the cell's current style with the built-in short-date (14) or
|
|
42
|
+
date-time (22) format is derived, so template fonts and borders stay.
|
|
43
|
+
- `EditableCell#style(font:, fill:, border:, alignment:, number_format:)`
|
|
44
|
+
changes part of a cell's current style — font and alignment
|
|
45
|
+
attributes and border sides merge, fill and number format replace.
|
|
46
|
+
`EditableWorkbook#add_style` registers a complete style (same keywords
|
|
47
|
+
as write-only mode) for `EditableCell#style_id=`; `#style_id` reads
|
|
48
|
+
the current index. New entries are only appended to `xl/styles.xml`,
|
|
49
|
+
so existing style indices never change, and the part is rewritten
|
|
50
|
+
only when a style was added.
|
|
51
|
+
- `EditableWorksheet#write_rows(anchor, rows)` writes a 2D array into
|
|
52
|
+
consecutive cells. Given a range such as `"B10:D19"`, data that does
|
|
53
|
+
not fit raises before any cell is touched.
|
|
54
|
+
- `EditableWorkbook#defined_name(name, sheet: nil)` returns the cell a
|
|
55
|
+
single-cell defined name points to; `#defined_names` lists them.
|
|
56
|
+
Raises the new `Rbxl::DefinedNameError` for unknown, ambiguous, or
|
|
57
|
+
range names.
|
|
58
|
+
- `EditableWorkbook#add_sheet(name)` appends an empty worksheet (part,
|
|
59
|
+
workbook relationship, and content-type override).
|
|
60
|
+
- `EditableWorkbook#save(path = nil, recalc: true)`: when cells were
|
|
61
|
+
edited, `<calcPr fullCalcOnLoad="1"/>` is set so Excel and
|
|
62
|
+
LibreOffice recompute stale template formulas on open. If an edit
|
|
63
|
+
replaced a formula, `xl/calcChain.xml` is dropped so Excel rebuilds
|
|
64
|
+
it.
|
|
65
|
+
|
|
66
|
+
### Changed
|
|
67
|
+
|
|
68
|
+
- Style colors (`font:` / `fill:` / `border:` `color`) are written as
|
|
69
|
+
8-digit ARGB (`FFRRGGBB`), the form ECMA-376 specifies. A leading `#`
|
|
70
|
+
is accepted; 8-digit input passes through.
|
|
71
|
+
- Edit mode no longer raises `Rbxl::EditableCellTypeError` for
|
|
72
|
+
`Date`/`Time`/`DateTime` (see above). Editing a cell now also touches
|
|
73
|
+
`xl/workbook.xml` (the recalculation flag) unless `recalc: false`.
|
|
74
|
+
|
|
75
|
+
### Fixed
|
|
76
|
+
|
|
77
|
+
- Edit mode: saving to another path, editing more, and saving again no
|
|
78
|
+
longer drops the first round of edits.
|
|
79
|
+
- Edit mode: after an in-place `save`, opening a sheet that had not been
|
|
80
|
+
loaded yet no longer reads from the replaced file's stale ZIP
|
|
81
|
+
directory (it raised `WorksheetFormatError`).
|
|
82
|
+
- Edit mode: rows and cells inserted into a part that binds SpreadsheetML
|
|
83
|
+
under a prefix (e.g. `x:` from the .NET OpenXML SDK) now get that
|
|
84
|
+
namespace instead of none.
|
|
85
|
+
- Edit mode: re-serialized parts keep their original layout; libxml2 no
|
|
86
|
+
longer re-indents them.
|
|
87
|
+
|
|
7
88
|
## [1.5.0] - 2026-10-31
|
|
8
89
|
|
|
9
90
|
### Changed
|
data/README.md
CHANGED
|
@@ -17,11 +17,16 @@ native extension for faster XML parsing when you need more throughput.
|
|
|
17
17
|
|
|
18
18
|
Supported:
|
|
19
19
|
|
|
20
|
-
- write-only workbook generation
|
|
20
|
+
- write-only workbook generation with **registered cell styles** (font,
|
|
21
|
+
fill, border, alignment, number format), per-column widths, per-row
|
|
22
|
+
heights, freeze panes, merged cells, and hyperlinks
|
|
21
23
|
- read-only row-by-row iteration
|
|
22
24
|
- read-modify-save surgical edits via `Rbxl.open(path, edit: true)` —
|
|
23
|
-
byte-for-byte preservation of untouched parts (
|
|
24
|
-
comments, pivot caches, custom XML, untouched sheets)
|
|
25
|
+
byte-for-byte preservation of untouched parts (drawings, charts,
|
|
26
|
+
comments, pivot caches, custom XML, untouched sheets). Built for
|
|
27
|
+
template fill-in: dates, bulk `write_rows`, defined-name lookup,
|
|
28
|
+
partial restyling on top of the template's formatting, and appending
|
|
29
|
+
new sheets
|
|
25
30
|
- opt-in date/time conversion driven by the workbook's `numFmt` styles
|
|
26
31
|
- optional C extension (`rbxl/native`) for maximum performance
|
|
27
32
|
|
|
@@ -32,11 +37,13 @@ Out of scope:
|
|
|
32
37
|
template fill-ins (a handful of cells in a templated workbook); the
|
|
33
38
|
touched sheet is loaded into a Nokogiri DOM, so memory scales with that
|
|
34
39
|
sheet's on-disk size
|
|
35
|
-
- inserting / deleting
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
+
- inserting / deleting rows or columns in edit mode (shifting every
|
|
41
|
+
reference — formulas, merges, conditional formats, defined names,
|
|
42
|
+
charts — is a different class of tool)
|
|
43
|
+
- deleting / renaming / reordering / duplicating sheets in edit mode
|
|
44
|
+
(`add_sheet` appends a new empty sheet)
|
|
45
|
+
- editing formulas, defined names, drawings, or shared strings through
|
|
46
|
+
the edit mode path
|
|
40
47
|
- legacy `.xls` (BIFF/CFB) input — rbxl reads OOXML `.xlsx` only. Convert
|
|
41
48
|
first, e.g. `libreoffice --headless --convert-to xlsx file.xls` or
|
|
42
49
|
`ssconvert file.xls file.xlsx` (Gnumeric). `Rbxl.open` detects the OLE
|
|
@@ -45,7 +52,6 @@ Out of scope:
|
|
|
45
52
|
error from rubyzip.
|
|
46
53
|
- preserving arbitrary workbook structure on _write-only_ save (edit mode
|
|
47
54
|
preserves every untouched part)
|
|
48
|
-
- rich style round-tripping when generating new workbooks
|
|
49
55
|
- formulas, images, charts, comments — readable in edit mode, but not
|
|
50
56
|
introspected or edited
|
|
51
57
|
|
|
@@ -84,6 +90,72 @@ Write-only workbooks follow three rules:
|
|
|
84
90
|
This is the tradeoff that keeps memory flat: rbxl buffers rows per sheet
|
|
85
91
|
and never materializes a full workbook object graph.
|
|
86
92
|
|
|
93
|
+
#### Styled cells, column widths, row heights, freeze, merge, hyperlinks
|
|
94
|
+
|
|
95
|
+
Cell formatting and worksheet metadata are registered up-front so the
|
|
96
|
+
write path stays append-only. Styles are attached to individual cells
|
|
97
|
+
through `Rbxl::WriteOnlyCell`; widths, heights, freeze, and merges are
|
|
98
|
+
sheet-level calls that ride the same one-shot save:
|
|
99
|
+
|
|
100
|
+
```ruby
|
|
101
|
+
require "rbxl"
|
|
102
|
+
|
|
103
|
+
book = Rbxl.new
|
|
104
|
+
|
|
105
|
+
header = book.add_style(
|
|
106
|
+
font: { name: "Yu Gothic UI", size: 11, bold: true, color: "333333" },
|
|
107
|
+
fill: { color: "EEEEEE" },
|
|
108
|
+
alignment: { horizontal: "center" },
|
|
109
|
+
)
|
|
110
|
+
mono = book.add_style(font: { name: "Consolas", size: 11, color: "4A90D9" })
|
|
111
|
+
link_style = book.add_style(font: { color: "0563C1", underline: true })
|
|
112
|
+
|
|
113
|
+
sheet = book.add_sheet("Report")
|
|
114
|
+
sheet.column_width(0, 45.7)
|
|
115
|
+
sheet.column_width(1, 60)
|
|
116
|
+
sheet.row_height(0, 22)
|
|
117
|
+
sheet.freeze!(rows: 1)
|
|
118
|
+
sheet.merge("A1:B1")
|
|
119
|
+
|
|
120
|
+
sheet << [Rbxl::WriteOnlyCell.new("Path", style_id: header),
|
|
121
|
+
Rbxl::WriteOnlyCell.new("Description", style_id: header)]
|
|
122
|
+
sheet << [Rbxl::WriteOnlyCell.new("/bin/run.rb", style_id: mono),
|
|
123
|
+
"entry point"]
|
|
124
|
+
sheet << [Rbxl::WriteOnlyCell.new(
|
|
125
|
+
"see Detail",
|
|
126
|
+
style_id: link_style,
|
|
127
|
+
hyperlink: "#'Detail'!A1",
|
|
128
|
+
),
|
|
129
|
+
Rbxl::WriteOnlyCell.new(
|
|
130
|
+
"repo",
|
|
131
|
+
style_id: link_style,
|
|
132
|
+
hyperlink: "https://github.com/matzlika/rbxl",
|
|
133
|
+
)]
|
|
134
|
+
|
|
135
|
+
book.save("report.xlsx")
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
`add_style` accepts five keyword arguments — `font:`, `fill:`, `border:`,
|
|
139
|
+
`alignment:`, `number_format:` — each as a hash:
|
|
140
|
+
|
|
141
|
+
| Component | Keys |
|
|
142
|
+
|-------------------|-----------------------------------------------------------------|
|
|
143
|
+
| `font:` | `name`, `size`, `bold`, `italic`, `underline`, `color` (RRGGBB, written as ARGB `FFRRGGBB`) |
|
|
144
|
+
| `fill:` | `color` (solid pattern), or `pattern_type` |
|
|
145
|
+
| `border:` | `all: { style:, color: }`, or per-side `top:` / `right:` / `bottom:` / `left:` |
|
|
146
|
+
| `alignment:` | `horizontal`, `vertical`, `wrap_text`, `text_rotation` |
|
|
147
|
+
| `number_format:` | Excel format code string (e.g. `"#,##0"`, `"yyyy-mm-dd"`) |
|
|
148
|
+
|
|
149
|
+
Two equivalent registrations return distinct `style_id`s — internal
|
|
150
|
+
sub-tables (`fonts`, `fills`, `borders`, `numFmts`) are still
|
|
151
|
+
deduplicated by content so `xl/styles.xml` stays compact.
|
|
152
|
+
|
|
153
|
+
Hyperlink targets starting with `#` are emitted as internal sheet
|
|
154
|
+
anchors (no `_rels` entry needed); other strings become external URLs
|
|
155
|
+
with a per-sheet `xl/worksheets/_rels/sheetN.xml.rels` entry. Sheets
|
|
156
|
+
that don't use any of these features keep the existing native fast
|
|
157
|
+
path with byte-identical output.
|
|
158
|
+
|
|
87
159
|
### Reading a workbook
|
|
88
160
|
|
|
89
161
|
```ruby
|
|
@@ -250,29 +322,51 @@ worksheet parser.
|
|
|
250
322
|
|
|
251
323
|
### Editing an existing workbook
|
|
252
324
|
|
|
253
|
-
Open a workbook in edit mode to
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
PowerPoint-style add-ins survive the save.
|
|
325
|
+
Open a workbook in edit mode to fill in a template without rebuilding it
|
|
326
|
+
from scratch: open a formatted template, write the cells that change,
|
|
327
|
+
save. Every part you don't touch — drawings, charts, comments, pivot
|
|
328
|
+
caches, custom XML, untouched worksheets — round-trips byte-for-byte
|
|
329
|
+
straight from the source ZIP, so unknown OOXML extensions and add-ins
|
|
330
|
+
survive the save.
|
|
260
331
|
|
|
261
332
|
```ruby
|
|
262
333
|
require "rbxl"
|
|
263
334
|
|
|
264
|
-
Rbxl.open("template.xlsx", edit: true) do |book|
|
|
265
|
-
sheet = book.sheet("
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
sheet["
|
|
335
|
+
Rbxl.open("invoice-template.xlsx", edit: true) do |book|
|
|
336
|
+
sheet = book.sheet("請求書")
|
|
337
|
+
|
|
338
|
+
# Plain values keep the template cell's formatting.
|
|
339
|
+
sheet["B3"].value = "株式会社Acme 御中"
|
|
340
|
+
|
|
341
|
+
# Dates are written as Excel serials. A cell that already has a date
|
|
342
|
+
# format keeps it; otherwise the built-in short-date format is layered
|
|
343
|
+
# on top of the cell's current style (fonts / borders survive).
|
|
344
|
+
sheet["F2"].value = Date.today
|
|
345
|
+
|
|
346
|
+
# Line items: consecutive cells from a top-left anchor. With a range,
|
|
347
|
+
# data that doesn't fit the template's prepared rows raises up front.
|
|
348
|
+
sheet.write_rows("B10:D19", [
|
|
349
|
+
["作業費", 1, 50_000],
|
|
350
|
+
["交通費", 1, 3_200],
|
|
351
|
+
])
|
|
352
|
+
|
|
353
|
+
# Partial restyle: only what you pass changes.
|
|
354
|
+
sheet["F20"].style(font: { color: "C00000", bold: true })
|
|
355
|
+
|
|
356
|
+
# Fill by defined name ("名前付きセル") instead of coordinate.
|
|
357
|
+
book.defined_name("顧客名").value = "株式会社Acme"
|
|
358
|
+
|
|
359
|
+
# Append a new, empty sheet.
|
|
360
|
+
book.add_sheet("明細データ").write_rows("A1", [["品目", "金額"]])
|
|
361
|
+
|
|
269
362
|
book.save("invoice-acme.xlsx")
|
|
270
363
|
end
|
|
271
364
|
```
|
|
272
365
|
|
|
273
366
|
`book.save` with no argument overwrites the original file via temp file
|
|
274
367
|
plus atomic rename, so a crash mid-save never produces a half-written
|
|
275
|
-
workbook
|
|
368
|
+
workbook. The workbook stays usable after `save`; later edits and saves
|
|
369
|
+
include everything edited so far.
|
|
276
370
|
|
|
277
371
|
```ruby
|
|
278
372
|
Rbxl.open("template.xlsx", edit: true) do |book|
|
|
@@ -284,20 +378,27 @@ end
|
|
|
284
378
|
Inside an edited worksheet, only the targeted `<c>` element is rewritten;
|
|
285
379
|
sibling cells, the row's other attributes, `<mergeCells>`,
|
|
286
380
|
`<conditionalFormatting>`, `<dataValidations>`, and any unknown OOXML
|
|
287
|
-
extensions remain in place
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
their enclosing rows) are inserted in column- and
|
|
381
|
+
extensions remain in place, and the part keeps its original layout (no
|
|
382
|
+
re-indentation). The cell's `s` (style index) attribute is preserved
|
|
383
|
+
when you overwrite an existing cell, so template formatting carries
|
|
384
|
+
through. New cells (and their enclosing rows) are inserted in column- and
|
|
385
|
+
row-sorted positions, in whatever namespace prefix the part uses.
|
|
291
386
|
|
|
292
387
|
`EditableCell#value=` accepts:
|
|
293
388
|
|
|
294
|
-
| Ruby
|
|
295
|
-
|
|
296
|
-
| `nil`
|
|
297
|
-
| `String`
|
|
298
|
-
| `Integer`, `Float`
|
|
299
|
-
| `true`, `false`
|
|
300
|
-
| `Date
|
|
389
|
+
| Ruby | XLSX representation |
|
|
390
|
+
|------------------------------|----------------------------------|
|
|
391
|
+
| `nil` | empty cell (preserves `s` style) |
|
|
392
|
+
| `String` | `t="inlineStr"` with `<is><t/>` |
|
|
393
|
+
| `Integer`, `Float`, `Numeric`| `<v>` numeric (no `t` attribute) |
|
|
394
|
+
| `true`, `false` | `t="b"` with `<v>1</v>`/`<v>0</v>` |
|
|
395
|
+
| `Date` | serial; built-in short date (14) unless the cell already has a date format |
|
|
396
|
+
| `Time`, `DateTime` | fractional serial; built-in date-time (22) unless the cell already has a date format |
|
|
397
|
+
|
|
398
|
+
Dates honor the workbook's 1900 / 1904 date system, and the wall-clock
|
|
399
|
+
fields of a `Time` are written as-is (Excel has no time zones). Reading a
|
|
400
|
+
date cell back through `EditableCell#value` returns the serial number;
|
|
401
|
+
use `Rbxl.open(path, date_conversion: true)` for `Date`/`Time` objects.
|
|
301
402
|
|
|
302
403
|
Strings always round-trip as inline strings — `xl/sharedStrings.xml` is
|
|
303
404
|
never mutated, so the SST entries that the cells you _didn't_ touch still
|
|
@@ -307,20 +408,72 @@ cell leaves an orphaned SST entry; for template fill-ins that's
|
|
|
307
408
|
negligible, and it's the simplest design that guarantees deterministic
|
|
308
409
|
output.
|
|
309
410
|
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
411
|
+
#### Styling in edit mode
|
|
412
|
+
|
|
413
|
+
```ruby
|
|
414
|
+
# Change part of the template's look; everything else is kept.
|
|
415
|
+
cell.style(font: { color: "C00000" }) # same face/size, now red
|
|
416
|
+
cell.style(fill: { color: "FFF2CC" }) # highlight
|
|
417
|
+
cell.style(border: { bottom: { style: "double" } })
|
|
418
|
+
cell.style(number_format: "#,##0")
|
|
419
|
+
|
|
420
|
+
# Or register a complete style and assign it.
|
|
421
|
+
header = book.add_style(font: { bold: true }, fill: { color: "DDEBF7" })
|
|
422
|
+
cell.style_id = header
|
|
423
|
+
cell.style_id # => current cellXfs index
|
|
424
|
+
```
|
|
425
|
+
|
|
426
|
+
`style` merges: `font:` attributes and `alignment:` attributes override
|
|
427
|
+
individually (`bold: false` turns bold off), `border:` overrides only
|
|
428
|
+
the sides you pass (`bottom: nil` removes that line), `fill:` and
|
|
429
|
+
`number_format:` replace. Cells sharing a style that receive the same
|
|
430
|
+
change share one derived style. `add_style` takes the same keywords as
|
|
431
|
+
in write-only mode.
|
|
432
|
+
|
|
433
|
+
Both only **append** to `xl/styles.xml` — existing `cellXfs` / `fonts` /
|
|
434
|
+
`fills` / `borders` / `numFmts` indices never move, so cells you don't
|
|
435
|
+
touch look exactly as before. `styles.xml` is rewritten only when a style
|
|
436
|
+
was actually added; otherwise it is copied byte-for-byte.
|
|
437
|
+
|
|
438
|
+
#### Defined names
|
|
439
|
+
|
|
440
|
+
```ruby
|
|
441
|
+
book.defined_names # => ["顧客名", "発行日", ...]
|
|
442
|
+
book.defined_name("顧客名").value = "Acme"
|
|
443
|
+
book.defined_name("小計", sheet: "請求書") # sheet-scoped name
|
|
444
|
+
```
|
|
445
|
+
|
|
446
|
+
Names match case-insensitively. A workbook-scoped name wins over
|
|
447
|
+
sheet-scoped ones; pass `sheet:` to pick a sheet-scoped name.
|
|
448
|
+
`Rbxl::DefinedNameError` is raised for unknown or ambiguous names and
|
|
449
|
+
for names that refer to a range rather than a single cell.
|
|
450
|
+
|
|
451
|
+
#### Recalculation
|
|
452
|
+
|
|
453
|
+
A template's cached formula results (say, `=SUM(D10:D19)`) are stale once
|
|
454
|
+
you write the inputs. When any cell was edited, `save` sets
|
|
455
|
+
`<calcPr fullCalcOnLoad="1"/>` in `xl/workbook.xml`, so Excel and
|
|
456
|
+
LibreOffice recompute every formula on open. Pass `save(path, recalc:
|
|
457
|
+
false)` to leave `workbook.xml` alone. If an edit replaced a formula with
|
|
458
|
+
a value, `xl/calcChain.xml` is dropped (Excel rebuilds it) so it never
|
|
459
|
+
points at a non-formula cell.
|
|
460
|
+
|
|
461
|
+
#### Adding sheets
|
|
462
|
+
|
|
463
|
+
`book.add_sheet(name)` appends an empty worksheet and returns it for
|
|
464
|
+
editing. Names follow Excel's rules (1–31 characters, none of
|
|
465
|
+
`[ ] : * ? / \`, no leading/trailing `'`, unique case-insensitively).
|
|
466
|
+
`docProps/app.xml` keeps listing the original sheet titles; Excel and
|
|
467
|
+
LibreOffice don't use it when opening.
|
|
316
468
|
|
|
317
469
|
#### Out of scope for edit mode
|
|
318
470
|
|
|
319
|
-
- inserting / deleting
|
|
320
|
-
|
|
471
|
+
- inserting / deleting rows or columns — `write_rows` never inserts rows;
|
|
472
|
+
prepare enough rows in the template
|
|
473
|
+
- deleting / renaming / reordering / duplicating sheets
|
|
474
|
+
- editing formulas, defined names, drawings, or shared strings
|
|
321
475
|
- recomputing the worksheet `<dimension>` when a write expands the bounds
|
|
322
|
-
(Excel recomputes on open
|
|
323
|
-
a later release)
|
|
476
|
+
(Excel recomputes on open)
|
|
324
477
|
- bulk rewrites of a worksheet's data area — touched sheets are loaded
|
|
325
478
|
into a Nokogiri DOM, so memory scales with that sheet's size on disk.
|
|
326
479
|
For data-area rewrites, use `Rbxl.new` instead.
|
data/ext/rbxl_native/native.c
CHANGED
|
@@ -720,14 +720,23 @@ static void write_cell(dynbuf *buf, int col, int row, VALUE value, VALUE cWriteO
|
|
|
720
720
|
}
|
|
721
721
|
|
|
722
722
|
/*
|
|
723
|
-
* Rbxl::Native.generate_sheet(rows) → XML string
|
|
723
|
+
* Rbxl::Native.generate_sheet(rows, row_heights = nil) → XML string
|
|
724
724
|
*
|
|
725
725
|
* rows: Array of Arrays, each inner array is a row of cell values
|
|
726
|
+
* row_heights: optional Hash<Integer, String> mapping 0-based row index to a
|
|
727
|
+
* pre-formatted height string. When present, the corresponding
|
|
728
|
+
* <row> element gets ht="..." customHeight="1" attributes.
|
|
726
729
|
*/
|
|
727
|
-
static VALUE rb_native_generate(VALUE
|
|
730
|
+
static VALUE rb_native_generate(int argc, VALUE *argv, VALUE self)
|
|
728
731
|
{
|
|
729
732
|
(void)self;
|
|
733
|
+
VALUE rows, row_heights;
|
|
734
|
+
rb_scan_args(argc, argv, "11", &rows, &row_heights);
|
|
735
|
+
|
|
730
736
|
Check_Type(rows, T_ARRAY);
|
|
737
|
+
if (!NIL_P(row_heights)) {
|
|
738
|
+
Check_Type(row_heights, T_HASH);
|
|
739
|
+
}
|
|
731
740
|
|
|
732
741
|
VALUE mRbxl = rb_const_get(rb_cObject, rb_intern("Rbxl"));
|
|
733
742
|
VALUE cWriteOnlyCell = rb_const_get(mRbxl, rb_intern("WriteOnlyCell"));
|
|
@@ -762,7 +771,19 @@ static VALUE rb_native_generate(VALUE self, VALUE rows)
|
|
|
762
771
|
|
|
763
772
|
W("<row r=\"");
|
|
764
773
|
write_int(&buf, row_num);
|
|
765
|
-
W("\"
|
|
774
|
+
W("\"");
|
|
775
|
+
|
|
776
|
+
if (!NIL_P(row_heights)) {
|
|
777
|
+
VALUE height_str = rb_hash_lookup(row_heights, LONG2FIX(i));
|
|
778
|
+
if (!NIL_P(height_str)) {
|
|
779
|
+
Check_Type(height_str, T_STRING);
|
|
780
|
+
W(" ht=\"");
|
|
781
|
+
dynbuf_append(&buf, RSTRING_PTR(height_str), RSTRING_LEN(height_str));
|
|
782
|
+
W("\" customHeight=\"1\"");
|
|
783
|
+
}
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
W(">");
|
|
766
787
|
|
|
767
788
|
for (long j = 0; j < ncols; j++) {
|
|
768
789
|
write_cell(&buf, (int)(j + 1), (int)row_num, rb_ary_entry(row, j), cWriteOnlyCell);
|
|
@@ -793,5 +814,5 @@ void Init_rbxl_native(void)
|
|
|
793
814
|
rb_define_module_function(mNative, "parse_sheet_full", rb_native_parse_full, 2);
|
|
794
815
|
rb_define_module_function(mNative, "parse_sheet_io", rb_native_parse_io, 3);
|
|
795
816
|
rb_define_module_function(mNative, "parse_sheet_full_io", rb_native_parse_full_io, 3);
|
|
796
|
-
rb_define_module_function(mNative, "generate_sheet", rb_native_generate, 1);
|
|
817
|
+
rb_define_module_function(mNative, "generate_sheet", rb_native_generate, -1);
|
|
797
818
|
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
module Rbxl
|
|
2
|
+
# @api private
|
|
3
|
+
#
|
|
4
|
+
# Date/time number-format detection and Excel serial conversion, shared by
|
|
5
|
+
# the read-only date conversion path and edit-mode date writes.
|
|
6
|
+
module DateFormat
|
|
7
|
+
# Built-in numFmtId values that Excel resolves to date/time formats.
|
|
8
|
+
# Ids outside this set are dates only when the workbook provides a
|
|
9
|
+
# matching custom +<numFmt>+ entry whose format code contains date
|
|
10
|
+
# tokens. See ECMA-376 part 1 §18.8.30.
|
|
11
|
+
BUILTIN_IDS = Set.new([14, 15, 16, 17, 18, 19, 20, 21, 22,
|
|
12
|
+
27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
|
|
13
|
+
45, 46, 47, 50, 51, 52, 53, 54, 55, 56,
|
|
14
|
+
57, 58]).freeze
|
|
15
|
+
|
|
16
|
+
# Built-in short date (+m/d/yyyy+, rendered per the viewer's locale).
|
|
17
|
+
BUILTIN_DATE_ID = 14
|
|
18
|
+
|
|
19
|
+
# Built-in date + time (+m/d/yyyy h:mm+, rendered per the viewer's locale).
|
|
20
|
+
BUILTIN_DATETIME_ID = 22
|
|
21
|
+
|
|
22
|
+
EPOCH_1900 = Date.new(1899, 12, 31)
|
|
23
|
+
EPOCH_1904 = Date.new(1904, 1, 1)
|
|
24
|
+
private_constant :EPOCH_1900, :EPOCH_1904
|
|
25
|
+
|
|
26
|
+
module_function
|
|
27
|
+
|
|
28
|
+
# Quoted literals, bracketed directives (e.g. [Red], [$-409]), and
|
|
29
|
+
# backslash-escaped characters never introduce date tokens, so strip
|
|
30
|
+
# them before looking for +y/m/d/h/s+.
|
|
31
|
+
def date_format_code?(code)
|
|
32
|
+
stripped = code.dup
|
|
33
|
+
stripped.gsub!(/\[[^\]]*\]/, "")
|
|
34
|
+
stripped.gsub!(/"[^"]*"/, "")
|
|
35
|
+
stripped.gsub!(/\\./, "")
|
|
36
|
+
stripped.match?(/[ymdhs]/i)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Converts a Ruby date/time to an Excel serial number. Time zones are
|
|
40
|
+
# ignored — Excel has no notion of one, so the wall-clock fields of the
|
|
41
|
+
# value are written as-is.
|
|
42
|
+
#
|
|
43
|
+
# The 1900 system reproduces Excel's phantom 1900-02-29 (serial 60), so
|
|
44
|
+
# dates on or after 1900-03-01 get serials matching what Excel shows.
|
|
45
|
+
#
|
|
46
|
+
# @param value [Date, DateTime, Time]
|
|
47
|
+
# @param date_1904 [Boolean] whether the workbook uses the 1904 system
|
|
48
|
+
# @return [Integer, Float] whole serial for midnight, fractional otherwise
|
|
49
|
+
# @raise [ArgumentError] for dates Excel cannot represent
|
|
50
|
+
def to_serial(value, date_1904: false)
|
|
51
|
+
date, seconds =
|
|
52
|
+
case value
|
|
53
|
+
when DateTime
|
|
54
|
+
[value.to_date, (value.hour * 3600) + (value.min * 60) + value.sec + value.sec_fraction]
|
|
55
|
+
when Time
|
|
56
|
+
[value.to_date, (value.hour * 3600) + (value.min * 60) + value.sec + value.subsec]
|
|
57
|
+
when Date
|
|
58
|
+
[value, 0]
|
|
59
|
+
else
|
|
60
|
+
raise ArgumentError, "expected Date, DateTime, or Time, got #{value.class}"
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
days =
|
|
64
|
+
if date_1904
|
|
65
|
+
(date - EPOCH_1904).to_i
|
|
66
|
+
else
|
|
67
|
+
d = (date - EPOCH_1900).to_i
|
|
68
|
+
d >= 60 ? d + 1 : d
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
min_days = date_1904 ? 0 : 1
|
|
72
|
+
if days < min_days
|
|
73
|
+
raise ArgumentError,
|
|
74
|
+
"#{value.inspect} is before the workbook's date epoch (#{date_1904 ? '1904-01-01' : '1900-01-01'})"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
return days if seconds.zero?
|
|
78
|
+
|
|
79
|
+
days + (seconds.to_r / 86_400).to_f
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|