fast_xlsx 0.1.1 → 0.2.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.
@@ -0,0 +1,23 @@
1
+ // Runs the Ruby test suite against this crate's current source.
2
+ //
3
+ // The extension's behaviour is tested from Ruby (test/), but cargo-mutants only
4
+ // runs `cargo test`. This test rebuilds the extension from the (possibly
5
+ // mutated) source and runs the Ruby suite, so a mutant counts as caught when a
6
+ // Ruby test fails. The dev profile shares build artifacts with `cargo test`.
7
+ use std::path::Path;
8
+ use std::process::Command;
9
+
10
+ #[test]
11
+ fn ruby_test_suite_passes() {
12
+ let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
13
+ let status = Command::new("bundle")
14
+ .args(["exec", "rake", "compile", "test"])
15
+ .current_dir(&root)
16
+ .env("RB_SYS_CARGO_PROFILE", "dev")
17
+ // The suite needs a few CPU seconds; a mutant that loops forever is
18
+ // stopped instead of running on after cargo-mutants gives up on it.
19
+ .env("FAST_XLSX_TEST_CPU_SECONDS", "120")
20
+ .status()
21
+ .expect("could not run `bundle exec rake compile test`");
22
+ assert!(status.success(), "the Ruby test suite failed");
23
+ }
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FastXlsx
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/fast_xlsx.rb CHANGED
@@ -84,6 +84,11 @@ module FastXlsx
84
84
  worksheets.find { |ws| ws.name == name }
85
85
  end
86
86
 
87
+ # path: a String, Pathname or anything responding to #to_path.
88
+ def save(path)
89
+ _save(File.path(path))
90
+ end
91
+
87
92
  # Document properties shown in Excel's File > Info: title:, subject:,
88
93
  # author:, manager:, company:, category:, keywords:, comments:, status:.
89
94
  # Later calls add to earlier ones.
@@ -108,11 +113,11 @@ module FastXlsx
108
113
 
109
114
  # columns: a 0-based column index or a Range of them. width is in characters.
110
115
  def column_width(columns, width)
111
- bounds = column_bounds(columns)
112
- _column_width(*bounds, width)
116
+ range = CellRange.bounds(columns)
117
+ _column_width(*range, width)
113
118
  @fixed_widths ||= {}
114
- @fixed_widths.delete(bounds) # re-insert so autofit replays calls in order
115
- @fixed_widths[bounds] = width
119
+ @fixed_widths.delete(range) # re-insert so autofit replays calls in order
120
+ @fixed_widths[range] = width
116
121
  self
117
122
  end
118
123
 
@@ -124,21 +129,32 @@ module FastXlsx
124
129
  self
125
130
  end
126
131
 
132
+ # The methods below take a cell range in any of these styles:
133
+ # (first_row, first_col, last_row, last_col) four 0-based numbers
134
+ # ("A1:D10") or ("B2") an Excel reference
135
+ # (rows, cols) Integers or Ranges, e.g. (0..9, 0..3)
136
+
137
+ # Filter buttons on the range's first row.
138
+ def autofilter(*range)
139
+ _autofilter(*CellRange.split(range).first)
140
+ end
141
+
127
142
  # Merges the range and writes value (any cell type) into its first cell.
128
- def merge_range(first_row, first_col, last_row, last_col, value, format = nil)
129
- _merge_range(first_row, first_col, last_row, last_col, value, format)
143
+ def merge_range(*args)
144
+ range, (value, format) = CellRange.split(args, 1..2)
145
+ _merge_range(*range, value, format)
130
146
  end
131
147
 
132
148
  # Highlights cells in the range by rule. type: :cell, :text, :formula,
133
149
  # :data_bar or :color_scale; see the README for each type's options.
134
- def conditional_format(first_row, first_col, last_row, last_col, type:, **)
135
- _conditional_format(first_row, first_col, last_row, last_col, { type: type, ** })
150
+ def conditional_format(*range, type:, **)
151
+ _conditional_format(*CellRange.split(range).first, { type: type, ** })
136
152
  end
