From ccb24876c46b609035f08960f00519843f68bbe6 Mon Sep 17 00:00:00 2001 From: John McNamara Date: Fri, 14 Feb 2025 11:15:56 +0000 Subject: [PATCH] workbook: add set_size() method Request #472 --- include/xlsxwriter/workbook.h | 20 +++++ src/workbook.c | 21 ++++- .../test_workbook_write_workbook_view.c | 80 ++++++++++++++++++- 3 files changed, 115 insertions(+), 6 deletions(-) diff --git a/include/xlsxwriter/workbook.h b/include/xlsxwriter/workbook.h index a495e97e..f8e759be 100644 --- a/include/xlsxwriter/workbook.h +++ b/include/xlsxwriter/workbook.h @@ -324,6 +324,8 @@ typedef struct lxw_workbook { uint16_t drawing_count; uint16_t comment_count; uint32_t num_embedded_images; + uint16_t window_width; + uint16_t window_height; uint16_t font_count; uint16_t border_count; @@ -1069,6 +1071,24 @@ lxw_error workbook_set_vba_name(lxw_workbook *workbook, const char *name); */ void workbook_read_only_recommended(lxw_workbook *workbook); +/** + * @brief Set the size of a workbook window. + * + * @param workbook Pointer to a lxw_workbook instance. + * @param width Width of the window in pixels. + * @param height Height of the window in pixels. + * + * Set the size of a workbook window. This is generally only useful on macOS + * since Microsoft Windows uses the window size from the last time an Excel file + * was opened/saved. The default size is 1073 x 644 pixels. + * + * The resulting pixel sizes may not exactly match the target screen and + * resolution since it is based on the original Excel for Windows sizes. Some + * trial and error may be required to get an exact size. + */ +void workbook_set_size(lxw_workbook *workbook, + u_int16_t width, u_int16_t height); + void lxw_workbook_free(lxw_workbook *workbook); void lxw_workbook_assemble_xml_file(lxw_workbook *workbook); void lxw_workbook_set_default_xf_indices(lxw_workbook *workbook); diff --git a/src/workbook.c b/src/workbook.c index 5a296319..7437d143 100644 --- a/src/workbook.c +++ b/src/workbook.c @@ -1630,8 +1630,8 @@ _write_workbook_view(lxw_workbook *self) LXW_INIT_ATTRIBUTES(); LXW_PUSH_ATTRIBUTES_STR("xWindow", "240"); LXW_PUSH_ATTRIBUTES_STR("yWindow", "15"); - LXW_PUSH_ATTRIBUTES_STR("windowWidth", "16095"); - LXW_PUSH_ATTRIBUTES_STR("windowHeight", "9660"); + LXW_PUSH_ATTRIBUTES_INT("windowWidth", self->window_width); + LXW_PUSH_ATTRIBUTES_INT("windowHeight", self->window_height); if (self->first_sheet) LXW_PUSH_ATTRIBUTES_INT("firstSheet", self->first_sheet); @@ -1970,6 +1970,8 @@ workbook_new_opt(const char *filename, lxw_workbook_options *options) } workbook->max_url_length = 2079; + workbook->window_width = 16095; + workbook->window_height = 9660; return workbook; @@ -2852,3 +2854,18 @@ workbook_read_only_recommended(lxw_workbook *self) { self->read_only = 2; } + +/* + * Set the size of a workbook window. + */ +void +workbook_set_size(lxw_workbook *workbook, u_int16_t width, u_int16_t height) +{ + /* Convert the width/height to twips at 96 dpi. */ + if (width) + workbook->window_width = width * 1440 / 96; + + if (height) + workbook->window_height = height * 1440 / 96; + +} diff --git a/test/unit/workbook/test_workbook_write_workbook_view.c b/test/unit/workbook/test_workbook_write_workbook_view.c index 8603c5c1..67f5665a 100644 --- a/test/unit/workbook/test_workbook_write_workbook_view.c +++ b/test/unit/workbook/test_workbook_write_workbook_view.c @@ -14,7 +14,6 @@ // Test the _write_workbook_view() function. CTEST(workbook, write_workbook_view1) { - char* got; char exp[] = ""; FILE* testfile = lxw_tmpfile(NULL); @@ -32,7 +31,6 @@ CTEST(workbook, write_workbook_view1) { // Test the _write_workbook_view() function. CTEST(workbook, write_workbook_view2) { - char* got; char exp[] = ""; FILE* testfile = lxw_tmpfile(NULL); @@ -43,7 +41,6 @@ CTEST(workbook, write_workbook_view2) { _write_workbook_view(workbook); - RUN_XLSX_STREQ(exp, got); lxw_workbook_free(workbook); @@ -52,7 +49,6 @@ CTEST(workbook, write_workbook_view2) { // Test the _write_workbook_view() function. CTEST(workbook, write_workbook_view3) { - char* got; char exp[] = ""; FILE* testfile = lxw_tmpfile(NULL); @@ -69,3 +65,79 @@ CTEST(workbook, write_workbook_view3) { lxw_workbook_free(workbook); } +// Test the _write_workbook_view() function with set_size(). +CTEST(workbook, write_workbook_view4) { + + char* got; + char exp[] = ""; + FILE* testfile = lxw_tmpfile(NULL); + + lxw_workbook *workbook = workbook_new(NULL); + workbook->file = testfile; + + workbook_set_size(workbook, 0, 0); + + _write_workbook_view(workbook); + + RUN_XLSX_STREQ(exp, got); + + lxw_workbook_free(workbook); +} + +// Test the _write_workbook_view() function with set_size(). +CTEST(workbook, write_workbook_view5) { + + char* got; + char exp[] = ""; + FILE* testfile = lxw_tmpfile(NULL); + + lxw_workbook *workbook = workbook_new(NULL); + workbook->file = testfile; + + workbook_set_size(workbook, 1073, 644); + + + _write_workbook_view(workbook); + + RUN_XLSX_STREQ(exp, got); + + lxw_workbook_free(workbook); +} + +// Test the _write_workbook_view() function with set_size(). +CTEST(workbook, write_workbook_view6) { + + char* got; + char exp[] = ""; + FILE* testfile = lxw_tmpfile(NULL); + + lxw_workbook *workbook = workbook_new(NULL); + workbook->file = testfile; + + workbook_set_size(workbook, 123, 70); + + _write_workbook_view(workbook); + + RUN_XLSX_STREQ(exp, got); + + lxw_workbook_free(workbook); +} + +// Test the _write_workbook_view() function with set_size(). +CTEST(workbook, write_workbook_view7) { + + char* got; + char exp[] = ""; + FILE* testfile = lxw_tmpfile(NULL); + + lxw_workbook *workbook = workbook_new(NULL); + workbook->file = testfile; + + workbook_set_size(workbook, 719, 490); + + _write_workbook_view(workbook); + + RUN_XLSX_STREQ(exp, got); + + lxw_workbook_free(workbook); +}