1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
require "test_helper"
class TestRooFormatterXML < Minitest::Test
def test_to_xml
expected_sheet_count = 5
options = { name: "numbers1", encoding: "utf8" }
with_each_spreadsheet(options) do |workbook|
skip if defined? JRUBY_VERSION
workbook.to_xml
sheetname = workbook.sheets.first
doc = Nokogiri::XML(workbook.to_xml)
assert_equal expected_sheet_count, doc.xpath("//spreadsheet/sheet").count
doc.xpath("//spreadsheet/sheet").each do |xml_sheet|
all_cells = init_all_cells(workbook, sheetname)
cells = xml_sheet.children.reject(&:text?)
assert_equal sheetname, xml_sheet["name"]
assert_equal all_cells.size, cells.size
cells.each_with_index do |cell, i|
expected = [
all_cells[i][:row],
all_cells[i][:column],
all_cells[i][:content],
all_cells[i][:type],
]
result = [
cell["row"],
cell["column"],
cell.text,
cell["type"],
]
assert_equal expected, result
end # end of sheet
sheetname = workbook.sheets[workbook.sheets.index(sheetname) + 1]
end
end
end
def test_bug_to_xml_with_empty_sheets
with_each_spreadsheet(name: "emptysheets", format: [:openoffice, :excelx]) do |workbook|
workbook.sheets.each do |sheet|
assert_nil workbook.first_row, "first_row not nil in sheet #{sheet}"
assert_nil workbook.last_row, "last_row not nil in sheet #{sheet}"
assert_nil workbook.first_column, "first_column not nil in sheet #{sheet}"
assert_nil workbook.last_column, "last_column not nil in sheet #{sheet}"
assert_nil workbook.first_row(sheet), "first_row not nil in sheet #{sheet}"
assert_nil workbook.last_row(sheet), "last_row not nil in sheet #{sheet}"
assert_nil workbook.first_column(sheet), "first_column not nil in sheet #{sheet}"
assert_nil workbook.last_column(sheet), "last_column not nil in sheet #{sheet}"
end
workbook.to_xml
end
end
# Erstellt eine Liste aller Zellen im Spreadsheet. Dies ist nötig, weil ein einfacher
# Textvergleich des XML-Outputs nicht funktioniert, da xml-builder die Attribute
# nicht immer in der gleichen Reihenfolge erzeugt.
def init_all_cells(workbook, sheet)
all = []
workbook.first_row(sheet).upto(workbook.last_row(sheet)) do |row|
workbook.first_column(sheet).upto(workbook.last_column(sheet)) do |col|
next if workbook.empty?(row, col, sheet)
all << {
row: row.to_s,
column: col.to_s,
content: workbook.cell(row, col, sheet).to_s,
type: workbook.celltype(row, col, sheet).to_s,
}
end
end
all
end
end
|