roo 1.13.2 → 2.10.1

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.
Files changed (236) hide show
  1. checksums.yaml +5 -5
  2. data/.codeclimate.yml +17 -0
  3. data/.github/issue_template.md +16 -0
  4. data/.github/pull_request_template.md +14 -0
  5. data/.github/workflows/pull-request.yml +15 -0
  6. data/.github/workflows/ruby.yml +34 -0
  7. data/.gitignore +11 -0
  8. data/.rubocop.yml +186 -0
  9. data/.simplecov +4 -0
  10. data/CHANGELOG.md +702 -0
  11. data/Gemfile +18 -12
  12. data/Guardfile +23 -0
  13. data/LICENSE +5 -1
  14. data/README.md +328 -0
  15. data/Rakefile +23 -23
  16. data/examples/roo_soap_client.rb +28 -31
  17. data/examples/roo_soap_server.rb +4 -6
  18. data/examples/write_me.rb +9 -10
  19. data/lib/roo/base.rb +317 -504
  20. data/lib/roo/constants.rb +7 -0
  21. data/lib/roo/csv.rb +141 -113
  22. data/lib/roo/errors.rb +11 -0
  23. data/lib/roo/excelx/cell/base.rb +108 -0
  24. data/lib/roo/excelx/cell/boolean.rb +30 -0
  25. data/lib/roo/excelx/cell/date.rb +28 -0
  26. data/lib/roo/excelx/cell/datetime.rb +107 -0
  27. data/lib/roo/excelx/cell/empty.rb +20 -0
  28. data/lib/roo/excelx/cell/number.rb +99 -0
  29. data/lib/roo/excelx/cell/string.rb +19 -0
  30. data/lib/roo/excelx/cell/time.rb +44 -0
  31. data/lib/roo/excelx/cell.rb +110 -0
  32. data/lib/roo/excelx/comments.rb +55 -0
  33. data/lib/roo/excelx/coordinate.rb +19 -0
  34. data/lib/roo/excelx/extractor.rb +39 -0
  35. data/lib/roo/excelx/format.rb +71 -0
  36. data/lib/roo/excelx/images.rb +26 -0
  37. data/lib/roo/excelx/relationships.rb +33 -0
  38. data/lib/roo/excelx/shared.rb +39 -0
  39. data/lib/roo/excelx/shared_strings.rb +151 -0
  40. data/lib/roo/excelx/sheet.rb +151 -0
  41. data/lib/roo/excelx/sheet_doc.rb +257 -0
  42. data/lib/roo/excelx/styles.rb +64 -0
  43. data/lib/roo/excelx/workbook.rb +64 -0
  44. data/lib/roo/excelx.rb +407 -601
  45. data/lib/roo/font.rb +17 -0
  46. data/lib/roo/formatters/base.rb +15 -0
  47. data/lib/roo/formatters/csv.rb +84 -0
  48. data/lib/roo/formatters/matrix.rb +23 -0
  49. data/lib/roo/formatters/xml.rb +31 -0
  50. data/lib/roo/formatters/yaml.rb +40 -0
  51. data/lib/roo/helpers/default_attr_reader.rb +20 -0
  52. data/lib/roo/helpers/weak_instance_cache.rb +41 -0
  53. data/lib/roo/libre_office.rb +4 -0
  54. data/lib/roo/link.rb +34 -0
  55. data/lib/roo/open_office.rb +631 -0
  56. data/lib/roo/spreadsheet.rb +28 -23
  57. data/lib/roo/tempdir.rb +24 -0
  58. data/lib/roo/utils.rb +128 -0
  59. data/lib/roo/version.rb +3 -0
  60. data/lib/roo.rb +26 -24
  61. data/roo.gemspec +29 -203
  62. data/spec/helpers.rb +5 -0
  63. data/spec/lib/roo/base_spec.rb +291 -3
  64. data/spec/lib/roo/csv_spec.rb +38 -11
  65. data/spec/lib/roo/excelx/cell/time_spec.rb +15 -0
  66. data/spec/lib/roo/excelx/format_spec.rb +7 -6
  67. data/spec/lib/roo/excelx/relationships_spec.rb +43 -0
  68. data/spec/lib/roo/excelx/sheet_doc_spec.rb +11 -0
  69. data/spec/lib/roo/excelx_spec.rb +672 -11
  70. data/spec/lib/roo/libreoffice_spec.rb +16 -6
  71. data/spec/lib/roo/openoffice_spec.rb +30 -8
  72. data/spec/lib/roo/spreadsheet_spec.rb +60 -12
  73. data/spec/lib/roo/strict_spec.rb +43 -0
  74. data/spec/lib/roo/utils_spec.rb +119 -0
  75. data/spec/lib/roo/weak_instance_cache_spec.rb +92 -0
  76. data/spec/lib/roo_spec.rb +0 -0
  77. data/spec/spec_helper.rb +7 -6
  78. data/test/all_ss.rb +12 -11
  79. data/test/excelx/cell/test_attr_reader_default.rb +72 -0
  80. data/test/excelx/cell/test_base.rb +68 -0
  81. data/test/excelx/cell/test_boolean.rb +36 -0
  82. data/test/excelx/cell/test_date.rb +38 -0
  83. data/test/excelx/cell/test_datetime.rb +45 -0
  84. data/test/excelx/cell/test_empty.rb +18 -0
  85. data/test/excelx/cell/test_number.rb +90 -0
  86. data/test/excelx/cell/test_string.rb +48 -0
  87. data/test/excelx/cell/test_time.rb +30 -0
  88. data/test/excelx/test_coordinate.rb +51 -0
  89. data/test/formatters/test_csv.rb +136 -0
  90. data/test/formatters/test_matrix.rb +76 -0
  91. data/test/formatters/test_xml.rb +78 -0
  92. data/test/formatters/test_yaml.rb +20 -0
  93. data/test/helpers/test_accessing_files.rb +81 -0
  94. data/test/helpers/test_comments.rb +43 -0
  95. data/test/helpers/test_formulas.rb +9 -0
  96. data/test/helpers/test_labels.rb +103 -0
  97. data/test/helpers/test_sheets.rb +55 -0
  98. data/test/helpers/test_styles.rb +62 -0
  99. data/test/roo/test_base.rb +182 -0
  100. data/test/roo/test_csv.rb +88 -0
  101. data/test/roo/test_excelx.rb +360 -0
  102. data/test/roo/test_libre_office.rb +9 -0
  103. data/test/roo/test_open_office.rb +289 -0
  104. data/test/test_helper.rb +123 -59
  105. data/test/test_roo.rb +392 -2292
  106. metadata +153 -298
  107. data/CHANGELOG +0 -417
  108. data/Gemfile.lock +0 -78
  109. data/README.markdown +0 -126
  110. data/VERSION +0 -1
  111. data/lib/roo/excel.rb +0 -355
  112. data/lib/roo/excel2003xml.rb +0 -300
  113. data/lib/roo/google.rb +0 -292
  114. data/lib/roo/openoffice.rb +0 -496
  115. data/lib/roo/roo_rails_helper.rb +0 -83
  116. data/lib/roo/worksheet.rb +0 -18
  117. data/scripts/txt2html +0 -67
  118. data/spec/lib/roo/excel2003xml_spec.rb +0 -15
  119. data/spec/lib/roo/excel_spec.rb +0 -17
  120. data/spec/lib/roo/google_spec.rb +0 -64
  121. data/test/files/1900_base.xls +0 -0
  122. data/test/files/1900_base.xlsx +0 -0
  123. data/test/files/1904_base.xls +0 -0
  124. data/test/files/1904_base.xlsx +0 -0
  125. data/test/files/Bibelbund.csv +0 -3741
  126. data/test/files/Bibelbund.ods +0 -0
  127. data/test/files/Bibelbund.xls +0 -0
  128. data/test/files/Bibelbund.xlsx +0 -0
  129. data/test/files/Bibelbund.xml +0 -62518
  130. data/test/files/Bibelbund1.ods +0 -0
  131. data/test/files/Pfand_from_windows_phone.xlsx +0 -0
  132. data/test/files/bad_excel_date.xls +0 -0
  133. data/test/files/bbu.ods +0 -0
  134. data/test/files/bbu.xls +0 -0
  135. data/test/files/bbu.xlsx +0 -0
  136. data/test/files/bbu.xml +0 -152
  137. data/test/files/bode-v1.ods.zip +0 -0
  138. data/test/files/bode-v1.xls.zip +0 -0
  139. data/test/files/boolean.csv +0 -2
  140. data/test/files/boolean.ods +0 -0
  141. data/test/files/boolean.xls +0 -0
  142. data/test/files/boolean.xlsx +0 -0
  143. data/test/files/boolean.xml +0 -112
  144. data/test/files/borders.ods +0 -0
  145. data/test/files/borders.xls +0 -0
  146. data/test/files/borders.xlsx +0 -0
  147. data/test/files/borders.xml +0 -144
  148. data/test/files/bug-numbered-sheet-names.xlsx +0 -0
  149. data/test/files/bug-row-column-fixnum-float.xls +0 -0
  150. data/test/files/bug-row-column-fixnum-float.xml +0 -127
  151. data/test/files/comments.ods +0 -0
  152. data/test/files/comments.xls +0 -0
  153. data/test/files/comments.xlsx +0 -0
  154. data/test/files/csvtypes.csv +0 -1
  155. data/test/files/datetime.ods +0 -0
  156. data/test/files/datetime.xls +0 -0
  157. data/test/files/datetime.xlsx +0 -0
  158. data/test/files/datetime.xml +0 -142
  159. data/test/files/datetime_floatconv.xls +0 -0
  160. data/test/files/datetime_floatconv.xml +0 -148
  161. data/test/files/dreimalvier.ods +0 -0
  162. data/test/files/emptysheets.ods +0 -0
  163. data/test/files/emptysheets.xls +0 -0
  164. data/test/files/emptysheets.xlsx +0 -0
  165. data/test/files/emptysheets.xml +0 -105
  166. data/test/files/excel2003.xml +0 -21140
  167. data/test/files/false_encoding.xls +0 -0
  168. data/test/files/false_encoding.xml +0 -132
  169. data/test/files/file_item_error.xlsx +0 -0
  170. data/test/files/formula.ods +0 -0
  171. data/test/files/formula.xls +0 -0
  172. data/test/files/formula.xlsx +0 -0
  173. data/test/files/formula.xml +0 -134
  174. data/test/files/formula_parse_error.xls +0 -0
  175. data/test/files/formula_parse_error.xml +0 -1833
  176. data/test/files/formula_string_error.xlsx +0 -0
  177. data/test/files/html-escape.ods +0 -0
  178. data/test/files/link.xls +0 -0
  179. data/test/files/link.xlsx +0 -0
  180. data/test/files/matrix.ods +0 -0
  181. data/test/files/matrix.xls +0 -0
  182. data/test/files/named_cells.ods +0 -0
  183. data/test/files/named_cells.xls +0 -0
  184. data/test/files/named_cells.xlsx +0 -0
  185. data/test/files/no_spreadsheet_file.txt +0 -1
  186. data/test/files/numbers1.csv +0 -18
  187. data/test/files/numbers1.ods +0 -0
  188. data/test/files/numbers1.xls +0 -0
  189. data/test/files/numbers1.xlsx +0 -0
  190. data/test/files/numbers1.xml +0 -312
  191. data/test/files/numeric-link.xlsx +0 -0
  192. data/test/files/only_one_sheet.ods +0 -0
  193. data/test/files/only_one_sheet.xls +0 -0
  194. data/test/files/only_one_sheet.xlsx +0 -0
  195. data/test/files/only_one_sheet.xml +0 -67
  196. data/test/files/paragraph.ods +0 -0
  197. data/test/files/paragraph.xls +0 -0
  198. data/test/files/paragraph.xlsx +0 -0
  199. data/test/files/paragraph.xml +0 -127
  200. data/test/files/prova.xls +0 -0
  201. data/test/files/ric.ods +0 -0
  202. data/test/files/simple_spreadsheet.ods +0 -0
  203. data/test/files/simple_spreadsheet.xls +0 -0
  204. data/test/files/simple_spreadsheet.xlsx +0 -0
  205. data/test/files/simple_spreadsheet.xml +0 -225
  206. data/test/files/simple_spreadsheet_from_italo.ods +0 -0
  207. data/test/files/simple_spreadsheet_from_italo.xls +0 -0
  208. data/test/files/simple_spreadsheet_from_italo.xml +0 -242
  209. data/test/files/so_datetime.csv +0 -7
  210. data/test/files/style.ods +0 -0
  211. data/test/files/style.xls +0 -0
  212. data/test/files/style.xlsx +0 -0
  213. data/test/files/style.xml +0 -154
  214. data/test/files/time-test.csv +0 -2
  215. data/test/files/time-test.ods +0 -0
  216. data/test/files/time-test.xls +0 -0
  217. data/test/files/time-test.xlsx +0 -0
  218. data/test/files/time-test.xml +0 -131
  219. data/test/files/type_excel.ods +0 -0
  220. data/test/files/type_excel.xlsx +0 -0
  221. data/test/files/type_excelx.ods +0 -0
  222. data/test/files/type_excelx.xls +0 -0
  223. data/test/files/type_openoffice.xls +0 -0
  224. data/test/files/type_openoffice.xlsx +0 -0
  225. data/test/files/whitespace.ods +0 -0
  226. data/test/files/whitespace.xls +0 -0
  227. data/test/files/whitespace.xlsx +0 -0
  228. data/test/files/whitespace.xml +0 -184
  229. data/test/rm_sub_test.rb +0 -12
  230. data/test/rm_test.rb +0 -7
  231. data/test/test_generic_spreadsheet.rb +0 -259
  232. data/website/index.html +0 -385
  233. data/website/index.txt +0 -423
  234. data/website/javascripts/rounded_corners_lite.inc.js +0 -285
  235. data/website/stylesheets/screen.css +0 -130
  236. data/website/template.rhtml +0 -48