137
153
 
138
154
  # Restricts what can be entered in the range. type: :list, :whole_number,
139
155
  # :decimal or :text_length; see the README for the options.
140
- def data_validation(first_row, first_col, last_row, last_col, type:, **)
141
- _data_validation(first_row, first_col, last_row, last_col, { type: type, ** })
156
+ def data_validation(*range, type:, **)
157
+ _data_validation(*CellRange.split(range).first, { type: type, ** })
142
158
  end
143
159
 
144
160
  # Adds a comment (Excel "note") to a cell.
@@ -165,8 +181,8 @@ module FastXlsx
165
181
  # into an Excel table. columns: header Strings or { header:, total:,
166
182
  # total_label:, format: }; other options: style:, name:, total_row:,
167
183
  # banded_rows:, autofilter:.
168
- def add_table(first_row, first_col, last_row, last_col, **)
169
- _add_table(first_row, first_col, last_row, last_col, { ** })
184
+ def add_table(*range, **)
185
+ _add_table(*CellRange.split(range).first, { ** })
170
186
  end
171
187
 
172
188
  # Printed page header/footer using Excel codes such as "&CPage &P of &N".
@@ -187,18 +203,106 @@ module FastXlsx
187
203
  self
188
204
  end
189
205
 
206
+ # Outline group with an expand/collapse button. rows: a 0-based row index
207
+ # or a Range; grouping rows already grouped nests them (up to 7 levels).
208
+ # collapsed: true hides them until expanded.
209
+ def group_rows(rows, collapsed: false)
210
+ _group_rows(*CellRange.bounds(rows), collapsed)
211
+ end
212
+
213
+ def group_columns(columns, collapsed: false)
214
+ _group_columns(*CellRange.bounds(columns), collapsed)
215
+ end
216
+
217
+ # Locks the sheet against editing. Cells whose format has locked: false
218
+ # stay editable. allow: actions users may still take, any of :format_cells,
219
+ # :format_columns, :format_rows, :insert_columns, :insert_rows,
220
+ # :insert_links, :delete_columns, :delete_rows, :sort, :use_autofilter,
221
+ # :use_pivot_tables, :edit_scenarios, :edit_objects.
222
+ def protect(password: nil, allow: [])
223
+ _protect(password, Array(allow))
224
+ end
225
+
190
226
  # Default format for cells in these columns that are written without one.
191
227
  def column_format(columns, format)
192
- _column_format(*column_bounds(columns), format)
228
+ _column_format(*CellRange.bounds(columns), format)
193
229
  self
194
230
  end
