@@ -15,10 +15,10 @@ def initialize(book, drawing_filepath)
1515 @drawings_rels = [ ]
1616 @images_pathnames = Hash . new { |hash , key | hash [ key ] = [ ] }
1717
18- if file_exist? ( @drawing_filepath )
19- load_drawings_and_rels
20- load_images_pathnames_by_cells if has_images?
21- end
18+ return unless file_exist? ( @drawing_filepath )
19+
20+ load_drawings_and_rels
21+ load_images_pathnames_by_cells if has_images?
2222 end
2323
2424 ##
@@ -36,13 +36,11 @@ def images_at(cell_name)
3636 return if pathnames_at_coordinate . empty?
3737
3838 pathnames_at_coordinate . map do |image_pathname |
39- if image_pathname . exist?
40- image_pathname
41- else
39+ unless image_pathname . exist?
4240 excel_image_path = "xl/media#{ image_pathname . to_path . split ( tmpdir ) . last } "
4341 IO . copy_stream ( @book . files . file . open ( excel_image_path ) , image_pathname . to_path )
44- image_pathname
45- end
42+ end
43+ image_pathname
4644 end
4745 end
4846
@@ -52,8 +50,8 @@ def images_at(cell_name)
5250 # Transforms cell name to [row, col], e.g. A1 => [0, 0], B3 => [1, 2]
5351 # Rows and cols start with 0.
5452 def calc_coordinate ( cell_name )
55- col = COLUMNS . index ( cell_name . slice /[A-Z]+/ )
56- row = ( cell_name . slice /\d +/ ) . to_i - 1 # rows in drawings start with 0
53+ col = COLUMNS . index ( cell_name . slice ( /[A-Z]+/ ) )
54+ row = cell_name . slice ( /\d +/ ) . to_i - 1 # rows in drawings start with 0
5755 [ row , col ]
5856 end
5957
@@ -68,7 +66,7 @@ def tmpdir
6866 # Drawing xml contains relationships ID's and coordinates (row, col).
6967 # Drawing relationships xml contains images' locations.
7068 def load_drawings_and_rels
71- @drawings = parse_xml ( @drawing_filepath ) . css ( 'xdr|twoCellAnchor' , 'xdr|oneCellAnchor' )
69+ @drawings = parse_xml ( @drawing_filepath ) . css ( 'xdr|twoCellAnchor' , 'xdr|oneCellAnchor' )
7270 drawing_rels_filepath = expand_to_rels_path ( @drawing_filepath )
7371 @drawings_rels = parse_xml ( drawing_rels_filepath ) . css ( 'Relationships' )
7472 end
@@ -78,11 +76,11 @@ def load_drawings_and_rels
7876 # As multiple images can be located in a single cell, hash values are array of Pathname objects.
7977 # One image can be spread across multiple cells (defined with from-row/to-row/from-col/to-col attributes) - same Pathname object is associated to each row-col combination for the range.
8078 def load_images_pathnames_by_cells
81- image_selector = 'xdr:pic/xdr:blipFill/a:blip' . freeze
82- row_from_selector = 'xdr:from/xdr:row' . freeze
83- row_to_selector = 'xdr:to/xdr:row' . freeze
84- col_from_selector = 'xdr:from/xdr:col' . freeze
85- col_to_selector = 'xdr:to/xdr:col' . freeze
79+ image_selector = 'xdr:pic/xdr:blipFill/a:blip'
80+ row_from_selector = 'xdr:from/xdr:row'
81+ row_to_selector = 'xdr:to/xdr:row'
82+ col_from_selector = 'xdr:from/xdr:col'
83+ col_to_selector = 'xdr:to/xdr:col'
8684
8785 @drawings . xpath ( '//xdr:twoCellAnchor' , '//xdr:oneCellAnchor' ) . each do |drawing |
8886 # embed = drawing.xpath(image_selector).first.attributes['embed']
@@ -91,13 +89,13 @@ def load_images_pathnames_by_cells
9189 next if embed . nil?
9290
9391 rid = embed . value
94- path = Pathname . new ( "#{ tmpdir } /#{ extract_drawing_path ( rid ) . slice ( /[^ \ / ]*$/ ) } " )
92+ path = Pathname . new ( "#{ tmpdir } /#{ extract_drawing_path ( rid ) . slice ( %r{[^ /]*$} ) } " )
9593
9694 row_from = drawing . xpath ( row_from_selector ) . text . to_i
9795 col_from = drawing . xpath ( col_from_selector ) . text . to_i
9896
9997 if drawing . name == 'oneCellAnchor'
100- @images_pathnames [ [ row_from , col_from ] ] . push ( path )
98+ @images_pathnames [ [ row_from , col_from ] ] . push ( path )
10199 else
102100 row_to = drawing . xpath ( row_to_selector ) . text . to_i
103101 col_to = drawing . xpath ( col_to_selector ) . text . to_i
0 commit comments