-
Notifications
You must be signed in to change notification settings - Fork 505
Fix/image order #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Fix/image order #497
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for opening this PR. I think we should handle mapping logic in Roo::Excelx::Sheet
and Roo::Excelx::Relationships
.
-
Roo::Excelx::Relationships
: Add new method to filter based on type. Similar toroo/lib/roo/excelx/relationships.rb
Lines 16 to 20 in 4ec1104
def include_type?(type) to_a.any? do |_, rel| rel["Type"]&.include? type end end -
Roo::Excelx::Sheet
: Addimages
method to lazy load followingLine 15 in 4ec1104
@images = Images.new(image_rels[sheet_index]).list @rels
-
Move kangaroos.xlsx file to test/files folder. As we ignore
test/files
while building gem to reduce size.Line 17 in 4ec1104
spec.files.reject! { |fn| fn.include?('test/files') }
test/image_order/test_image_order.rb
Outdated
@@ -0,0 +1,11 @@ | |||
require "test_helper" | |||
require 'pry' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this line
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
will do, thanks! |
- created a method that returns "Target" attribute based on passed type - created "images" method on sheet class, that resolves the correct index by said "Target" attribute and regex - moved excel file to test/files - changed test file name because this is about indices of images, not ordering
I fixed the code! But I want to elaborate test cases a little bit more when I get to office(I don't have Excel installed on any of my cumputers thus cannot modify excel files at home). Any test scenarios do you have in your mind? |
lib/roo/excelx/relationships.rb
Outdated
@@ -19,6 +19,12 @@ def include_type?(type) | |||
end | |||
end | |||
|
|||
def target(type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
target
is a confusing name for what this method is doing.
We also need relevant test cases for the newly added method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method name should signify that we are returning target(s) while filtering relations on Type attribute. If it doesn't signify the above then it is confusing in nature.
@@ -96,6 +93,16 @@ def dimensions | |||
@sheet.dimensions | |||
end | |||
|
|||
def images |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This method should be memoized.
def images
@images ||= begin
...
end
end
lib/roo/excelx/sheet.rb
Outdated
def images | ||
target = @rels.target("drawing") | ||
match = /[a-zA-Z]+([0-9]+).xml/.match target | ||
if match |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this condition? Images class already handles nil as file_path
lib/roo/excelx.rb
Outdated
@@ -455,7 +455,6 @@ def process_zipfile_entries(entries) | |||
nr = Regexp.last_match[1].to_i | |||
image_rels[nr - 1] = "#{@tmpdir}/roo_image_rels#{nr}" | |||
end | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert unnecessary cosmetic changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry about that
- sort the images hash because its order was messed up too - added new test case which tests the said order
Fixed! But I have few questions. I just noticed the order of image resources are like this if you have multiple images:
It looks like it's random and I thought we'd better sort it by its number, so I coded just so it does.
I bumped into this problem in my job and sorted images based on its names outside of roo. And I'd love to write some tests on Thanks! |
hello? |
@rels.filter_by_type(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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if it fails?
Summary
If there is a sheet without any images attached, indices for image files are messed up and crammed.
I had to have a proper sheet indices as a hash and later refer to it to correctly resolve the sheet that the image belongs.
My code might look wonky but it works!
here's the rake test result:
my evnviroinments are:
ruby version:
Microsoft excel version:
Other Information
I attached an excel file that contains 2 kangaroos on sheet 1 and 3.
If you run it with older code, it tells you that sheet 1 and 2 have kangaroos.