Skip to content

Commit

Permalink
workbook: add set_size() method
Browse files Browse the repository at this point in the history
Request #472
  • Loading branch information
jmcnamara committed Feb 14, 2025
1 parent afaa075 commit ccb2487
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 6 deletions.
20 changes: 20 additions & 0 deletions include/xlsxwriter/workbook.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
21 changes: 19 additions & 2 deletions src/workbook.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

}
80 changes: 76 additions & 4 deletions test/unit/workbook/test_workbook_write_workbook_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// Test the _write_workbook_view() function.
CTEST(workbook, write_workbook_view1) {


char* got;
char exp[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"16095\" windowHeight=\"9660\"/>";
FILE* testfile = lxw_tmpfile(NULL);
Expand All @@ -32,7 +31,6 @@ CTEST(workbook, write_workbook_view1) {
// Test the _write_workbook_view() function.
CTEST(workbook, write_workbook_view2) {


char* got;
char exp[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"16095\" windowHeight=\"9660\" activeTab=\"1\"/>";
FILE* testfile = lxw_tmpfile(NULL);
Expand All @@ -43,7 +41,6 @@ CTEST(workbook, write_workbook_view2) {

_write_workbook_view(workbook);


RUN_XLSX_STREQ(exp, got);

lxw_workbook_free(workbook);
Expand All @@ -52,7 +49,6 @@ CTEST(workbook, write_workbook_view2) {
// Test the _write_workbook_view() function.
CTEST(workbook, write_workbook_view3) {


char* got;
char exp[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"16095\" windowHeight=\"9660\" firstSheet=\"2\" activeTab=\"1\"/>";
FILE* testfile = lxw_tmpfile(NULL);
Expand All @@ -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[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"16095\" windowHeight=\"9660\"/>";
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[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"16095\" windowHeight=\"9660\"/>";
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[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"1845\" windowHeight=\"1050\"/>";
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[] = "<workbookView xWindow=\"240\" yWindow=\"15\" windowWidth=\"10785\" windowHeight=\"7350\"/>";
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);
}

0 comments on commit ccb2487

Please sign in to comment.