Skip to content

Commit

Permalink
Merge 46a4280 into 4ec1104
Browse files Browse the repository at this point in the history
  • Loading branch information
knzwats committed Mar 14, 2019
2 parents 4ec1104 + 46a4280 commit 7a829bd
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
8 changes: 8 additions & 0 deletions lib/roo/excelx/relationships.rb
Expand Up @@ -19,6 +19,14 @@ def include_type?(type)
end
end

def targets(type: "")
return to_enum(__method__, type: type) unless block_given?

to_a.each do |_, rel|
yield rel["Target"] if rel["Type"]&.include? type
end
end

private

def extract_relationships
Expand Down
12 changes: 9 additions & 3 deletions lib/roo/excelx/sheet.rb
Expand Up @@ -6,13 +6,10 @@ class Sheet

delegate [:styles, :workbook, :shared_strings, :rels_files, :sheet_files, :comments_files, :image_rels] => :@shared

attr_reader :images

def initialize(name, shared, sheet_index, options = {})
@name = name
@shared = shared
@sheet_index = sheet_index
@images = Images.new(image_rels[sheet_index]).list
@rels = Relationships.new(rels_files[sheet_index])
@comments = Comments.new(comments_files[sheet_index])
@sheet = SheetDoc.new(sheet_files[sheet_index], @rels, shared, options)
Expand Down Expand Up @@ -96,6 +93,15 @@ def dimensions
@sheet.dimensions
end

def images
@images ||= begin
@rels.targets(type: "drawing").map do |target|
match = /[a-zA-Z]+([0-9]+).xml/.match target
Images.new(image_rels[match[1].to_i - 1]).list
end.reduce({}, :merge).sort.to_h
end
end

private

# Take an xml row and return an array of Excelx::Cell objects
Expand Down
20 changes: 20 additions & 0 deletions test/excelx/test_image_index.rb
@@ -0,0 +1,20 @@
require "test_helper"

class TestImageIndex < Minitest::Test
def test_number_of_images
with_each_spreadsheet(name: "kangaroos", format: [:excelx]) do |oo|
assert_equal 4, oo.sheet_for(0).images.size
assert_equal 0, oo.sheet_for(1).images.size
assert_equal 1, oo.sheet_for(2).images.size
end
end

def test_order_of_images
with_each_spreadsheet(name: "kangaroos", format: [:excelx]) do |oo|
expected = {"rId1"=>"roo_media_image1.jpeg", "rId2"=>"roo_media_image2.jpeg", "rId3"=>"roo_media_image3.jpeg", "rId4"=>"roo_media_image4.jpeg"}

assert_equal expected.keys, oo.sheet_for(0).images.keys
assert_equal expected.values, oo.sheet_for(0).images.values
end
end
end
Binary file added test/files/kangaroos.xlsx
Binary file not shown.

0 comments on commit 7a829bd

Please sign in to comment.