Skip to content

Commit 1acc2fd

Browse files
authored
Add RBS for rubyXL 3.4 (#1006)
* Add RBS for rubyXL 3.4 rubyXL is a gem for reading and writing Excel (.xlsx) files. This commit adds type definitions for the most commonly used APIs described in the README. Defined classes: - RubyXL::SheetData — sparse array of rows with index access - RubyXL::Row — sparse array of cells with index access - RubyXL::Cell — cell value accessor and style-change methods - RubyXL::Worksheet — sheet name, cell creation, row/column styling - RubyXL::Workbook — workbook construction, worksheet access, I/O - RubyXL::Parser — parsing .xlsx files from path or buffer Notable design decisions: - Cell#value returns String | Integer | Float | DateTime | nil. The implementation (num_to_date) returns DateTime, not Date. - manifest.yaml declares `date` stdlib dependency because rubyXL's gemspec does not list it, yet DateTime is used in the RBS. - Workbook#write is included as it is a documented public API (alias of save), even though it is defined via alias_method. * remove metadata.yaml
1 parent 395d5a4 commit 1acc2fd

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

gems/rubyXL/.rubocop.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This configuration inherits from /.rubocop.yml.
2+
# You can configure RBS style of this gem.
3+
# This file is used on CI. It is configured to automatically
4+
# make rubocop suggestions on pull requests for this gem.
5+
# If you do not like the style enforcement, you should remove this file.
6+
inherit_from: ../../.rubocop.yml
7+
8+
##
9+
# If you want to customize the style, please consult with the gem reviewers.
10+
# You can see the list of cops at https://github.com/ksss/rubocop-on-rbs/blob/main/docs/modules/ROOT/pages/cops.adoc
11+
12+
RBS/Layout:
13+
Enabled: true
14+
15+
RBS/Lint:
16+
Enabled: true
17+
18+
RBS/Style:
19+
Enabled: true

gems/rubyXL/3.4/_test/test.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
require "rubyXL"
2+
require "rubyXL/convenience_methods"
3+
4+
workbook = RubyXL::Workbook.new
5+
6+
worksheets = workbook.worksheets
7+
worksheet = worksheets[0]
8+
9+
new_sheet = workbook.add_worksheet("Sheet2")
10+
new_sheet.sheet_name = "Renamed Sheet"
11+
name = new_sheet.sheet_name
12+
13+
sheet_by_index = workbook[0]
14+
sheet_by_name = workbook["Sheet1"]
15+
16+
cell = new_sheet.add_cell(0, 0, "Hello, RubyXL!")
17+
cell2 = new_sheet.add_cell(0, 1, 42)
18+
cell3 = new_sheet.add_cell(1, 0, 3.14)
19+
20+
val = cell.value
21+
22+
cell.change_fill("FF0000")
23+
cell.change_font_bold(true)
24+
cell.change_text_wrap(true)
25+
cell.change_vertical_alignment("center")
26+
27+
new_sheet.change_row_height(0, 20)
28+
new_sheet.change_row_font_size(0, 12)
29+
new_sheet.change_column_width(0, 15)
30+
31+
sheet_data = new_sheet.sheet_data
32+
rows = sheet_data.rows
33+
row_count = sheet_data.size
34+
35+
row = new_sheet[0]
36+
if row
37+
cells = row.cells
38+
cell_count = row.size
39+
40+
first_cell = row[0]
41+
if first_cell
42+
first_val = first_cell.value
43+
end
44+
end
45+
46+
io = workbook.stream

gems/rubyXL/3.4/manifest.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# manifest.yaml describes dependencies which do not appear in the gemspec.
2+
# If this gem includes such dependencies, comment-out the following lines and
3+
# declare the dependencies.
4+
# If all dependencies appear in the gemspec, you should remove this file.
5+
#
6+
dependencies:
7+
- name: date

gems/rubyXL/3.4/rubyXL.rbs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
module RubyXL
2+
class SheetData
3+
def rows: () -> Array[Row?]
4+
def []: (Integer ind) -> Row?
5+
def size: () -> Integer
6+
end
7+
8+
class Row
9+
def cells: () -> Array[Cell?]
10+
def []: (Integer ind) -> Cell?
11+
def size: () -> Integer
12+
end
13+
14+
class Cell
15+
def value: () -> (::String | ::Integer | ::Float | ::DateTime | nil)
16+
def change_fill: (::String rgb) -> void
17+
def change_font_bold: (bool bold) -> void
18+
def change_text_wrap: (bool wrap) -> void
19+
def change_vertical_alignment: (::String alignment) -> void
20+
end
21+
22+
class Worksheet
23+
def sheet_name: () -> ::String
24+
def sheet_name=: (::String name) -> ::String
25+
def sheet_data: () -> SheetData
26+
def []: (Integer row) -> Row?
27+
def add_cell: (Integer row_index, Integer column_index, ?untyped data, ?::String? formula, ?bool overwrite) -> Cell
28+
def change_row_height: (?Integer row, ?::Numeric height) -> void
29+
def change_row_font_size: (?Integer row, ?::Numeric font_size) -> void
30+
def change_column_width: (Integer column_index, ?::Numeric width_in_chars) -> void
31+
end
32+
33+
class Workbook
34+
def initialize: () -> void
35+
def worksheets: () -> Array[Worksheet]
36+
def []: (Integer index) -> Worksheet?
37+
| (::String name) -> Worksheet?
38+
def add_worksheet: (?::String? name) -> Worksheet
39+
def stream: () -> ::StringIO
40+
def write: (::String dst_file_path) -> ::String
41+
end
42+
43+
class Parser
44+
def self.parse: (::String src_file_path) -> Workbook
45+
def self.parse_buffer: (::IO | ::String buffer) -> Workbook
46+
end
47+
end

0 commit comments

Comments
 (0)