ironcalc 0.7.1.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 +7 -0
- data/CHANGELOG.md +6 -0
- data/Cargo.lock +1239 -0
- data/Cargo.toml +9 -0
- data/LICENSE-Apache-2.0.md +191 -0
- data/LICENSE-MIT.md +9 -0
- data/README.md +124 -0
- data/ext/ironcalc/Cargo.toml +18 -0
- data/ext/ironcalc/extconf.rb +4 -0
- data/ext/ironcalc/src/error.rs +17 -0
- data/ext/ironcalc/src/lib.rs +270 -0
- data/ext/ironcalc/src/model.rs +330 -0
- data/ext/ironcalc/src/user_model.rs +365 -0
- data/lib/ironcalc/model.rb +81 -0
- data/lib/ironcalc/native_methods.rb +501 -0
- data/lib/ironcalc/version.rb +3 -0
- data/lib/ironcalc.rb +11 -0
- metadata +74 -0
data/Cargo.toml
ADDED
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Jaap van der Plas
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
|
191
|
+
|
data/LICENSE-MIT.md
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Jaap van der Plas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# IronCalc Ruby
|
|
2
|
+
|
|
3
|
+
Ruby bindings for [IronCalc](https://www.ironcalc.com/), a modern spreadsheet
|
|
4
|
+
engine written in Rust. Create, read and manipulate xlsx files — manage sheets,
|
|
5
|
+
set and read cell values, and evaluate formulas.
|
|
6
|
+
|
|
7
|
+
Built with [magnus](https://github.com/matsadler/magnus) /
|
|
8
|
+
[rb-sys](https://github.com/oxidize-rb/rb-sys), and modeled on the
|
|
9
|
+
[IronCalc Python bindings](https://github.com/ironcalc/ironcalc/tree/main/bindings/python).
|
|
10
|
+
|
|
11
|
+
## Installation
|
|
12
|
+
|
|
13
|
+
Add this line to your application's Gemfile:
|
|
14
|
+
|
|
15
|
+
```ruby
|
|
16
|
+
gem "ironcalc"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
A Rust toolchain is required to build from source.
|
|
20
|
+
|
|
21
|
+
## Usage
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
require "ironcalc"
|
|
25
|
+
|
|
26
|
+
# Raw API — call evaluate yourself.
|
|
27
|
+
model = IronCalc.create("model", "en", "UTC", "en")
|
|
28
|
+
model.set_user_input(0, 1, 1, "=21*2")
|
|
29
|
+
model.evaluate
|
|
30
|
+
model.get_formatted_cell_value(0, 1, 1) # => "42"
|
|
31
|
+
model.save_to_xlsx("out.xlsx")
|
|
32
|
+
|
|
33
|
+
# User API — auto-evaluates and tracks diffs for collaboration.
|
|
34
|
+
um = IronCalc.create_user_model("model", "en", "UTC", "en")
|
|
35
|
+
um.set_user_input(0, 1, 1, "=1+2")
|
|
36
|
+
um.get_formatted_cell_value(0, 1, 1) # => "3"
|
|
37
|
+
diffs = um.flush_send_queue # binary diff to send to peers
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Coordinates: `sheet` is a 0-based index; `row` and `column` are 1-based. Styles
|
|
41
|
+
are exchanged as plain Ruby hashes via `get_cell_style` / `set_cell_style`.
|
|
42
|
+
|
|
43
|
+
### Top-level methods
|
|
44
|
+
|
|
45
|
+
`create`, `load_from_xlsx`, `load_from_icalc`, `load_from_bytes`,
|
|
46
|
+
`create_user_model`, `create_user_model_from_xlsx`,
|
|
47
|
+
`create_user_model_from_icalc`, `create_user_model_from_bytes`.
|
|
48
|
+
|
|
49
|
+
Errors raised by the engine surface as `IronCalc::Error`.
|
|
50
|
+
|
|
51
|
+
## Relationship to the Python bindings
|
|
52
|
+
|
|
53
|
+
`ironcalc-ruby` is a thin binding over the same Rust engine as the
|
|
54
|
+
[IronCalc Python bindings](https://github.com/ironcalc/ironcalc/tree/main/bindings/python),
|
|
55
|
+
and deliberately mirrors their API. Both are compiled native extensions (Python
|
|
56
|
+
via **pyo3**, Ruby via **magnus** / **rb-sys**) exposing a module named
|
|
57
|
+
`ironcalc` backed by the IronCalc engine.
|
|
58
|
+
|
|
59
|
+
### What's identical
|
|
60
|
+
|
|
61
|
+
- **Two APIs**: a raw `Model` (you call `evaluate` yourself) and a higher-level
|
|
62
|
+
`UserModel` (auto-evaluates and tracks diffs).
|
|
63
|
+
- **Top-level constructors**, same names and argument order
|
|
64
|
+
`(name_or_path, locale, tz, language_id)`: `create`, `load_from_xlsx`,
|
|
65
|
+
`load_from_icalc`, `load_from_bytes`, `create_user_model`,
|
|
66
|
+
`create_user_model_from_xlsx`, `create_user_model_from_icalc`,
|
|
67
|
+
`create_user_model_from_bytes`.
|
|
68
|
+
- **Method names and signatures** on `Model` / `UserModel` — `set_user_input`,
|
|
69
|
+
`get_formatted_cell_value`, `evaluate`, `insert_rows`, `set_column_width`,
|
|
70
|
+
`add_sheet`, `save_to_xlsx`, `to_bytes`, … (Python's `snake_case` is also
|
|
71
|
+
Ruby's convention, so they match exactly).
|
|
72
|
+
- **Coordinates**: `sheet` is a 0-based index; `row` and `column` are 1-based.
|
|
73
|
+
- **Semantics**: the same engine, so the same inputs produce the same results.
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
# Python
|
|
77
|
+
import ironcalc as ic
|
|
78
|
+
model = ic.create("model", "en", "UTC", "en")
|
|
79
|
+
model.set_user_input(0, 1, 1, "=21*2")
|
|
80
|
+
model.evaluate()
|
|
81
|
+
model.get_formatted_cell_value(0, 1, 1) # "42"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
```ruby
|
|
85
|
+
# Ruby
|
|
86
|
+
require "ironcalc"
|
|
87
|
+
model = IronCalc.create("model", "en", "UTC", "en")
|
|
88
|
+
model.set_user_input(0, 1, 1, "=21*2")
|
|
89
|
+
model.evaluate
|
|
90
|
+
model.get_formatted_cell_value(0, 1, 1) # "42"
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Idiomatic Ruby adaptations
|
|
94
|
+
|
|
95
|
+
A few return and argument types follow Ruby conventions instead of being literal
|
|
96
|
+
ports:
|
|
97
|
+
|
|
98
|
+
| Concern | Python | Ruby |
|
|
99
|
+
|---|---|---|
|
|
100
|
+
| Module access | `ic.create(...)` | `IronCalc.create(...)` |
|
|
101
|
+
| Error type | `WorkbookError` | `IronCalc::Error` |
|
|
102
|
+
| Cell style | typed `Style` / `Font` / … objects | plain `Hash` (`get_cell_style` / `set_cell_style`) |
|
|
103
|
+
| Cell type | `CellType` enum | `Symbol` (`:number`, `:text`, …) |
|
|
104
|
+
| Worksheet properties | list of `SheetProperty` objects | array of `Hash`es (`:name`, `:state`, `:sheet_id`, `:color`) |
|
|
105
|
+
| Sheet dimensions | tuple `(min_row, max_row, min_col, max_col)` | 4-element `Array` |
|
|
106
|
+
| Binary blobs | `bytes` | binary `String` |
|
|
107
|
+
| Version | `ironcalc.__version__` | `IronCalc::VERSION` |
|
|
108
|
+
|
|
109
|
+
Rather than reconstruct Python's per-field style classes, Ruby exchanges styles
|
|
110
|
+
as plain hashes (serialized as JSON across the boundary). Everything else is kept
|
|
111
|
+
as close to the Python bindings as the two languages allow.
|
|
112
|
+
|
|
113
|
+
## Development
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
bundle install
|
|
117
|
+
bundle exec rake compile
|
|
118
|
+
bundle exec rake test
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
Dual-licensed under [MIT](LICENSE-MIT.md) or [Apache-2.0](LICENSE-Apache-2.0.md),
|
|
124
|
+
matching IronCalc.
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[package]
|
|
2
|
+
name = "ironcalc_ruby"
|
|
3
|
+
version = "0.7.1"
|
|
4
|
+
edition = "2021"
|
|
5
|
+
publish = false
|
|
6
|
+
|
|
7
|
+
[lib]
|
|
8
|
+
name = "ironcalc_ruby"
|
|
9
|
+
crate-type = ["cdylib"]
|
|
10
|
+
|
|
11
|
+
[dependencies]
|
|
12
|
+
magnus = "0.8"
|
|
13
|
+
serde = { version = "1.0", features = ["derive"] }
|
|
14
|
+
serde_json = "1.0"
|
|
15
|
+
bitcode = "0.6.8"
|
|
16
|
+
# The IronCalc engine, published on crates.io as `ironcalc`. Renamed to `xlsx`
|
|
17
|
+
# here so the source can refer to `xlsx::base`, `xlsx::import`, `xlsx::export`.
|
|
18
|
+
xlsx = { package = "ironcalc", version = "0.7.1" }
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
use magnus::value::Lazy;
|
|
2
|
+
use magnus::{Error, ExceptionClass, Module, Ruby};
|
|
3
|
+
|
|
4
|
+
/// `IronCalc::Error` — the single exception class raised by this binding,
|
|
5
|
+
/// mirroring the Python binding's `WorkbookError`.
|
|
6
|
+
pub static IRONCALC_ERROR: Lazy<ExceptionClass> = Lazy::new(|ruby| {
|
|
7
|
+
let module = ruby.define_module("IronCalc").unwrap();
|
|
8
|
+
module
|
|
9
|
+
.define_error("Error", ruby.exception_standard_error())
|
|
10
|
+
.unwrap()
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/// Converts any engine error into an `IronCalc::Error` Ruby exception.
|
|
14
|
+
pub fn workbook_error<E: ToString>(e: E) -> Error {
|
|
15
|
+
let ruby = Ruby::get().expect("called outside of a Ruby thread");
|
|
16
|
+
Error::new(ruby.get_inner(&IRONCALC_ERROR), e.to_string())
|
|
17
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
use magnus::value::Lazy;
|
|
2
|
+
use magnus::{function, method, prelude::*, Error, RString, Ruby};
|
|
3
|
+
|
|
4
|
+
use xlsx::base::types::Workbook;
|
|
5
|
+
use xlsx::base::{Model as CoreModel, UserModel as CoreUserModel};
|
|
6
|
+
use xlsx::import;
|
|
7
|
+
|
|
8
|
+
mod error;
|
|
9
|
+
mod model;
|
|
10
|
+
mod user_model;
|
|
11
|
+
|
|
12
|
+
use error::{workbook_error, IRONCALC_ERROR};
|
|
13
|
+
use model::Model;
|
|
14
|
+
use user_model::UserModel;
|
|
15
|
+
|
|
16
|
+
fn leak_str(s: &str) -> &'static str {
|
|
17
|
+
Box::leak(s.to_owned().into_boxed_str())
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// Top-level constructors --------------------------------------------------
|
|
21
|
+
|
|
22
|
+
/// Creates an empty model using the raw API.
|
|
23
|
+
fn create(name: String, locale: String, tz: String, language_id: String) -> Result<Model, Error> {
|
|
24
|
+
let model = CoreModel::new_empty(
|
|
25
|
+
leak_str(&name),
|
|
26
|
+
leak_str(&locale),
|
|
27
|
+
leak_str(&tz),
|
|
28
|
+
leak_str(&language_id),
|
|
29
|
+
)
|
|
30
|
+
.map_err(workbook_error)?;
|
|
31
|
+
Ok(Model::new(model))
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/// Loads a model from an xlsx file.
|
|
35
|
+
fn load_from_xlsx(
|
|
36
|
+
file_path: String,
|
|
37
|
+
locale: String,
|
|
38
|
+
tz: String,
|
|
39
|
+
language_id: String,
|
|
40
|
+
) -> Result<Model, Error> {
|
|
41
|
+
let model = import::load_from_xlsx(&file_path, &locale, &tz, leak_str(&language_id))
|
|
42
|
+
.map_err(workbook_error)?;
|
|
43
|
+
Ok(Model::new(model))
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/// Loads a model from the internal binary icalc format.
|
|
47
|
+
fn load_from_icalc(file_name: String, language_id: String) -> Result<Model, Error> {
|
|
48
|
+
let model =
|
|
49
|
+
import::load_from_icalc(&file_name, leak_str(&language_id)).map_err(workbook_error)?;
|
|
50
|
+
Ok(Model::new(model))
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/// Loads a model from icalc bytes (same format as `save_to_icalc`).
|
|
54
|
+
fn load_from_bytes(bytes: RString, language_id: String) -> Result<Model, Error> {
|
|
55
|
+
let raw = unsafe { bytes.as_slice() }.to_vec();
|
|
56
|
+
let workbook: Workbook = bitcode::decode(&raw).map_err(workbook_error)?;
|
|
57
|
+
let model = CoreModel::from_workbook(workbook, leak_str(&language_id)).map_err(workbook_error)?;
|
|
58
|
+
Ok(Model::new(model))
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/// Creates an empty model using the user-model API.
|
|
62
|
+
fn create_user_model(
|
|
63
|
+
name: String,
|
|
64
|
+
locale: String,
|
|
65
|
+
tz: String,
|
|
66
|
+
language_id: String,
|
|
67
|
+
) -> Result<UserModel, Error> {
|
|
68
|
+
let model = CoreUserModel::new_empty(
|
|
69
|
+
leak_str(&name),
|
|
70
|
+
leak_str(&locale),
|
|
71
|
+
leak_str(&tz),
|
|
72
|
+
leak_str(&language_id),
|
|
73
|
+
)
|
|
74
|
+
.map_err(workbook_error)?;
|
|
75
|
+
Ok(UserModel::new(model))
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/// Creates a user model from an xlsx file.
|
|
79
|
+
fn create_user_model_from_xlsx(
|
|
80
|
+
file_path: String,
|
|
81
|
+
locale: String,
|
|
82
|
+
tz: String,
|
|
83
|
+
language_id: String,
|
|
84
|
+
) -> Result<UserModel, Error> {
|
|
85
|
+
let model = import::load_from_xlsx(&file_path, &locale, &tz, leak_str(&language_id))
|
|
86
|
+
.map_err(workbook_error)?;
|
|
87
|
+
Ok(UserModel::new(CoreUserModel::from_model(model)))
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/// Creates a user model from an icalc file.
|
|
91
|
+
fn create_user_model_from_icalc(
|
|
92
|
+
file_name: String,
|
|
93
|
+
language_id: String,
|
|
94
|
+
) -> Result<UserModel, Error> {
|
|
95
|
+
let model =
|
|
96
|
+
import::load_from_icalc(&file_name, leak_str(&language_id)).map_err(workbook_error)?;
|
|
97
|
+
Ok(UserModel::new(CoreUserModel::from_model(model)))
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/// Creates a user model from icalc bytes (same format as `save_to_icalc`).
|
|
101
|
+
fn create_user_model_from_bytes(bytes: RString, language_id: String) -> Result<UserModel, Error> {
|
|
102
|
+
let raw = unsafe { bytes.as_slice() }.to_vec();
|
|
103
|
+
let workbook: Workbook = bitcode::decode(&raw).map_err(workbook_error)?;
|
|
104
|
+
let model = CoreModel::from_workbook(workbook, leak_str(&language_id)).map_err(workbook_error)?;
|
|
105
|
+
Ok(UserModel::new(CoreUserModel::from_model(model)))
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
#[allow(clippy::panic)]
|
|
109
|
+
fn test_panic() {
|
|
110
|
+
panic!("This function panics for testing panic handling");
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
#[magnus::init]
|
|
114
|
+
fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
115
|
+
let module = ruby.define_module("IronCalc")?;
|
|
116
|
+
|
|
117
|
+
// Ensure the IronCalc::Error exception class exists.
|
|
118
|
+
Lazy::force(&IRONCALC_ERROR, ruby);
|
|
119
|
+
|
|
120
|
+
// Top-level constructors.
|
|
121
|
+
module.define_module_function("create", function!(create, 4))?;
|
|
122
|
+
module.define_module_function("load_from_xlsx", function!(load_from_xlsx, 4))?;
|
|
123
|
+
module.define_module_function("load_from_icalc", function!(load_from_icalc, 2))?;
|
|
124
|
+
module.define_module_function("load_from_bytes", function!(load_from_bytes, 2))?;
|
|
125
|
+
module.define_module_function("create_user_model", function!(create_user_model, 4))?;
|
|
126
|
+
module.define_module_function(
|
|
127
|
+
"create_user_model_from_xlsx",
|
|
128
|
+
function!(create_user_model_from_xlsx, 4),
|
|
129
|
+
)?;
|
|
130
|
+
module.define_module_function(
|
|
131
|
+
"create_user_model_from_icalc",
|
|
132
|
+
function!(create_user_model_from_icalc, 2),
|
|
133
|
+
)?;
|
|
134
|
+
module.define_module_function(
|
|
135
|
+
"create_user_model_from_bytes",
|
|
136
|
+
function!(create_user_model_from_bytes, 2),
|
|
137
|
+
)?;
|
|
138
|
+
module.define_module_function("test_panic", function!(test_panic, 0))?;
|
|
139
|
+
|
|
140
|
+
// Raw API: IronCalc::Model
|
|
141
|
+
let model_class = module.define_class("Model", ruby.class_object())?;
|
|
142
|
+
model_class.define_method("save_to_xlsx", method!(Model::save_to_xlsx, 1))?;
|
|
143
|
+
model_class.define_method("save_to_icalc", method!(Model::save_to_icalc, 1))?;
|
|
144
|
+
model_class.define_method("to_bytes", method!(Model::to_bytes, 0))?;
|
|
145
|
+
model_class.define_method("evaluate", method!(Model::evaluate, 0))?;
|
|
146
|
+
model_class.define_method("set_user_input", method!(Model::set_user_input, 4))?;
|
|
147
|
+
model_class.define_method("clear_cell_contents", method!(Model::clear_cell_contents, 3))?;
|
|
148
|
+
model_class.define_method("get_cell_content", method!(Model::get_cell_content, 3))?;
|
|
149
|
+
model_class.define_method("get_cell_type", method!(Model::get_cell_type, 3))?;
|
|
150
|
+
model_class.define_method(
|
|
151
|
+
"get_formatted_cell_value",
|
|
152
|
+
method!(Model::get_formatted_cell_value, 3),
|
|
153
|
+
)?;
|
|
154
|
+
model_class.define_method("set_cell_style_json", method!(Model::set_cell_style_json, 4))?;
|
|
155
|
+
model_class.define_method("get_cell_style_json", method!(Model::get_cell_style_json, 3))?;
|
|
156
|
+
model_class.define_method("insert_rows", method!(Model::insert_rows, 3))?;
|
|
157
|
+
model_class.define_method("insert_columns", method!(Model::insert_columns, 3))?;
|
|
158
|
+
model_class.define_method("delete_rows", method!(Model::delete_rows, 3))?;
|
|
159
|
+
model_class.define_method("delete_columns", method!(Model::delete_columns, 3))?;
|
|
160
|
+
model_class.define_method("get_column_width", method!(Model::get_column_width, 2))?;
|
|
161
|
+
model_class.define_method("get_row_height", method!(Model::get_row_height, 2))?;
|
|
162
|
+
model_class.define_method("set_column_width", method!(Model::set_column_width, 3))?;
|
|
163
|
+
model_class.define_method("set_row_height", method!(Model::set_row_height, 3))?;
|
|
164
|
+
model_class.define_method(
|
|
165
|
+
"get_frozen_columns_count",
|
|
166
|
+
method!(Model::get_frozen_columns_count, 1),
|
|
167
|
+
)?;
|
|
168
|
+
model_class.define_method(
|
|
169
|
+
"get_frozen_rows_count",
|
|
170
|
+
method!(Model::get_frozen_rows_count, 1),
|
|
171
|
+
)?;
|
|
172
|
+
model_class.define_method(
|
|
173
|
+
"set_frozen_columns_count",
|
|
174
|
+
method!(Model::set_frozen_columns_count, 2),
|
|
175
|
+
)?;
|
|
176
|
+
model_class.define_method(
|
|
177
|
+
"set_frozen_rows_count",
|
|
178
|
+
method!(Model::set_frozen_rows_count, 2),
|
|
179
|
+
)?;
|
|
180
|
+
model_class.define_method(
|
|
181
|
+
"get_worksheets_properties",
|
|
182
|
+
method!(Model::get_worksheets_properties, 0),
|
|
183
|
+
)?;
|
|
184
|
+
model_class.define_method("set_sheet_color", method!(Model::set_sheet_color, 2))?;
|
|
185
|
+
model_class.define_method("add_sheet", method!(Model::add_sheet, 1))?;
|
|
186
|
+
model_class.define_method("new_sheet", method!(Model::new_sheet, 0))?;
|
|
187
|
+
model_class.define_method("delete_sheet", method!(Model::delete_sheet, 1))?;
|
|
188
|
+
model_class.define_method("rename_sheet", method!(Model::rename_sheet, 2))?;
|
|
189
|
+
model_class.define_method("get_sheet_dimensions", method!(Model::get_sheet_dimensions, 1))?;
|
|
190
|
+
model_class.define_method("test_panic", method!(Model::test_panic, 0))?;
|
|
191
|
+
|
|
192
|
+
// User API: IronCalc::UserModel
|
|
193
|
+
let user_model_class = module.define_class("UserModel", ruby.class_object())?;
|
|
194
|
+
user_model_class.define_method("save_to_xlsx", method!(UserModel::save_to_xlsx, 1))?;
|
|
195
|
+
user_model_class.define_method("save_to_icalc", method!(UserModel::save_to_icalc, 1))?;
|
|
196
|
+
user_model_class.define_method(
|
|
197
|
+
"apply_external_diffs",
|
|
198
|
+
method!(UserModel::apply_external_diffs, 1),
|
|
199
|
+
)?;
|
|
200
|
+
user_model_class.define_method("flush_send_queue", method!(UserModel::flush_send_queue, 0))?;
|
|
201
|
+
// Evaluation / history
|
|
202
|
+
user_model_class.define_method("evaluate", method!(UserModel::evaluate, 0))?;
|
|
203
|
+
user_model_class.define_method("undo", method!(UserModel::undo, 0))?;
|
|
204
|
+
user_model_class.define_method("redo", method!(UserModel::redo, 0))?;
|
|
205
|
+
user_model_class.define_method("can_undo", method!(UserModel::can_undo, 0))?;
|
|
206
|
+
user_model_class.define_method("can_redo", method!(UserModel::can_redo, 0))?;
|
|
207
|
+
// Set / clear / get values
|
|
208
|
+
user_model_class.define_method("set_user_input", method!(UserModel::set_user_input, 4))?;
|
|
209
|
+
user_model_class.define_method(
|
|
210
|
+
"clear_cell_contents",
|
|
211
|
+
method!(UserModel::clear_cell_contents, 3),
|
|
212
|
+
)?;
|
|
213
|
+
user_model_class.define_method("get_cell_content", method!(UserModel::get_cell_content, 3))?;
|
|
214
|
+
user_model_class.define_method("get_cell_type", method!(UserModel::get_cell_type, 3))?;
|
|
215
|
+
user_model_class.define_method(
|
|
216
|
+
"get_formatted_cell_value",
|
|
217
|
+
method!(UserModel::get_formatted_cell_value, 3),
|
|
218
|
+
)?;
|
|
219
|
+
// Styles
|
|
220
|
+
user_model_class.define_method(
|
|
221
|
+
"get_cell_style_json",
|
|
222
|
+
method!(UserModel::get_cell_style_json, 3),
|
|
223
|
+
)?;
|
|
224
|
+
user_model_class.define_method(
|
|
225
|
+
"update_range_style",
|
|
226
|
+
method!(UserModel::update_range_style, 5),
|
|
227
|
+
)?;
|
|
228
|
+
// Rows / columns
|
|
229
|
+
user_model_class.define_method("insert_rows", method!(UserModel::insert_rows, 3))?;
|
|
230
|
+
user_model_class.define_method("insert_columns", method!(UserModel::insert_columns, 3))?;
|
|
231
|
+
user_model_class.define_method("delete_rows", method!(UserModel::delete_rows, 3))?;
|
|
232
|
+
user_model_class.define_method("delete_columns", method!(UserModel::delete_columns, 3))?;
|
|
233
|
+
user_model_class.define_method("get_column_width", method!(UserModel::get_column_width, 2))?;
|
|
234
|
+
user_model_class.define_method("get_row_height", method!(UserModel::get_row_height, 2))?;
|
|
235
|
+
user_model_class.define_method("set_column_width", method!(UserModel::set_column_width, 3))?;
|
|
236
|
+
user_model_class.define_method("set_row_height", method!(UserModel::set_row_height, 3))?;
|
|
237
|
+
// Frozen rows / columns
|
|
238
|
+
user_model_class.define_method(
|
|
239
|
+
"get_frozen_columns_count",
|
|
240
|
+
method!(UserModel::get_frozen_columns_count, 1),
|
|
241
|
+
)?;
|
|
242
|
+
user_model_class.define_method(
|
|
243
|
+
"get_frozen_rows_count",
|
|
244
|
+
method!(UserModel::get_frozen_rows_count, 1),
|
|
245
|
+
)?;
|
|
246
|
+
user_model_class.define_method(
|
|
247
|
+
"set_frozen_columns_count",
|
|
248
|
+
method!(UserModel::set_frozen_columns_count, 2),
|
|
249
|
+
)?;
|
|
250
|
+
user_model_class.define_method(
|
|
251
|
+
"set_frozen_rows_count",
|
|
252
|
+
method!(UserModel::set_frozen_rows_count, 2),
|
|
253
|
+
)?;
|
|
254
|
+
// Sheets
|
|
255
|
+
user_model_class.define_method(
|
|
256
|
+
"get_worksheets_properties",
|
|
257
|
+
method!(UserModel::get_worksheets_properties, 0),
|
|
258
|
+
)?;
|
|
259
|
+
user_model_class.define_method("set_sheet_color", method!(UserModel::set_sheet_color, 2))?;
|
|
260
|
+
user_model_class.define_method("new_sheet", method!(UserModel::new_sheet, 0))?;
|
|
261
|
+
user_model_class.define_method("delete_sheet", method!(UserModel::delete_sheet, 1))?;
|
|
262
|
+
user_model_class.define_method("rename_sheet", method!(UserModel::rename_sheet, 2))?;
|
|
263
|
+
user_model_class.define_method(
|
|
264
|
+
"get_sheet_dimensions",
|
|
265
|
+
method!(UserModel::get_sheet_dimensions, 1),
|
|
266
|
+
)?;
|
|
267
|
+
user_model_class.define_method("to_bytes", method!(UserModel::to_bytes, 0))?;
|
|
268
|
+
|
|
269
|
+
Ok(())
|
|
270
|
+
}
|