231
+ end
232
+
233
+ # Cell ranges in the styles Worksheet methods accept: four 0-based numbers,
234
+ # an Excel reference ("A1:D10", "B2"), or rows and columns as Integers or
235
+ # Ranges.
236
+ module CellRange
237
+ REF = /\A\$?([A-Za-z]{1,3})\$?([1-9]\d*)\z/ # ASCII only: /i also matches the Kelvin sign
238
+ FORMS = 'a range is (first_row, first_col, last_row, last_col), "A1:D10" or (rows, cols)'
239
+
240
+ module_function
241
+
242
+ # Splits a range off the front of args and checks how many args follow.
243
+ # Returns [[first_row, first_col, last_row, last_col], the args after it].
244
+ def split(args, following = 0..0)
245
+ range, rest = parse(args)
246
+ unless following.cover?(rest.size)
247
+ expected = following.minmax.uniq.join("..")
248
+ raise ArgumentError, "wrong number of arguments after the cell range (given #{rest.size}, " \
249
+ "expected #{expected}); #{FORMS}"
250
+ end
251
+
252
+ [range, rest]
253
+ end
254
+
255
+ def parse(args)
256
+ case args
257
+ in [Numeric, Numeric, Numeric, Numeric, *rest] then [args.first(4), rest]
258
+ in [String => ref, *rest] then [excel(ref), rest]
259
+ in [Integer | Range => rows, Integer | Range => cols, *rest]
260
+ [rows_and_cols(rows, cols), rest]
261
+ else
262
+ raise ArgumentError, "expected a cell range, got #{args.inspect}; #{FORMS}"
263
+ end
264
+ end
265
+
266
+ def rows_and_cols(rows, cols)
267
+ first_row, last_row = bounds(rows)
268
+ first_col, last_col = bounds(cols)
269
+ [first_row, first_col, last_row, last_col]
270
+ end
271
+
272
+ # "A1:D10", "$A$1:$D$10" or a single "B2".
273
+ def excel(ref)
274
+ cells = cell_matches(ref)
275
+ rows = cells.map { |m| m[2].to_i - 1 }
276
+ cols = cells.map { |m| column(m[1]) }
277
+ [rows.min, cols.min, rows.max, cols.max]
278
+ end
279
+
280
+ def cell_matches(ref)
281
+ cells = ref.split(":", -1).map { |cell| REF.match(cell) }
282
+ return cells if (1..2).cover?(cells.size) && cells.all?
283
+
284
+ raise ArgumentError, "invalid cell range #{ref.inspect}: use e.g. \"A1:D10\" or \"B2\""
285
+ end
286
+
287
+ # "A" => 0, "AA" => 26.
288
+ def column(letters)
289
+ letters.upcase.each_char.reduce(0) { |n, c| (n * 26) + c.ord - 64 } - 1
290
+ end
291
+
292
+ # [first, last] of an index or a Range.
293
+ def bounds(indexes)
294
+ return [indexes, indexes] if indexes.is_a?(Integer)
295
+ unless indexes.is_a?(Range) && indexes.begin.is_a?(Integer) && indexes.end.is_a?(Integer)
296
+ raise ArgumentError, "expected an Integer or a Range of Integers, got #{indexes.inspect}"
297
+ end
195
298
 
196
- private
299
+ first, last = indexes.minmax
300
+ raise ArgumentError, "empty range #{indexes.inspect}" unless first
197
301
 
198
- def column_bounds(columns)
199
- columns.is_a?(Integer) ? [columns, columns] : columns.minmax
302
+ [first, last]
200
303
  end
201
304
  end
305
+ private_constant :CellRange
202
306
 
203
307
  # Cell style, e.g. Format.new(bold: true). Pass to Worksheet#write.
204
308
  class Format
data/sig/fast_xlsx.rbs CHANGED
@@ -36,7 +36,8 @@ module FastXlsx
36
36
  ?company: String, ?category: String, ?keywords: String,
37
37
  ?comments: String, ?status: String) -> self
38
38
  def to_xlsx: () -> String
39
- def save: (String path) -> void
39
+ def define_name: (String name, String formula) -> self
40
+ def save: (String | _ToPath path) -> void
40
41
  end
41
42
 
42
43
  class Format
@@ -46,7 +47,7 @@ module FastXlsx
46
47
  type underline = bool | :single | :double | :single_accounting | :double_accounting
47
48
 
