diff --git a/docs/images/top_left_cell.png b/docs/images/top_left_cell.png new file mode 100644 index 00000000..2c359b18 Binary files /dev/null and b/docs/images/top_left_cell.png differ diff --git a/include/xlsxwriter/worksheet.h b/include/xlsxwriter/worksheet.h index 29c94dff..826796cf 100644 --- a/include/xlsxwriter/worksheet.h +++ b/include/xlsxwriter/worksheet.h @@ -2236,6 +2236,7 @@ typedef struct lxw_worksheet { struct lxw_rel_tuples *external_table_links; struct lxw_panes panes; + char top_left_cell[LXW_MAX_CELL_NAME_LENGTH]; struct lxw_protection_obj protection; @@ -4578,6 +4579,27 @@ void worksheet_set_selection(lxw_worksheet *worksheet, lxw_row_t first_row, lxw_col_t first_col, lxw_row_t last_row, lxw_col_t last_col); +/** + * @brief Set the first visible cell at the top left of a worksheet. + * + * @param worksheet Pointer to a lxw_worksheet instance to be updated. + * @param row The cell row (zero indexed). + * @param col The cell column (zero indexed). + * + * The `%worksheet_set_top_left_cell()` function can be used to set the + * top leftmost visible cell in the worksheet: + * + * @code + * worksheet_set_top_left_cell(worksheet, 31, 26); + * worksheet_set_top_left_cell(worksheet, CELL("AA32")); // Same as above. + * @endcode + * + * @image html top_left_cell.png + * + */ +void worksheet_set_top_left_cell(lxw_worksheet *worksheet, lxw_row_t row, + lxw_col_t col); + /** * @brief Set the page orientation as landscape. * diff --git a/src/worksheet.c b/src/worksheet.c index f513b2d1..4b7c94a0 100644 --- a/src/worksheet.c +++ b/src/worksheet.c @@ -2396,28 +2396,29 @@ _worksheet_write_sheet_view(lxw_worksheet *self) LXW_PUSH_ATTRIBUTES_STR("showGridLines", "0"); /* Hide zeroes in cells. */ - if (!self->show_zeros) { + if (!self->show_zeros) LXW_PUSH_ATTRIBUTES_STR("showZeros", "0"); - } /* Display worksheet right to left for Hebrew, Arabic and others. */ - if (self->right_to_left) { + if (self->right_to_left) LXW_PUSH_ATTRIBUTES_STR("rightToLeft", "1"); - } /* Show that the sheet tab is selected. */ if (self->selected) LXW_PUSH_ATTRIBUTES_STR("tabSelected", "1"); /* Turn outlines off. Also required in the outlinePr element. */ - if (!self->outline_on) { + if (!self->outline_on) LXW_PUSH_ATTRIBUTES_STR("showOutlineSymbols", "0"); - } /* Set the page view/layout mode if required. */ if (self->page_view) LXW_PUSH_ATTRIBUTES_STR("view", "pageLayout"); + /* Set the top left cell if required. */ + if (self->top_left_cell[0]) + LXW_PUSH_ATTRIBUTES_STR("topLeftCell", self->top_left_cell); + /* Set the zoom level. */ if (self->zoom != 100 && !self->page_view) { LXW_PUSH_ATTRIBUTES_INT("zoomScale", self->zoom); @@ -9465,6 +9466,18 @@ worksheet_set_selection(lxw_worksheet *self, STAILQ_INSERT_TAIL(self->selections, selection, list_pointers); } +/* + * Set the first visible cell at the top left of the worksheet. + */ +void +worksheet_set_top_left_cell(lxw_worksheet *self, lxw_row_t row, lxw_col_t col) +{ + if (row == 0 && col == 0) + return; + + lxw_rowcol_to_cell(self->top_left_cell, row, col); +} + /* * Set panes and mark them as frozen. With extra options. */ diff --git a/test/functional/src/test_top_left_cell01.c b/test/functional/src/test_top_left_cell01.c new file mode 100644 index 00000000..92757504 --- /dev/null +++ b/test/functional/src/test_top_left_cell01.c @@ -0,0 +1,20 @@ +/***************************************************************************** + * Test cases for libxlsxwriter. + * + * Test to compare output against Excel files. + * + * Copyright 2014-2021, John McNamara, jmcnamara@cpan.org + * + */ + +#include "xlsxwriter.h" + +int main() { + + lxw_workbook *workbook = workbook_new("test_top_left_cell01.xlsx"); + lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); + + worksheet_set_top_left_cell(worksheet, 15, 0); + + return workbook_close(workbook); +} diff --git a/test/functional/src/test_top_left_cell02.c b/test/functional/src/test_top_left_cell02.c new file mode 100644 index 00000000..06a6519e --- /dev/null +++ b/test/functional/src/test_top_left_cell02.c @@ -0,0 +1,20 @@ +/***************************************************************************** + * Test cases for libxlsxwriter. + * + * Test to compare output against Excel files. + * + * Copyright 2014-2021, John McNamara, jmcnamara@cpan.org + * + */ + +#include "xlsxwriter.h" + +int main() { + + lxw_workbook *workbook = workbook_new("test_top_left_cell02.xlsx"); + lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); + + worksheet_set_top_left_cell(worksheet, 15, 6); + + return workbook_close(workbook); +} diff --git a/test/functional/src/test_top_left_cell03.c b/test/functional/src/test_top_left_cell03.c new file mode 100644 index 00000000..252a5866 --- /dev/null +++ b/test/functional/src/test_top_left_cell03.c @@ -0,0 +1,20 @@ +/***************************************************************************** + * Test cases for libxlsxwriter. + * + * Test to compare output against Excel files. + * + * Copyright 2014-2021, John McNamara, jmcnamara@cpan.org + * + */ + +#include "xlsxwriter.h" + +int main() { + + lxw_workbook *workbook = workbook_new("test_top_left_cell03.xlsx"); + lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL); + + worksheet_set_top_left_cell(worksheet, CELL("AA32")); + + return workbook_close(workbook); +} diff --git a/test/functional/test_top_left_cell.py b/test/functional/test_top_left_cell.py new file mode 100644 index 00000000..5c76274e --- /dev/null +++ b/test/functional/test_top_left_cell.py @@ -0,0 +1,24 @@ +############################################################################### +# +# Tests for libxlsxwriter. +# +# Copyright 2014-2021, John McNamara, jmcnamara@cpan.org +# + +import base_test_class + +class TestCompareXLSXFiles(base_test_class.XLSXBaseTest): + """ + Test file created with libxlsxwriter against a file created by Excel. + + """ + + def test_top_left_cell01(self): + self.run_exe_test('test_top_left_cell01') + + def test_top_left_cell02(self): + self.run_exe_test('test_top_left_cell02') + + def test_top_left_cell03(self): + self.run_exe_test('test_top_left_cell03') + diff --git a/test/functional/xlsx_files/top_left_cell01.xlsx b/test/functional/xlsx_files/top_left_cell01.xlsx new file mode 100644 index 00000000..f43d15b3 Binary files /dev/null and b/test/functional/xlsx_files/top_left_cell01.xlsx differ diff --git a/test/functional/xlsx_files/top_left_cell02.xlsx b/test/functional/xlsx_files/top_left_cell02.xlsx new file mode 100644 index 00000000..cfd37d75 Binary files /dev/null and b/test/functional/xlsx_files/top_left_cell02.xlsx differ diff --git a/test/functional/xlsx_files/top_left_cell03.xlsx b/test/functional/xlsx_files/top_left_cell03.xlsx new file mode 100644 index 00000000..1a5f8f26 Binary files /dev/null and b/test/functional/xlsx_files/top_left_cell03.xlsx differ