diff options
author | Unit 193 <unit193@ubuntu.com> | 2019-02-01 19:22:38 -0500 |
---|---|---|
committer | Unit 193 <unit193@ubuntu.com> | 2019-02-01 19:22:38 -0500 |
commit | 46eadd2724cd841328d90c3143a485fcdf423ed6 (patch) | |
tree | eb23c011f1c80a4cce169d9d9523a28927770b12 | |
parent | 16dd018ca735392c2fb28b986316a9b22a06ccf9 (diff) | |
download | ruby-roo-46eadd2724cd841328d90c3143a485fcdf423ed6.tar.bz2 ruby-roo-46eadd2724cd841328d90c3143a485fcdf423ed6.tar.xz ruby-roo-46eadd2724cd841328d90c3143a485fcdf423ed6.tar.zst |
New upstream version 2.8.2upstream/2.8.2
-rw-r--r-- | CHANGELOG.md | 8 | ||||
-rw-r--r-- | lib/roo/excelx/relationships.rb | 10 | ||||
-rwxr-xr-x | lib/roo/excelx/sheet_doc.rb | 7 | ||||
-rw-r--r-- | lib/roo/utils.rb | 23 | ||||
-rw-r--r-- | lib/roo/version.rb | 2 | ||||
-rw-r--r-- | spec/lib/roo/excelx/relationships_spec.rb | 43 | ||||
-rwxr-xr-x | spec/lib/roo/excelx_spec.rb | 25 | ||||
-rw-r--r-- | spec/lib/roo/utils_spec.rb | 13 | ||||
-rw-r--r-- | test/files/bad_link.xlsx | bin | 0 -> 8870 bytes | |||
-rw-r--r-- | test/files/cell-range-link.xlsx | bin | 0 -> 9508 bytes | |||
-rw-r--r-- | test/roo/test_excelx.rb | 5 |
11 files changed, 131 insertions, 5 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index c0c4a39..fb1734a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ ## Unreleased +## [2.8.2] 2019-02-01 +### Changed/Added +- Support range cell for Excelx's links [490](https://github.com/roo-rb/roo/pull/490) +- Skip `extract_hyperlinks` if not required [488](https://github.com/roo-rb/roo/pull/488) + +### Fixed +- Fixed error for invalid link [492](https://github.com/roo-rb/roo/pull/492) + ## [2.8.1] 2019-01-21 ### Fixed - Fixed error if excelx's cell have empty children [487](https://github.com/roo-rb/roo/pull/487) diff --git a/lib/roo/excelx/relationships.rb b/lib/roo/excelx/relationships.rb index 19f9919..754775d 100644 --- a/lib/roo/excelx/relationships.rb +++ b/lib/roo/excelx/relationships.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'roo/excelx/extractor' module Roo @@ -11,10 +13,16 @@ module Roo @relationships ||= extract_relationships end + def include_type?(type) + to_a.any? do |_, rel| + rel["Type"]&.include? type + end + end + private def extract_relationships - return [] unless doc_exists? + return {} unless doc_exists? doc.xpath('/Relationships/Relationship').each_with_object({}) do |rel, hash| hash[rel['Id']] = rel diff --git a/lib/roo/excelx/sheet_doc.rb b/lib/roo/excelx/sheet_doc.rb index 2b3aa39..6da4c26 100755 --- a/lib/roo/excelx/sheet_doc.rb +++ b/lib/roo/excelx/sheet_doc.rb @@ -22,7 +22,7 @@ module Roo def hyperlinks(relationships) # If you're sure you're not going to need this hyperlinks you can discard it - @hyperlinks ||= if @options[:no_hyperlinks] + @hyperlinks ||= if @options[:no_hyperlinks] || !relationships.include_type?("hyperlink") {} else extract_hyperlinks(relationships) @@ -185,7 +185,10 @@ module Roo if relationship = relationships[hyperlink['id']] target_link = relationship['Target'] target_link += "##{hyperlink['location']}" if hyperlink['location'] - hash[::Roo::Utils.ref_to_key(hyperlink["ref"].to_s)] = target_link + + Roo::Utils.coordinates_in_range(hyperlink["ref"].to_s) do |coord| + hash[coord] = target_link + end end end end diff --git a/lib/roo/utils.rb b/lib/roo/utils.rb index dcaedde..2d6754a 100644 --- a/lib/roo/utils.rb +++ b/lib/roo/utils.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Roo module Utils extend self @@ -41,7 +43,7 @@ module Roo # convert a number to something like 'AB' (1 => 'A', 2 => 'B', ...) def number_to_letter(num) - result = "" + result = +"" until num.zero? num, index = (num - 1).divmod(26) @@ -73,6 +75,25 @@ module Roo (x2 - (x1 - 1)) * (y2 - (y1 - 1)) end + def coordinates_in_range(str) + return to_enum(:coordinates_in_range, str) unless block_given? + coordinates = str.split(":", 2).map! { |s| extract_coordinate s } + + case coordinates.size + when 1 + yield coordinates[0] + when 2 + tl, br = coordinates + rows = tl.row..br.row + cols = tl.column..br.column + rows.each do |row| + cols.each do |column| + yield Excelx::Coordinate.new(row, column) + end + end + end + end + def load_xml(path) ::File.open(path, 'rb') do |file| ::Nokogiri::XML(file) diff --git a/lib/roo/version.rb b/lib/roo/version.rb index 625e495..1f2140b 100644 --- a/lib/roo/version.rb +++ b/lib/roo/version.rb @@ -1,3 +1,3 @@ module Roo - VERSION = "2.8.1" + VERSION = "2.8.2" end diff --git a/spec/lib/roo/excelx/relationships_spec.rb b/spec/lib/roo/excelx/relationships_spec.rb new file mode 100644 index 0000000..6171d8f --- /dev/null +++ b/spec/lib/roo/excelx/relationships_spec.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +require "spec_helper" + +describe Roo::Excelx::Relationships do + subject(:relationships) { Roo::Excelx::Relationships.new Roo::Excelx.new(path).rels_files[0] } + + describe "#include_type?" do + [ + ["with hyperlink type", "test/files/link.xlsx", true, false], + ["with nil path", "test/files/Bibelbund.xlsx", false, false], + ["with comments type", "test/files/comments-google.xlsx", false, true], + ].each do |context_desc, file_path, hyperlink_value, comments_value| + context context_desc do + let(:path) { file_path } + + it "should return #{hyperlink_value} for hyperlink" do + expect(subject.include_type?("hyperlink")).to be hyperlink_value + end + + it "should return #{hyperlink_value} for link" do + expect(subject.include_type?("link")).to be hyperlink_value + end + + it "should return false for hypelink" do + expect(subject.include_type?("hypelink")).to be false + end + + it "should return false for coment" do + expect(subject.include_type?("coment")).to be false + end + + it "should return #{comments_value} for comments" do + expect(subject.include_type?("comments")).to be comments_value + end + + it "should return #{comments_value} for comment" do + expect(subject.include_type?("comment")).to be comments_value + end + end + end + end +end diff --git a/spec/lib/roo/excelx_spec.rb b/spec/lib/roo/excelx_spec.rb index 6c2289f..1b67a4d 100755 --- a/spec/lib/roo/excelx_spec.rb +++ b/spec/lib/roo/excelx_spec.rb @@ -379,9 +379,34 @@ describe Roo::Excelx do expect(subject.hyperlink?(1, 1)).to eq true expect(subject.hyperlink?(1, 2)).to eq false end + + context 'defined on cell range' do + let(:path) { 'test/files/cell-range-link.xlsx' } + + it 'returns the expected result' do + [[false]*3, *[[true, true, false]]*4, [false]*3].each.with_index(1) do |row, row_index| + row.each.with_index(1) do |value, col_index| + expect(subject.hyperlink?(row_index, col_index)).to eq(value) + end + end + end + end end describe '#hyperlink' do + context 'defined on cell range' do + let(:path) { 'test/files/cell-range-link.xlsx' } + + it 'returns the expected result' do + link = "http://www.google.com" + [[nil]*3, *[[link, link, nil]]*4, [nil]*3].each.with_index(1) do |row, row_index| + row.each.with_index(1) do |value, col_index| + expect(subject.hyperlink(row_index, col_index)).to eq(value) + end + end + end + end + context 'without location' do let(:path) { 'test/files/link.xlsx' } diff --git a/spec/lib/roo/utils_spec.rb b/spec/lib/roo/utils_spec.rb index 8f322d4..c000ae7 100644 --- a/spec/lib/roo/utils_spec.rb +++ b/spec/lib/roo/utils_spec.rb @@ -90,6 +90,19 @@ RSpec.describe ::Roo::Utils do end end + context '.coordinates_in_range' do + it "returns the expected result" do + expect(described_class.coordinates_in_range('').to_a).to eq [] + expect(described_class.coordinates_in_range('B2').to_a).to eq [[2, 2]] + expect(described_class.coordinates_in_range('D2:G3').to_a).to eq [[2, 4], [2, 5], [2, 6], [2, 7], [3, 4], [3, 5], [3, 6], [3, 7]] + expect(described_class.coordinates_in_range('G3:D2').to_a).to eq [] + end + + it "raises an error when appropriate" do + expect { described_class.coordinates_in_range('D2:G3:I5').to_a }.to raise_error(ArgumentError) + end + end + context '.load_xml' do it 'returns the expected result' do expect(described_class.load_xml('test/files/sheet1.xml')).to be_a(Nokogiri::XML::Document) diff --git a/test/files/bad_link.xlsx b/test/files/bad_link.xlsx Binary files differnew file mode 100644 index 0000000..30eaa0b --- /dev/null +++ b/test/files/bad_link.xlsx diff --git a/test/files/cell-range-link.xlsx b/test/files/cell-range-link.xlsx Binary files differnew file mode 100644 index 0000000..50bc067 --- /dev/null +++ b/test/files/cell-range-link.xlsx diff --git a/test/roo/test_excelx.rb b/test/roo/test_excelx.rb index 75ecfa2..9d460af 100644 --- a/test/roo/test_excelx.rb +++ b/test/roo/test_excelx.rb @@ -302,6 +302,11 @@ class TestRworkbookExcelx < Minitest::Test end end + def test_handles_link_without_hyperlink + workbook = Roo::Spreadsheet.open(File.join(TESTDIR, "bad_link.xlsx")) + assert_equal "Test", workbook.cell(1, 1) + end + # Excel has two base date formats one from 1900 and the other from 1904. # see #test_base_dates_in_excel def test_base_dates_in_excelx |