diff --git a/CHANGELOG.md b/CHANGELOG.md index a267798..481a0eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,7 @@ Squib follows [semantic versioning](http://semver.org). ## v0.6.0 / Unreleased Features: -* Upgraded roo (Excel parsing) to 2.0.0. Nothing major for Squib users, just keeping up with the times. +* Upgraded roo (Excel parsing) to 2.0.0. Nothing major for Squib users, just keeping up with the times. () Bugs: * Fixed global text hinting (#63) diff --git a/lib/squib/api/data.rb b/lib/squib/api/data.rb index e0f1e5c..2ef0abd 100644 --- a/lib/squib/api/data.rb +++ b/lib/squib/api/data.rb @@ -24,15 +24,15 @@ module Squib def xlsx(opts = {}) opts = Squib::SYSTEM_DEFAULTS.merge(opts) opts = Squib::InputHelpers.fileify(opts) - s = Roo::Excelx.new(opts[:file]) + s = Roo::Excelx.new(opts[:file]) s.default_sheet = s.sheets[opts[:sheet]] data = {} s.first_column.upto(s.last_column) do |col| header = s.cell(s.first_row,col).to_s data[header] = [] - (s.first_row+1).upto(s.last_row) do |row| + (s.first_row + 1).upto(s.last_row) do |row| cell = s.cell(row,col) - # Roo hack for avoiding unnecessary .0's on whole integers + # Roo hack for avoiding unnecessary .0's on whole integers (https://github.com/roo-rb/roo/issues/139) cell = s.excelx_value(row,col) if s.excelx_type(row,col) == [:numeric_or_formula, 'General'] data[header] << cell end#row diff --git a/spec/api/api_data_spec.rb b/spec/api/api_data_spec.rb index b90bd44..2519956 100644 --- a/spec/api/api_data_spec.rb +++ b/spec/api/api_data_spec.rb @@ -28,4 +28,23 @@ describe Squib::Deck do }) end end + + context '#xlsx' do + it 'loads basic xlsx data' do + expect(Squib.xlsx(file: xlsx_file('basic.xlsx'))).to eq({ + 'Name' => %w(Larry Curly Mo), + 'General Number' => %w(1 2 3), #general types always get loaded as strings with no conversion + 'Actual Number' => [4.0, 5.0, 6.0], #numbers get auto-converted to integers + }) + end + + it 'loads xlsx with formulas' do + expect(Squib.xlsx(file: xlsx_file('formulas.xlsx'))).to eq({ + 'A' => %w(1, 2), + 'B' => %w(3, 4), + 'Sum' => %w(4, 6), + }) + end + + end end \ No newline at end of file diff --git a/spec/data/xlsx/basic.xlsx b/spec/data/xlsx/basic.xlsx new file mode 100644 index 0000000..d37739f Binary files /dev/null and b/spec/data/xlsx/basic.xlsx differ diff --git a/spec/data/xlsx/formulas.xlsx b/spec/data/xlsx/formulas.xlsx new file mode 100644 index 0000000..fadc137 Binary files /dev/null and b/spec/data/xlsx/formulas.xlsx differ diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index ad075b8..8f2296f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -32,6 +32,10 @@ def csv_file(file) "#{File.expand_path(File.dirname(__FILE__))}/data/csv/#{file}" end +def xlsx_file(file) + "#{File.expand_path(File.dirname(__FILE__))}/data/xlsx/#{file}" +end + def overwrite_sample(sample_name, log) # Use this to overwrite the regression with current state File.open(sample_regression_file(sample_name), 'w+:UTF-8') do |f| diff --git a/squib.sublime-project b/squib.sublime-project index c6b3d03..e4926f2 100644 --- a/squib.sublime-project +++ b/squib.sublime-project @@ -43,6 +43,11 @@ "shell_cmd": "rspec spec/graphics/graphics_text_spec.rb", "working_dir": "${project_path:${folder}}" }, + { + "name": "rspec spec/api/api_data_spec.rb", + "shell_cmd": "rspec spec/api/api_data_spec.rb", + "working_dir": "${project_path:${folder}}" + }, ],