jmcnamara / libxlsxwriter

A C library for creating Excel XLSX files.
https://libxlsxwriter.github.io
Other
1.49k stars 332 forks source link

A bug in documentation #393

Closed Horseechen closed 1 year ago

Horseechen commented 1 year ago

There is a confusing error in the 7th example in the following code(http://libxlsxwriter.github.io/data_validate_8c-example.html ): data_validation->validate should be assigned with a value of LXW_VALIDATION_TYPE_LIST_FORMULA rather than LXW_VALIDATION_TYPE_LIST.

original code:

    /*
     * Example 7. Limiting input to a value in a dropdown list.
     */
    worksheet_write_string(worksheet,
                           CELL("A15"),
                           "Select a value from a drop down list (using a cell range)",
                           NULL);

    data_validation->validate     = LXW_VALIDATION_TYPE_LIST;
    data_validation->value_formula = "=$E$4:$G$4";

    worksheet_data_validation_cell(worksheet, CELL("B15"), data_validation);

it should be

    /*
     * Example 7. Limiting input to a value in a dropdown list.
     */
    worksheet_write_string(worksheet,
                           CELL("A15"),
                           "Select a value from a drop down list (using a cell range)",
                           NULL);

    data_validation->validate     = LXW_VALIDATION_TYPE_LIST_FORMULA;
    data_validation->value_formula = "=$E$4:$G$4";

    worksheet_data_validation_cell(worksheet, CELL("B15"), data_validation);
jmcnamara commented 1 year ago

The member name for data_validation->value_formula is a little bit confusing but the example is correct. The data validation list comes from a range in the spreadsheet:

Screenshot 2023-02-14 at 14 25 31

Probably the member should be called something like value_range or value_source but I was trying to reuse/minimise the number of fields.

Horseechen commented 1 year ago

No, the example is incorrect. The parameter in Example 6 works in Example 7. You get the "right" result just by chance. You may try to change the value in reference cells then you will find that truth. Or you may comment Example 6's code, if you do so, the program will return a warning message. 2

Horseechen commented 1 year ago

By the way, in this page http://libxlsxwriter.github.io/working_with_data_validation.html data_validation->validate is assigned with LXW_VALIDATION_TYPE_LIST_FORMULA image

Just check it, I believe that the example code is incorrect. I followed the http://libxlsxwriter.github.io/working_with_data_validation.html page and got the right result.

jmcnamara commented 1 year ago

Ok, thanks. I get it now.

You are right the example is incorrect. The validation type should be LXW_VALIDATION_TYPE_LIST_FORMULA. I will fix it.

Horseechen commented 1 year ago

I would like to say thank you because the libxlsxwriter is really useful. Thank you for your sharing.

jmcnamara commented 1 year ago

Fixed on main and the documentation has been updated. Thanks for the report.