Skip to content

Commit 4c6b9fa

Browse files
authored
Support for parsing one cell anchored images (#107)
1 parent 494ed05 commit 4c6b9fa

File tree

4 files changed

+44
-7
lines changed

4 files changed

+44
-7
lines changed

lib/creek/drawing.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def tmpdir
6666
# Drawing xml contains relationships ID's and coordinates (row, col).
6767
# Drawing relationships xml contains images' locations.
6868
def load_drawings_and_rels
69-
@drawings = parse_xml(@drawing_filepath).css('xdr|twoCellAnchor')
69+
@drawings = parse_xml(@drawing_filepath).css('xdr|twoCellAnchor', 'xdr|oneCellAnchor' )
7070
drawing_rels_filepath = expand_to_rels_path(@drawing_filepath)
7171
@drawings_rels = parse_xml(drawing_rels_filepath).css('Relationships')
7272
end
@@ -82,7 +82,7 @@ def load_images_pathnames_by_cells
8282
col_from_selector = 'xdr:from/xdr:col'.freeze
8383
col_to_selector = 'xdr:to/xdr:col'.freeze
8484

85-
@drawings.xpath('//xdr:twoCellAnchor').each do |drawing|
85+
@drawings.xpath('//xdr:twoCellAnchor', '//xdr:oneCellAnchor').each do |drawing|
8686
# embed = drawing.xpath(image_selector).first.attributes['embed']
8787
temp = drawing.xpath(image_selector).first
8888
embed = temp.attributes['embed'] if temp
@@ -93,12 +93,17 @@ def load_images_pathnames_by_cells
9393

9494
row_from = drawing.xpath(row_from_selector).text.to_i
9595
col_from = drawing.xpath(col_from_selector).text.to_i
96-
row_to = drawing.xpath(row_to_selector).text.to_i
97-
col_to = drawing.xpath(col_to_selector).text.to_i
9896

99-
(col_from..col_to).each do |col|
100-
(row_from..row_to).each do |row|
101-
@images_pathnames[[row, col]].push(path)
97+
if drawing.name == 'oneCellAnchor'
98+
@images_pathnames[[row_from , col_from ]].push(path)
99+
else
100+
row_to = drawing.xpath(row_to_selector).text.to_i
101+
col_to = drawing.xpath(col_to_selector).text.to_i
102+
103+
(col_from..col_to).each do |col|
104+
(row_from..row_to).each do |row|
105+
@images_pathnames[[row, col]].push(path)
106+
end
102107
end
103108
end
104109
end

spec/drawing_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
describe 'drawing' do
44
let(:book) { Creek::Book.new('spec/fixtures/sample-with-images.xlsx') }
55
let(:book_no_images) { Creek::Book.new('spec/fixtures/sample.xlsx') }
6+
let(:book_with_one_cell_anchored_images) { Creek::Book.new('spec/fixtures/sample-with-one-cell-anchored-images.xlsx') }
67
let(:drawingfile) { 'xl/drawings/drawing1.xml' }
78
let(:drawing) { Creek::Drawing.new(book, drawingfile) }
89
let(:drawing_without_images) { Creek::Drawing.new(book_no_images, drawingfile) }
10+
let(:drawing_with_one_cell_anchored_images) { Creek::Drawing.new(book_with_one_cell_anchored_images, drawingfile) }
911

1012
describe '#has_images?' do
1113
it 'has' do
@@ -48,5 +50,21 @@
4850
expect(image1).to eq(image2)
4951
end
5052
end
53+
54+
context 'when one cell anchored images in cell' do
55+
it 'returns image for anchored cell' do
56+
image = drawing_with_one_cell_anchored_images.images_at('A2')[0]
57+
expect(image.class).to eq(Pathname)
58+
expect(image.exist?).to eq(true)
59+
end
60+
61+
it 'returns nil for non-anchored cell' do
62+
image = drawing_with_one_cell_anchored_images.images_at('A3')[0]
63+
# Image can be seen present on cell A4 in `sample-with-one-cell-anchored-images.xlsx`
64+
image_at_non_anchored_cell = drawing_with_one_cell_anchored_images.images_at('A4')
65+
expect(image.class).to eq(Pathname)
66+
expect(image_at_non_anchored_cell).to eq(nil)
67+
end
68+
end
5169
end
5270
end
244 KB
Binary file not shown.

spec/sheet_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ def load_cell(rows, cell_name)
6565
end
6666
end
6767

68+
context 'when one cell anchored images in cell' do
69+
let(:book_with_one_cell_anchored_images) { Creek::Book.new('spec/fixtures/sample-with-one-cell-anchored-images.xlsx') }
70+
let(:sheet_with_one_cell_anchored_images) { Creek::Sheet.new(book_with_one_cell_anchored_images, 'Sheet 1', 1, '', '', '1', sheetfile) }
71+
let(:rows) { sheet_with_one_cell_anchored_images.with_images.rows.map { |r| r } }
72+
73+
it 'returns image for anchored cell' do
74+
expect(load_cell(rows, 'A2').size).to eq(1)
75+
end
76+
77+
it 'returns nil for non-anchored cell' do
78+
expect(load_cell(rows, 'A4')).to eq(nil)
79+
end
80+
end
81+
6882
context 'with excel without images' do
6983
let(:book_no_images) { Creek::Book.new('spec/fixtures/sample.xlsx') }
7084
let(:sheet_no_images) { Creek::Sheet.new(book_no_images, 'Sheet 1', 1, '', '', '1', sheetfile) }

0 commit comments

Comments
 (0)