Skip to content

Commit

Permalink
Handle columns without r attribute (#50)
Browse files Browse the repository at this point in the history
* Handle columns without r attribute (#48)

* Refactor logic
  • Loading branch information
martijn authored Feb 16, 2023
1 parent 8796222 commit 7f68eeb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 3 deletions.
17 changes: 14 additions & 3 deletions lib/xsv/sheet_rows_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(mode, empty_row, workbook, row_skip, last_row, &block)
@store_characters = false

@row_index = 0
@col_index = 0
@current_row = {}
@current_row_number = 0
@current_cell = {}
Expand All @@ -33,7 +34,7 @@ def start_element(name, attrs)
when "v", "is", "t"
@store_characters = true
when "row"
@current_row = @empty_row.dup
@current_row = @mode == :array ? [] : @empty_row.dup
@current_row_number = attrs[:r].to_i
end
end
Expand All @@ -47,19 +48,22 @@ def end_element(name)
when "v", "is", "t"
@store_characters = false
when "c"
col_index = column_index(@current_cell[:r])
col_index = @current_cell[:r] ? column_index(@current_cell[:r]) : @col_index

if @mode == :array
@current_row[col_index] = format_cell
else
@current_row[@headers[col_index]] = format_cell
end

@col_index += 1
when "row"
return if @current_row_number <= @row_skip

adjusted_row_number = @current_row_number - @row_skip

@row_index += 1
@col_index = 0

# Skip first row if we're in hash mode
return if adjusted_row_number == 1 && @mode == :hash
Expand All @@ -72,7 +76,14 @@ def end_element(name)
end

# Do not return empty trailing rows
@block.call(@current_row) unless @row_index > @last_row
return if @row_index > @last_row

# Add trailing empty columns
if @mode == :array && @current_row.length < @empty_row.length
@block.call(@current_row + @empty_row[@current_row.length..])
else
@block.call(@current_row)
end
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/files/column-without-r.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr2="http://schemas.microsoft.com/office/spreadsheetml/2015/revision2" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" mc:Ignorable="x14ac xr xr2 xr3" xr:uid="{8E20F794-4B88-3543-AD6B-C05CAF17DEC8}">
<dimension ref="A1:A4"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="A5" sqref="A5"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="16" x14ac:dyDescent="0.2"/>
<sheetData>
<row r="1" spans="1:1" x14ac:dyDescent="0.2">
<c r="A1" t="s">
<v>0</v>
</c>
<c t="s">
<v>1</v>
</c>
</row>
<row r="2" spans="1:1" x14ac:dyDescent="0.2">
<c t="s">
<v>2</v>
</c>
<c r="B2" t="s">
<v>3</v>
</c>
</row>
</sheetData>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>
31 changes: 31 additions & 0 deletions test/sheet_rows_handler_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,35 @@ def test_unknown_type
handler.parse(data)
end
end

def test_column_without_r_array
@sheet = File.read("test/files/column-without-r.xml")

rows = []

collector = proc do |row|
rows << row
end

handler = Xsv::SheetRowsHandler.new(:array, ([nil] * 2), @workbook, 0, 6, &collector)
handler.parse(@sheet)

assert_equal ["Some strings", "Foo"], rows[0]
assert_equal ["Bar", "Baz"], rows[1]
end

def test_column_without_r_hash
@sheet = File.read("test/files/column-without-r.xml")

rows = []

collector = proc do |row|
rows << row
end

handler = Xsv::SheetRowsHandler.new(:hash, {"Some strings" => "", "Foo" => ""}, @workbook, 0, 6, &collector)
handler.parse(@sheet)

assert_equal({"Some strings" => "Bar", "Foo" => "Baz"}, rows[0])
end
end

0 comments on commit 7f68eeb

Please sign in to comment.