48
49
  def self.new: (?bold: bool, ?italic: bool, ?underline: underline, ?strikeout: bool, ?text_wrap: bool,
49
- ?shrink: bool, ?num_format: String, ?font_size: Numeric, ?font_name: String,
50
+ ?shrink: bool, ?locked: bool, ?hidden: bool, ?num_format: String, ?font_size: Numeric, ?font_name: String,
50
51
  ?font_color: color, ?bg_color: color, ?font_script: :superscript | :subscript,
51
52
  ?align: :left | :center | :right, ?valign: :top | :center | :bottom,
52
53
  ?rotation: Integer, ?indent: Integer,
@@ -62,7 +63,14 @@ module FastXlsx
62
63
  def column_width: (Integer | Range[Integer] columns, Numeric width) -> self
63
64
  def column_format: (Integer | Range[Integer] columns, Format format) -> self
64
65
  def autofit: () -> self
66
+ # A cell range: four 0-based numbers, an Excel reference ("A1:D10"), or
67
+ # rows and columns as Integers or Ranges. Methods with options take it as
68
+ # *cell_range so the keywords can follow.
69
+ type indexes = Integer | Range[Integer]
70
+ type cell_range = Integer | String | Range[Integer]
65
71
  def autofilter: (Integer first_row, Integer first_col, Integer last_row, Integer last_col) -> self
72
+ | (String range) -> self
73
+ | (indexes rows, indexes cols) -> self
66
74
  def freeze_panes: (Integer row, Integer col) -> self
67
75
  def row_height: (Integer row, Numeric height) -> self
68
76
  def page_breaks: (Array[Integer] rows) -> self
@@ -71,12 +79,20 @@ module FastXlsx
71
79
  def page_footer: (String text, ?margin: Numeric?) -> self
72
80
  def margins: (?left: Numeric?, ?right: Numeric?, ?top: Numeric?, ?bottom: Numeric?,
73
81
  ?header: Numeric?, ?footer: Numeric?) -> self
82
+ type protect_action = :format_cells | :format_columns | :format_rows | :insert_columns | :insert_rows
83
+ | :insert_links | :delete_columns | :delete_rows | :sort | :use_autofilter
84
+ | :use_pivot_tables | :edit_scenarios | :edit_objects
85
+ def group_rows: (Integer | Range[Integer] rows, ?collapsed: bool) -> self
86
+ def group_columns: (Integer | Range[Integer] columns, ?collapsed: bool) -> self
87
+ def protect: (?password: String?, ?allow: protect_action | Array[protect_action] | nil) -> self
74
88
  def merge_range: (Integer first_row, Integer first_col, Integer last_row, Integer last_col, cell value, ?Format? format) -> self
75
- def conditional_format: (Integer first_row, Integer first_col, Integer last_row, Integer last_col,
89
+ | (String range, cell value, ?Format? format) -> self
90
+ | (indexes rows, indexes cols, cell value, ?Format? format) -> self
91
+ def conditional_format: (*cell_range range,
76
92
  type: :cell | :text | :formula | :data_bar | :color_scale,
77
93
  ?criteria: Symbol, ?value: Numeric | String | Array[Numeric | String],
78
94
  ?format: Format, ?colors: 2 | 3) -> self
79
- def data_validation: (Integer first_row, Integer first_col, Integer last_row, Integer last_col,
95
+ def data_validation: (*cell_range range,
80
96
  type: :list | :whole_number | :decimal | :text_length,
81
97
  ?criteria: Symbol, ?value: Numeric | String | Array[Numeric | String],
82
98
  ?input_title: String, ?input_message: String,
@@ -91,7 +107,7 @@ module FastXlsx
91
107
  ?width: Integer, ?height: Integer) -> self
92
108
  type table_column = String | { header: String, ?total: Symbol, ?total_label: String, ?format: Format }
93
109
 
94
- def add_table: (Integer first_row, Integer first_col, Integer last_row, Integer last_col,
110
+ def add_table: (*cell_range range,
95
111
  ?columns: Array[table_column], ?style: Symbol, ?name: String,
96
112
  ?total_row: bool, ?banded_rows: bool, ?autofilter: bool) -> self
97
113
  def name: () -> String
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fast_xlsx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zac
@@ -48,6 +48,7 @@ files:
48
48
  - ext/fast_xlsx/build.rs
49
49
  - ext/fast_xlsx/extconf.rb
50
50
  - ext/fast_xlsx/src/lib.rs
51
+ - ext/fast_xlsx/tests/ruby_suite.rs
51
52
  - lib/fast_xlsx.rb
52
53
  - lib/fast_xlsx/version.rb
53
54
  - sig/fast_xlsx.rbs