@@ -0,0 +1,360 @@
1
+ require "test_helper"
2
+
3
+ class TestRworkbookExcelx < Minitest::Test
4
+ def test_download_uri_with_invalid_host
5
+ assert_raises(RuntimeError) do
6
+ Roo::Excelx.new("http://examples.com/file.xlsx")
7
+ end
8
+ end
9
+
10
+ def test_download_uri_with_query_string
11
+ file = filename("simple_spreadsheet")
12
+ port = 12_344
13
+ url = "#{local_server(port)}/#{file}?query-param=value"
14
+
15
+ start_local_server(file, port) do
16
+ spreadsheet = roo_class.new(url)
17
+ assert_equal "Task 1", spreadsheet.cell("f", 4)
18
+ end
19
+ end
20
+
21
+ def test_should_raise_file_not_found_error
22
+ assert_raises(IOError) do
23
+ roo_class.new(File.join("testnichtvorhanden", "Bibelbund.xlsx"))
24
+ end
25
+ end
26
+
27
+ def test_file_warning_default
28
+ assert_raises(TypeError) { roo_class.new(File.join(TESTDIR, "numbers1.ods")) }
29
+ assert_raises(TypeError) { roo_class.new(File.join(TESTDIR, "numbers1.xls")) }
30
+ end
31
+
32
+ def test_file_warning_error
33
+ %w(ods xls).each do |extension|
34
+ assert_raises(TypeError) do
35
+ options = { packed: false, file_warning: :error }
36
+ roo_class.new(File.join(TESTDIR, "numbers1. #{extension}"), options)
37
+ end
38
+ end
39
+ end
40
+
41
+ def test_file_warning_warning
42
+ options = { packed: false, file_warning: :warning }
43
+ assert_raises(ArgumentError) do
44
+ roo_class.new(File.join(TESTDIR, "numbers1.ods"), options)
45
+ end
46
+ end
47
+
48
+ def test_file_warning_ignore
49
+ options = { packed: false, file_warning: :ignore }
50
+ sheet = roo_class.new(File.join(TESTDIR, "type_excelx.ods"), options)
51
+ assert sheet, "Should not throw an error"
52
+ end
53
+
54
+ def test_bug_xlsx_reference_cell
55
+ # NOTE: If cell A contains a string and cell B references cell A. When
56
+ # reading the value of cell B, the result will be "0.0" instead of the
57
+ # value of cell A.
58
+ #
59
+ # Before this test case, the following code:
60
+ #
61
+ # spreadsheet = Roo::Excelx.new("formula_string_error.xlsx")
62
+ # spreadsheet.default_sheet = "sheet1"
63
+ # p "A: #{spreadsheet.cell(1, 1)}" #=> "A: TestString"
64
+ # p "B: #{spreadsheet.cell(2, 1)}" #=> "B: 0.0"
65
+ #
66
+ # where the expected result is
67
+ # "A: TestString"
68
+ # "B: TestString"
69
+ xlsx = roo_class.new(File.join(TESTDIR, "formula_string_error.xlsx"))
70
+ assert_equal "Teststring", xlsx.cell("a", 1)
71
+ assert_equal "Teststring", xlsx.cell("a", 2)
72
+ end
73
+
74
+ def test_parsing_xslx_from_numbers
75
+ xlsx = roo_class.new(File.join(TESTDIR, "numbers-export.xlsx"))
76
+
77
+ xlsx.default_sheet = xlsx.sheets.first
78
+ assert_equal "Sheet 1", xlsx.cell("a", 1)
79
+
80
+ # Another buggy behavior of Numbers 3.1: if a warkbook has more than a
81
+ # single sheet, all sheets except the first one will have an extra row and
82
+ # column added to the beginning. That's why we assert against cell B2 and
83
+ # not A1
84
+ xlsx.default_sheet = xlsx.sheets.last
85
+ assert_equal "Sheet 2", xlsx.cell("b", 2)
86
+ end
87
+
88
+ def assert_cell_range_values(sheet, row_range, column_range, is_merged_range, expected_value)
89
+ row_range.each do |row|
90
+ column_range.each do |col|
91
+ value = sheet.cell(col, row)
92
+ if is_merged_range.call(row, col)
93
+ assert_equal expected_value, value
94
+ else
95
+ assert_nil value
96
+ end
97
+ end
98
+ end
99
+ end
100
+
101
+ def test_expand_merged_range
102
+ options = { expand_merged_ranges: true }
103
+ xlsx = roo_class.new(File.join(TESTDIR, "merged_ranges.xlsx"), options)
104
+
105
+ [
106
+ {
107
+ rows: (3..7),
108
+ columns: ("a".."b"),
109
+ conditional: ->(row, col) { row > 3 && row < 7 && col == "a" },
110
+ expected_value: "vertical1"
111
+ },
112
+ {
113
+ rows: (3..11),
114
+ columns: ("f".."h"),
115
+ conditional: ->(row, col) { row > 3 && row < 11 && col == "g" },
116
+ expected_value: "vertical2"
117
+ },
118
+ {
119
+ rows: (3..5),
120
+ columns: ("b".."f"),
121
+ conditional: ->(row, col) { row == 4 && col > "b" && col < "f" },
122
+ expected_value: "horizontal"
123
+ },
124
+ {
125
+ rows: (8..13),
126
+ columns: ("a".."e"),
127
+ conditional: ->(row, col) { row > 8 && row < 13 && col > "a" && col < "e" },
128
+ expected_value: "block"
129
+ }
130
+ ].each do |data|
131
+ rows, cols, conditional, expected_value = data.values
132
+ assert_cell_range_values(xlsx, rows, cols, conditional, expected_value)
133
+ end
134
+ end
135
+
136
+ def test_expand_merged_range_doesnt_insert_nil_values
137
+ options = { expand_merged_ranges: true }
138
+ xlsx = roo_class.new(File.join(TESTDIR, "merged_ranges.xlsx"), options)
139
+
140
+ refute_includes xlsx.sheet_for(0).cells.values, nil, "`nil` was copied into the cells hash from an empty merged range"
141
+ end
142
+
143
+ def test_expand_merged_range_doesnt_raise_issue_506
144
+ # Issue 506 sent an example test.xlsx file that would raise an error upon parsing.
145
+ xl = Roo::Spreadsheet.open(File.join(TESTDIR, "expand_merged_ranges_issue_506.xlsx"), expand_merged_ranges: true)
146
+ data = xl.parse(one: /one/i, two: /two/i, clean: true)
147
+ assert_equal [{:one=>"John", :two=>"Johnson"}, {:one=>"Sam", :two=>nil}, {:one=>"Dave", :two=>nil}], data
148
+ end
149
+
150
+ def test_noexpand_merged_range
151
+ xlsx = roo_class.new(File.join(TESTDIR, "merged_ranges.xlsx"))
152
+
153
+ [
154
+ {
155
+ rows: (3..7),
156
+ columns: ("a".."b"),
157
+ conditional: ->(row, col) { row == 4 && col == "a" },
158
+ expected_value: "vertical1"
159
+ },
160
+ {
161
+ rows: (3..11),
162
+ columns: ("f".."h"),
163
+ conditional: ->(row, col) { row == 4 && col == "g" },
164
+ expected_value: "vertical2"
165
+ },
166
+ {
167
+ rows: (3..5),
168
+ columns: ("b".."f"),
169
+ conditional: ->(row, col) { row == 4 && col == "c" },
170
+ expected_value: "horizontal"
171
+ },
172
+ {
173
+ rows: (8..13),
174
+ columns: ("a".."e"),
175
+ conditional: ->(row, col) { row == 9 && col == "b" },
176
+ expected_value: "block"
177
+ }
178
+ ].each do |data|
179
+ rows, cols, conditional, expected_value = data.values
180
+ assert_cell_range_values(xlsx, rows, cols, conditional, expected_value)
181
+ end
182
+ end
183
+
184
+ def test_open_stream
185
+ file = filename(:numbers1)
186
+ file_contents = File.read File.join(TESTDIR, file), encoding: "BINARY"
187
+ stream = StringIO.new(file_contents)
188
+ xlsx = roo_class.new(stream)
189
+ expected_sheet_names = ["Tabelle1", "Name of Sheet 2", "Sheet3", "Sheet4", "Sheet5"]
190
+ assert_equal expected_sheet_names, xlsx.sheets
191
+ end
192
+
193
+ def test_header_offset
194
+ xlsx = roo_class.new(File.join(TESTDIR, "header_offset.xlsx"))
195
+ data = xlsx.parse(column_1: "Header A1", column_2: "Header B1")
196
+ assert_equal "Data A2", data[0][:column_1]
197
+ assert_equal "Data B2", data[0][:column_2]
198
+ end
199
+
200
+ def test_formula_excelx
201
+ with_each_spreadsheet(name: "formula", format: :excelx) do |workbook|
202
+ assert_equal 1, workbook.cell("A", 1)
203
+ assert_equal 2, workbook.cell("A", 2)
204
+ assert_equal 3, workbook.cell("A", 3)
205
+ assert_equal 4, workbook.cell("A", 4)
206
+ assert_equal 5, workbook.cell("A", 5)
207
+ assert_equal 6, workbook.cell("A", 6)
208
+ assert_equal 21, workbook.cell("A", 7)
209
+ assert_equal :formula, workbook.celltype("A", 7)
210
+ assert_nil workbook.formula("A", 6)
211
+
212
+ expected_result = [
213
+ [7, 1, "SUM(A1:A6)"],
214
+ [7, 2, "SUM($A$1:B6)"],
215
+ ]
216
+ assert_equal expected_result, workbook.formulas(workbook.sheets.first)
217
+
218
+ # setting a cell
219
+ workbook.set("A", 15, 41)
220
+ assert_equal 41, workbook.cell("A", 15)
221
+ workbook.set("A", 16, "41")
222
+ assert_equal "41", workbook.cell("A", 16)
223
+ workbook.set("A", 17, 42.5)
224
+ assert_equal 42.5, workbook.cell("A", 17)
225
+ end
226
+ end
227
+
228
+ # TODO: temporaerer Test
229
+ def test_seiten_als_date
230
+ skip_long_test
231
+
232
+ with_each_spreadsheet(name: "Bibelbund", format: :excelx) do |workbook|
233
+ assert_equal "Bericht aus dem Sekretariat", workbook.cell(13, 1)
234
+ assert_equal "1981-4", workbook.cell(13, "D")
235
+ assert_equal String, workbook.excelx_type(13, "E")[1].class
236
+ assert_equal [:numeric_or_formula, "General"], workbook.excelx_type(13, "E")
237
+ assert_equal "428", workbook.excelx_value(13, "E")
238
+ assert_equal 428.0, workbook.cell(13, "E")
239
+ end
240
+ end
241
+
242
+ def test_bug_simple_spreadsheet_time_bug
243
+ # really a bug? are cells really of type time?
244
+ # No! :float must be the correct type
245
+ with_each_spreadsheet(name: "simple_spreadsheet", format: :excelx) do |workbook|
246
+ # puts workbook.cell("B", 5).to_s
247
+ # assert_equal :time, workbook.celltype("B", 5)
248
+ assert_equal :float, workbook.celltype("B", 5)
249
+ assert_equal 10.75, workbook.cell("B", 5)
250
+ assert_equal 12.50, workbook.cell("C", 5)
251
+ assert_equal 0, workbook.cell("D", 5)
252
+ assert_equal 1.75, workbook.cell("E", 5)
253
+ assert_equal "Task 1", workbook.cell("F", 5)
254
+ assert_equal Date.new(2007, 5, 7), workbook.cell("A", 5)
255
+ end
256
+ end
257
+
258
+ def test_simple2_excelx
259
+ with_each_spreadsheet(name: "simple_spreadsheet", format: :excelx) do |workbook|
260
+ assert_equal [:numeric_or_formula, "yyyy\\-mm\\-dd"], workbook.excelx_type("A", 4)
261
+ assert_equal [:numeric_or_formula, "#,##0.00"], workbook.excelx_type("B", 4)
262
+ assert_equal [:numeric_or_formula, "#,##0.00"], workbook.excelx_type("c", 4)
263
+ assert_equal [:numeric_or_formula, "General"], workbook.excelx_type("d", 4)
264
+ assert_equal [:numeric_or_formula, "General"], workbook.excelx_type("e", 4)
265
+ assert_equal :string, workbook.excelx_type("f", 4)
266
+
267
+ assert_equal "39209", workbook.excelx_value("a", 4)
268
+ assert_equal "yyyy\\-mm\\-dd", workbook.excelx_format("a", 4)
269
+ assert_equal "9.25", workbook.excelx_value("b", 4)
270
+ assert_equal "10.25", workbook.excelx_value("c", 4)
271
+ assert_equal "0", workbook.excelx_value("d", 4)
272
+ # ... Sum-Spalte
273
+ # assert_equal "Task 1", workbook.excelx_value("f", 4)
274
+ assert_equal "Task 1", workbook.cell("f", 4)
275
+ assert_equal Date.new(2007, 05, 07), workbook.cell("a", 4)
276
+ assert_equal "9.25", workbook.excelx_value("b", 4)
277
+ assert_equal "#,##0.00", workbook.excelx_format("b", 4)
278
+ assert_equal 9.25, workbook.cell("b", 4)
279
+ assert_equal :float, workbook.celltype("b", 4)
280
+ assert_equal :float, workbook.celltype("d", 4)
281
+ assert_equal 0, workbook.cell("d", 4)
282
+ assert_equal :formula, workbook.celltype("e", 4)
283
+ assert_equal 1, workbook.cell("e", 4)
284
+ assert_equal "C4-B4-D4", workbook.formula("e", 4)
285
+ assert_equal :string, workbook.celltype("f", 4)
286
+ assert_equal "Task 1", workbook.cell("f", 4)
287
+ end
288
+ end
289
+
290
+ def test_bug_pfand_from_windows_phone_xlsx
291
+ # skip_jruby_incompatible_test
292
+ # TODO: Does JRUBY need to skip this test
293
+ return if defined? JRUBY_VERSION
294
+
295
+ options = { name: "Pfand_from_windows_phone", format: :excelx }
296
+ with_each_spreadsheet(options) do |workbook|
297
+ workbook.default_sheet = workbook.sheets.first
298
+ assert_equal ["Blatt1", "Blatt2", "Blatt3"], workbook.sheets
299
+ assert_equal "Summe", workbook.cell("b", 1)
300
+
301
+ assert_equal Date.new(2011, 9, 14), workbook.cell("a", 2)
302
+ assert_equal :date, workbook.celltype("a", 2)
303
+ assert_equal Date.new(2011, 9, 15), workbook.cell("a", 3)
304
+ assert_equal :date, workbook.celltype("a", 3)
305
+
306
+ assert_equal 3.81, workbook.cell("b", 2)
307
+ assert_equal "SUM(C2:L2)", workbook.formula("b", 2)
308
+ assert_equal 0.7, workbook.cell("c", 2)
309
+ end # each
310
+ end
311
+
312
+ def test_excelx_links
313
+ with_each_spreadsheet(name: "link", format: :excelx) do |workbook|
314
+ assert_equal "Google", workbook.cell(1, 1)
315
+ assert_equal "http://www.google.com", workbook.cell(1, 1).href
316
+ end
317
+ end
318
+
319
+ def test_handles_link_without_hyperlink
320
+ workbook = Roo::Spreadsheet.open(File.join(TESTDIR, "bad_link.xlsx"))
321
+ assert_equal "Test", workbook.cell(1, 1)
322
+ end
323
+
324
+ # Excel has two base date formats one from 1900 and the other from 1904.
325
+ # see #test_base_dates_in_excel
326
+ def test_base_dates_in_excelx
327
+ with_each_spreadsheet(name: "1900_base", format: :excelx) do |workbook|
328
+ assert_equal Date.new(2009, 06, 15), workbook.cell(1, 1)
329
+ assert_equal :date, workbook.celltype(1, 1)
330
+ end
331
+ with_each_spreadsheet(name: "1904_base", format: :excelx) do |workbook|
332
+ assert_equal Date.new(2009, 06, 15), workbook.cell(1, 1)
333
+ assert_equal :date, workbook.celltype(1, 1)
334
+ end
335
+ end
336
+
337
+ def test_parsing_xlsx_with_richtext
338
+ xlsx = roo_class.new(File.join(TESTDIR, "richtext_example.xlsx"))
339
+
340
+ assert_equal "Example richtext", xlsx.cell("a", 1)
341
+ assert_equal "Example richtext", xlsx.cell("b", 1)
342
+ end
343
+
344
+ def test_implicit_coordinates
345
+ xlsx = roo_class.new(File.join(TESTDIR, 'implicit_coordinates.xlsx'))
346
+
347
+ assert_equal 'Test', xlsx.cell('a', 1)
348
+ assert_equal 'A2', xlsx.cell('a', 2)
349
+ assert_equal 'B2', xlsx.cell(2, 2)
350
+ assert_equal 'C2', xlsx.cell('c', 2)
351
+ end
352
+
353
+ def roo_class
354
+ Roo::Excelx
355
+ end
356
+
357
+ def filename(name)
358
+ "#{name}.xlsx"
359
+ end
360
+ end
@@ -0,0 +1,9 @@
1
+ require "test_helper"
2
+
3
+ class TestRooOpenOffice < Minitest::Test
4
+ def test_libre_office
5
+ oo = Roo::LibreOffice.new(File.join(TESTDIR, "numbers1.ods"))
6
+ oo.default_sheet = oo.sheets.first
7
+ assert_equal 41, oo.cell("a", 12)
8
+ end
9
+ end
@@ -0,0 +1,289 @@
1
+ # encoding: utf-8
2
+ require "test_helper"
3
+
4
+ class TestRooOpenOffice < Minitest::Test
5
+ def test_openoffice_download_uri_and_zipped
6
+ port = 12_345
7
+ file = "rata.ods.zip"
8
+ start_local_server(file, port) do
9
+ url = "#{local_server(port)}/#{file}"
10
+
11
+ workbook = roo_class.new(url, packed: :zip)
12
+ assert_in_delta 0.001, 505.14, workbook.cell("c", 33).to_f
13
+ end
14
+ end
15
+
16
+ def test_download_uri_with_invalid_host
17
+ assert_raises(RuntimeError) do
18
+ roo_class.new("http://examples.com/file.ods")
19
+ end
20
+ end
21
+
22
+ def test_download_uri_with_query_string
23
+ file = filename("simple_spreadsheet")
24
+ port = 12_346
25
+ url = "#{local_server(port)}/#{file}?query-param=value"
26
+ start_local_server(file, port) do
27
+ spreadsheet = roo_class.new(url)
28
+ assert_equal "Task 1", spreadsheet.cell("f", 4)
29
+ end
30
+ end
31
+
32
+ def test_openoffice_zipped
33
+ workbook = roo_class.new(File.join(TESTDIR, "bode-v1.ods.zip"), packed: :zip)
34
+ assert workbook
35
+ assert_equal 'ist "e" im Nenner von H(s)', workbook.cell("b", 5)
36
+ end
37
+
38
+ def test_should_raise_file_not_found_error
39
+ assert_raises(IOError) do
40
+ roo_class.new(File.join("testnichtvorhanden", "Bibelbund.ods"))
41
+ end
42
+ end
43
+
44
+ def test_file_warning_default_is_error
45
+ expected_message = "test/files/numbers1.xls is not an openoffice spreadsheet"
46
+ assert_raises(TypeError, expected_message) do
47
+ roo_class.new(File.join(TESTDIR, "numbers1.xls"))
48
+ end
49
+
50
+ assert_raises(TypeError) do
51
+ roo_class.new(File.join(TESTDIR, "numbers1.xlsx"))
52
+ end
53
+ end
54
+
55
+ def test_file_warning_error
56
+ options = { packed: false, file_warning: :error }
57
+
58
+ assert_raises(TypeError) do
59
+ roo_class.new(File.join(TESTDIR, "numbers1.xls"), options)
60
+ end
61
+
62
+ assert_raises(TypeError) do
63
+ roo_class.new(File.join(TESTDIR, "numbers1.xlsx"), options)
64
+ end
65
+ end
66
+
67
+ def test_file_warning_warning
68
+ assert_raises(ArgumentError) do
69
+ options = { packed: false, file_warning: :warning }
70
+ roo_class.new(File.join(TESTDIR, "numbers1.xlsx"), options)
71
+ end
72
+ end
73
+
74
+ def test_file_warning_ignore
75
+ options = { packed: false, file_warning: :ignore }
76
+ assert roo_class.new(File.join(TESTDIR, "type_openoffice.xlsx"), options), "Should not throw an error"
77
+ end
78
+
79
+ def test_encrypted_file
80
+ workbook = roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "letmein")
81
+ assert_equal "Hello World", workbook.cell("a", 1)
82
+ end
83
+
84
+ def test_encrypted_file_requires_password
85
+ assert_raises(ArgumentError) do
86
+ roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"))
87
+ end
88
+ end
89
+
90
+ def test_encrypted_file_with_incorrect_password
91
+ assert_raises(ArgumentError) do
92
+ roo_class.new(File.join(TESTDIR, "encrypted-letmein.ods"), password: "badpassword")
93
+ end
94
+ end
95
+
96
+ # 2011-08-11
97
+ def test_bug_openoffice_formula_missing_letters
98
+ # NOTE: This document was created using LibreOffice. The formulas seem
99
+ # different from a document created using OpenOffice.
100
+ #
101
+ # TODO: translate
102
+ # Bei den OpenOffice-Dateien ist in diesem Feld in der XML-
103
+ # Datei of: als Prefix enthalten, waehrend in dieser Datei
104
+ # irgendetwas mit oooc: als Prefix verwendet wird.
105
+ workbook = roo_class.new(File.join(TESTDIR, "dreimalvier.ods"))
106
+ assert_equal "=SUM([.A1:.D1])", workbook.formula("e", 1)
107
+ assert_equal "=SUM([.A2:.D2])", workbook.formula("e", 2)
108
+ assert_equal "=SUM([.A3:.D3])", workbook.formula("e", 3)
109
+ expected_formulas = [
110
+ [1, 5, "=SUM([.A1:.D1])"],
111
+ [2, 5, "=SUM([.A2:.D2])"],
112
+ [3, 5, "=SUM([.A3:.D3])"],
113
+ ]
114
+ assert_equal expected_formulas, workbook.formulas
115
+ end
116
+
117
+ def test_header_with_brackets_open_office
118
+ options = { name: "advanced_header", format: :openoffice }
119
+ with_each_spreadsheet(options) do |workbook|
120
+ parsed_head = workbook.parse(headers: true)
121
+ assert_equal "Date(yyyy-mm-dd)", workbook.cell("A", 1)
122
+ assert_equal parsed_head[0].keys, ["Date(yyyy-mm-dd)"]
123
+ assert_equal parsed_head[0].values, ["Date(yyyy-mm-dd)"]
124
+ end
125
+ end
126
+
127
+ def test_office_version
128
+ with_each_spreadsheet(name: "numbers1", format: :openoffice) do |workbook|
129
+ assert_equal "1.0", workbook.officeversion
130
+ end
131
+ end
132
+
133
+ def test_bug_contiguous_cells
134
+ with_each_spreadsheet(name: "numbers1", format: :openoffice) do |workbook|
135
+ workbook.default_sheet = "Sheet4"
136
+ assert_equal Date.new(2007, 06, 16), workbook.cell("a", 1)
137
+ assert_equal 10, workbook.cell("b", 1)
138
+ assert_equal 10, workbook.cell("c", 1)
139
+ assert_equal 10, workbook.cell("d", 1)
140
+ assert_equal 10, workbook.cell("e", 1)
141
+ end
142
+ end
143
+
144
+ def test_italo_table
145
+ with_each_spreadsheet(name: "simple_spreadsheet_from_italo", format: :openoffice) do |workbook|
146
+ assert_equal "1", workbook.cell("A", 1)
147
+ assert_equal "1", workbook.cell("B", 1)
148
+ assert_equal "1", workbook.cell("C", 1)
149
+ assert_equal 1, workbook.cell("A", 2).to_i
150
+ assert_equal 2, workbook.cell("B", 2).to_i
151
+ assert_equal 1, workbook.cell("C", 2).to_i
152
+ assert_equal 1, workbook.cell("A", 3)
153
+ assert_equal 3, workbook.cell("B", 3)
154
+ assert_equal 1, workbook.cell("C", 3)
155
+ assert_equal "A", workbook.cell("A", 4)
156
+ assert_equal "A", workbook.cell("B", 4)
157
+ assert_equal "A", workbook.cell("C", 4)
158
+ assert_equal 0.01, workbook.cell("A", 5)
159
+ assert_equal 0.01, workbook.cell("B", 5)
160
+ assert_equal 0.01, workbook.cell("C", 5)
161
+ assert_equal 0.03, workbook.cell("a", 5) + workbook.cell("b", 5) + workbook.cell("c", 5)
162
+
163
+ # Cells values in row 1:
164
+ assert_equal "1:string", [workbook.cell(1, 1), workbook.celltype(1, 1)].join(":")
165
+ assert_equal "1:string", [workbook.cell(1, 2), workbook.celltype(1, 2)].join(":")
166
+ assert_equal "1:string", [workbook.cell(1, 3), workbook.celltype(1, 3)].join(":")
167
+
168
+ # Cells values in row 2:
169
+ assert_equal "1:string", [workbook.cell(2, 1), workbook.celltype(2, 1)].join(":")
170
+ assert_equal "2:string", [workbook.cell(2, 2), workbook.celltype(2, 2)].join(":")
171
+ assert_equal "1:string", [workbook.cell(2, 3), workbook.celltype(2, 3)].join(":")
172
+
173
+ # Cells values in row 3:
174
+ assert_equal "1:float", [workbook.cell(3, 1), workbook.celltype(3, 1)].join(":")
175
+ assert_equal "3:float", [workbook.cell(3, 2), workbook.celltype(3, 2)].join(":")
176
+ assert_equal "1:float", [workbook.cell(3, 3), workbook.celltype(3, 3)].join(":")
177
+
178
+ # Cells values in row 4:
179
+ assert_equal "A:string", [workbook.cell(4, 1), workbook.celltype(4, 1)].join(":")
180
+ assert_equal "A:string", [workbook.cell(4, 2), workbook.celltype(4, 2)].join(":")
181
+ assert_equal "A:string", [workbook.cell(4, 3), workbook.celltype(4, 3)].join(":")
182
+
183
+ # Cells values in row 5:
184
+ assert_equal "0.01:percentage", [workbook.cell(5, 1), workbook.celltype(5, 1)].join(":")
185
+ assert_equal "0.01:percentage", [workbook.cell(5, 2), workbook.celltype(5, 2)].join(":")
186
+ assert_equal "0.01:percentage", [workbook.cell(5, 3), workbook.celltype(5, 3)].join(":")
187
+ end
188
+ end
189
+
190
+ def test_formula_openoffice
191
+ with_each_spreadsheet(name: "formula", format: :openoffice) do |workbook|
192
+ assert_equal 1, workbook.cell("A", 1)
193
+ assert_equal 2, workbook.cell("A", 2)
194
+ assert_equal 3, workbook.cell("A", 3)
195
+ assert_equal 4, workbook.cell("A", 4)
196
+ assert_equal 5, workbook.cell("A", 5)
197
+ assert_equal 6, workbook.cell("A", 6)
198
+ assert_equal 21, workbook.cell("A", 7)
199
+ assert_equal :formula, workbook.celltype("A", 7)
200
+ assert_equal "=[Sheet2.A1]", workbook.formula("C", 7)
201
+ assert_nil workbook.formula("A", 6)
202
+ expected_formulas = [
203
+ [7, 1, "=SUM([.A1:.A6])"],
204
+ [7, 2, "=SUM([.$A$1:.B6])"],
205
+ [7, 3, "=[Sheet2.A1]"],
206
+ [8, 2, "=SUM([.$A$1:.B7])"],
207
+ ]
208
+ assert_equal expected_formulas, workbook.formulas(workbook.sheets.first)
209
+
210
+ # setting a cell
211
+ workbook.set("A", 15, 41)
212
+ assert_equal 41, workbook.cell("A", 15)
213
+ workbook.set("A", 16, "41")
214
+ assert_equal "41", workbook.cell("A", 16)
215
+ workbook.set("A", 17, 42.5)
216
+ assert_equal 42.5, workbook.cell("A", 17)
217
+ end
218
+ end
219
+
220
+ def test_bug_ric
221
+ with_each_spreadsheet(name: "ric", format: :openoffice) do |workbook|
222
+ assert workbook.empty?("A", 1)
223
+ assert workbook.empty?("B", 1)
224
+ assert workbook.empty?("C", 1)
225
+ assert workbook.empty?("D", 1)
226
+ expected = 1
227
+ letter = "e"
228
+ while letter <= "u"
229
+ assert_equal expected, workbook.cell(letter, 1)
230
+ letter.succ!
231
+ expected += 1
232
+ end
233
+ assert_equal "J", workbook.cell("v", 1)
234
+ assert_equal "P", workbook.cell("w", 1)
235
+ assert_equal "B", workbook.cell("x", 1)
236
+ assert_equal "All", workbook.cell("y", 1)
237
+ assert_equal 0, workbook.cell("a", 2)
238
+ assert workbook.empty?("b", 2)
239
+ assert workbook.empty?("c", 2)
240
+ assert workbook.empty?("d", 2)
241
+ assert_equal "B", workbook.cell("e", 2)
242
+ assert_equal "B", workbook.cell("f", 2)
243
+ assert_equal "B", workbook.cell("g", 2)
244
+ assert_equal "B", workbook.cell("h", 2)
245
+ assert_equal "B", workbook.cell("i", 2)
246
+ assert_equal "B", workbook.cell("j", 2)
247
+ assert_equal "B", workbook.cell("k", 2)
248
+ assert_equal "B", workbook.cell("l", 2)
249
+ assert_equal "B", workbook.cell("m", 2)
250
+ assert_equal "B", workbook.cell("n", 2)
251
+ assert_equal "B", workbook.cell("o", 2)
252
+ assert_equal "B", workbook.cell("p", 2)
253
+ assert_equal "B", workbook.cell("q", 2)
254
+ assert_equal "B", workbook.cell("r", 2)
255
+ assert_equal "B", workbook.cell("s", 2)
256
+ assert workbook.empty?("t", 2)
257
+ assert workbook.empty?("u", 2)
258
+ assert_equal 0, workbook.cell("v", 2)
259
+ assert_equal 0, workbook.cell("w", 2)
260
+ assert_equal 15, workbook.cell("x", 2)
261
+ assert_equal 15, workbook.cell("y", 2)
262
+ end
263
+ end
264
+
265
+ def test_mehrteilig
266
+ with_each_spreadsheet(name: "Bibelbund1", format: :openoffice) do |workbook|
267
+ assert_equal "Tagebuch des Sekret\303\244rs. Letzte Tagung 15./16.11.75 Schweiz", workbook.cell(45, "A")
268
+ end
269
+ end
270
+
271
+ def test_cell_openoffice_html_escape
272
+ with_each_spreadsheet(name: "html-escape", format: :openoffice) do |workbook|
273
+ assert_equal "'", workbook.cell(1, 1)
274
+ assert_equal "&", workbook.cell(2, 1)
275
+ assert_equal ">", workbook.cell(3, 1)
276
+ assert_equal "<", workbook.cell(4, 1)
277
+ assert_equal "`", workbook.cell(5, 1)
278
+ # test_openoffice_zipped will catch issues with &quot;
279
+ end
280
+ end
281
+
282
+ def roo_class
283
+ Roo::OpenOffice
284
+ end
285
+
286
+ def filename(name)
287
+ "#{name}.ods"
288
+ end
289
+ end