Skip to content

Commit

Permalink
Fix issue with images in hidden cells.
Browse files Browse the repository at this point in the history
Fix issue where images that started in hidden rows/cols weren't
placed correctly in the worksheet.
  • Loading branch information
jmcnamara committed Apr 7, 2019
1 parent f00ab0c commit e51eada
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/worksheet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1740,6 +1740,9 @@ _worksheet_size_col(lxw_worksheet *self, lxw_col_t col_num)
}

if (col_opt) {
if (col_opt->hidden)
return 0;

width = col_opt->width;

/* Convert to pixels. */
Expand Down Expand Up @@ -1775,6 +1778,9 @@ _worksheet_size_row(lxw_worksheet *self, lxw_row_t row_num)
row = lxw_worksheet_find_row(self, row_num);

if (row) {
if (row->hidden)
return 0;

height = row->height;

if (height == 0)
Expand Down Expand Up @@ -1901,23 +1907,30 @@ _worksheet_position_object_pixels(lxw_worksheet *self,
y_abs += y1;

/* Adjust start col for offsets that are greater than the col width. */
while (x1 >= _worksheet_size_col(self, col_start)) {
x1 -= _worksheet_size_col(self, col_start);
col_start++;
if (_worksheet_size_col(self, col_start) > 0) {
while (x1 >= _worksheet_size_col(self, col_start)) {
x1 -= _worksheet_size_col(self, col_start);
col_start++;
}
}

/* Adjust start row for offsets that are greater than the row height. */
while (y1 >= _worksheet_size_row(self, row_start)) {
y1 -= _worksheet_size_row(self, row_start);
row_start++;
if (_worksheet_size_row(self, row_start) > 0) {
while (y1 >= _worksheet_size_row(self, row_start)) {
y1 -= _worksheet_size_row(self, row_start);
row_start++;
}
}

/* Initialize end cell to the same as the start cell. */
col_end = col_start;
row_end = row_start;

width = width + x1;
height = height + y1;
/* Only offset the image in the cell if the row/col isn't hidden. */
if (_worksheet_size_col(self, col_start) > 0)
width = width + x1;
if (_worksheet_size_row(self, row_start) > 0)
height = height + y1;

/* Subtract the underlying cell widths to find the end cell. */
while (width >= _worksheet_size_col(self, col_end)) {
Expand Down
22 changes: 22 additions & 0 deletions test/functional/src/test_image44.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2019, John McNamara, jmcnamara@cpan.org
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = new_workbook("test_image44.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

worksheet_insert_image(worksheet, CELL("E9"), "images/red.png");

worksheet_set_row(worksheet, 8, 30, NULL);

return workbook_close(workbook);
}
23 changes: 23 additions & 0 deletions test/functional/src/test_image45.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2019, John McNamara, jmcnamara@cpan.org
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = new_workbook("test_image45.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

worksheet_insert_image(worksheet, CELL("E9"), "images/red.png");

lxw_row_col_options options = {.hidden = LXW_TRUE};
worksheet_set_row_opt(worksheet, 8, 30, NULL, &options);

return workbook_close(workbook);
}
24 changes: 24 additions & 0 deletions test/functional/src/test_image46.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2019, John McNamara, jmcnamara@cpan.org
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = new_workbook("test_image46.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

lxw_image_options image_options = {.x_offset = 0, .y_offset = 4};
worksheet_insert_image_opt(worksheet, CELL("E9"), "images/red.png", &image_options);

lxw_row_col_options row_options = {.hidden = LXW_TRUE};
worksheet_set_row_opt(worksheet, 8, 30, NULL, &row_options);

return workbook_close(workbook);
}
23 changes: 23 additions & 0 deletions test/functional/src/test_image47.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*****************************************************************************
* Test cases for libxlsxwriter.
*
* Test to compare output against Excel files.
*
* Copyright 2014-2019, John McNamara, jmcnamara@cpan.org
*
*/

#include "xlsxwriter.h"

int main() {

lxw_workbook *workbook = new_workbook("test_image47.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);

worksheet_insert_image(worksheet, CELL("E9"), "images/red.png");

lxw_row_col_options options = {.hidden = LXW_TRUE};
worksheet_set_row_opt(worksheet, 9, LXW_DEF_ROW_HEIGHT, NULL, &options);

return workbook_close(workbook);
}
12 changes: 12 additions & 0 deletions test/functional/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ def test_image35(self):
def test_image36(self):
self.run_exe_test('test_image36')

def test_image44(self):
self.run_exe_test('test_image44')

def test_image45(self):
self.run_exe_test('test_image45')

def test_image46(self):
self.run_exe_test('test_image46')

def test_image47(self):
self.run_exe_test('test_image47')

# Test in-memory image handling.
def test_image81(self):
self.run_exe_test('test_image81', 'image01.xlsx')
Expand Down
Binary file added test/functional/xlsx_files/image44.xlsx
Binary file not shown.
Binary file added test/functional/xlsx_files/image45.xlsx
Binary file not shown.
Binary file added test/functional/xlsx_files/image46.xlsx
Binary file not shown.
Binary file added test/functional/xlsx_files/image47.xlsx
Binary file not shown.

0 comments on commit e51eada

Please sign in to comment.