[GH-ISSUE #519] worksheet_set_start_page() has no effect unless another page-setup function is also called #517

Open
opened 2026-10-02 23:11:47 -06:00 by gitea-mirror · 1 comment
Owner

Originally created by @billdenney on GitHub (Jul 27, 2026).
Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/519

Originally assigned to: @jmcnamara on GitHub.

worksheet_set_start_page() is silently ignored when it is the only page-setup
function called on a worksheet. No <pageSetup> element is written, so
firstPageNumber never reaches the file. Calling any other page-setup function
alongside it makes it work.

#include <stdio.h>
#include "xlsxwriter.h"

int main(void) {
    lxw_workbook  *wb = workbook_new("start_page.xlsx");
    lxw_worksheet *ws = workbook_add_worksheet(wb, NULL);

    worksheet_write_string(ws, 0, 0, "hello", NULL);
    worksheet_set_start_page(ws, 3);   /* the only page-setup call */

    printf("libxlsxwriter %s\n", lxw_version());
    workbook_close(wb);
    return 0;
}

xl/worksheets/sheet1.xml contains no <pageSetup> element at all:

<worksheet ...><dimension ref="A1"/><sheetViews>...</sheetViews>
<sheetFormatPr defaultRowHeight="15"/>
<sheetData><row r="1" spans="1:1"><c r="A1" t="s"><v>0</v></c></row></sheetData>
<pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/>
</worksheet>

Adding any other page-setup call — worksheet_set_landscape(),
worksheet_set_paper(), worksheet_set_print_scale() — makes
firstPageNumber="3" useFirstPageNumber="1" appear as expected.

Cause

worksheet_set_start_page() does not raise page_setup_changed, which is what
gates <pageSetup>:

worksheet_set_start_page(lxw_worksheet *self, uint16_t start_page)
{
    self->page_start = start_page;
}

It appears to be the only <pageSetup> writer that omits the flag —
worksheet_set_landscape(), worksheet_set_portrait(),
worksheet_set_paper(), worksheet_set_print_scale(),
worksheet_print_across() and worksheet_print_black_and_white() all set it.
(worksheet_set_page_view() also omits it, but correctly so: it drives
<sheetView view="pageLayout"> rather than <pageSetup>, and works on its own.)

Suggested fix

worksheet_set_start_page(lxw_worksheet *self, uint16_t start_page)
{
    self->page_start = start_page;
    self->page_setup_changed = LXW_TRUE;
}

I built the reprex against a patched copy to confirm; it then produces:

<pageSetup firstPageNumber="3" orientation="portrait" useFirstPageNumber="1"/>
Originally created by @billdenney on GitHub (Jul 27, 2026). Original GitHub issue: https://github.com/jmcnamara/libxlsxwriter/issues/519 Originally assigned to: @jmcnamara on GitHub. `worksheet_set_start_page()` is silently ignored when it is the only page-setup function called on a worksheet. No `<pageSetup>` element is written, so `firstPageNumber` never reaches the file. Calling any other page-setup function alongside it makes it work. ```C #include <stdio.h> #include "xlsxwriter.h" int main(void) { lxw_workbook *wb = workbook_new("start_page.xlsx"); lxw_worksheet *ws = workbook_add_worksheet(wb, NULL); worksheet_write_string(ws, 0, 0, "hello", NULL); worksheet_set_start_page(ws, 3); /* the only page-setup call */ printf("libxlsxwriter %s\n", lxw_version()); workbook_close(wb); return 0; } ``` `xl/worksheets/sheet1.xml` contains no `<pageSetup>` element at all: ```XML <worksheet ...><dimension ref="A1"/><sheetViews>...</sheetViews> <sheetFormatPr defaultRowHeight="15"/> <sheetData><row r="1" spans="1:1"><c r="A1" t="s"><v>0</v></c></row></sheetData> <pageMargins left="0.7" right="0.7" top="0.75" bottom="0.75" header="0.3" footer="0.3"/> </worksheet> ``` Adding any other page-setup call — `worksheet_set_landscape()`, `worksheet_set_paper()`, `worksheet_set_print_scale()` — makes `firstPageNumber="3" useFirstPageNumber="1"` appear as expected. ### Cause `worksheet_set_start_page()` does not raise `page_setup_changed`, which is what gates `<pageSetup>`: ```C worksheet_set_start_page(lxw_worksheet *self, uint16_t start_page) { self->page_start = start_page; } ``` It appears to be the only `<pageSetup>` writer that omits the flag — `worksheet_set_landscape()`, `worksheet_set_portrait()`, `worksheet_set_paper()`, `worksheet_set_print_scale()`, `worksheet_print_across()` and `worksheet_print_black_and_white()` all set it. (`worksheet_set_page_view()` also omits it, but correctly so: it drives `<sheetView view="pageLayout">` rather than `<pageSetup>`, and works on its own.) ### Suggested fix ```C worksheet_set_start_page(lxw_worksheet *self, uint16_t start_page) { self->page_start = start_page; self->page_setup_changed = LXW_TRUE; } ``` I built the reprex against a patched copy to confirm; it then produces: ```XML <pageSetup firstPageNumber="3" orientation="portrait" useFirstPageNumber="1"/> ```
Author
Owner

@jmcnamara commented on GitHub (Jul 27, 2026):

@billdenney Thanks for the report. I'll look into it.

<!-- gh-comment-id:5095743312 --> @jmcnamara commented on GitHub (Jul 27, 2026): @billdenney Thanks for the report. I'll look into it.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
github-starred/libxlsxwriter#517
No description provided.