Skip to content

Commit

Permalink
workbook: add check for blank worksheet name
Browse files Browse the repository at this point in the history
Issue #442
  • Loading branch information
jmcnamara committed May 8, 2024
1 parent 5bd28df commit e3f39fd
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/xlsxwriter/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ typedef enum lxw_error {
/** Function parameter validation error. */
LXW_ERROR_PARAMETER_VALIDATION,

/** Worksheet name cannot be blank. */
LXW_ERROR_SHEETNAME_IS_BLANK,

/** Worksheet name exceeds Excel's limit of 31 characters. */
LXW_ERROR_SHEETNAME_LENGTH_EXCEEDED,

Expand Down
3 changes: 3 additions & 0 deletions include/xlsxwriter/workbook.h
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,7 @@ lxw_workbook *workbook_new_opt(const char *filename,
*
* The worksheet name must be a valid Excel worksheet name, i.e:
*
* - The name cannot be blank.
* - The name is less than or equal to 31 UTF-8 characters.
* - The name doesn't contain any of the characters: ` [ ] : * ? / \ `
* - The name doesn't start or end with an apostrophe.
Expand Down Expand Up @@ -511,6 +512,7 @@ lxw_worksheet *workbook_add_worksheet(lxw_workbook *workbook,
*
* The chartsheet name must be a valid Excel worksheet name, i.e.:
*
* - The name cannot be blank.
* - The name is less than or equal to 31 UTF-8 characters.
* - The name doesn't contain any of the characters: ` [ ] : * ? / \ `
* - The name doesn't start or end with an apostrophe.
Expand Down Expand Up @@ -927,6 +929,7 @@ lxw_chartsheet *workbook_get_chartsheet_by_name(lxw_workbook *workbook,
* This function is used to validate a worksheet or chartsheet name according
* to the rules used by Excel:
*
* - The name cannot be blank.
* - The name is less than or equal to 31 UTF-8 characters.
* - The name doesn't contain any of the characters: ` [ ] : * ? / \ `
* - The name doesn't start or end with an apostrophe.
Expand Down
1 change: 1 addition & 0 deletions src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ char *error_strings[LXW_MAX_ERRNO + 1] = {
"Feature is not currently supported in this configuration.",
"NULL function parameter ignored.",
"Function parameter validation error.",
"Worksheet name cannot be blank.",
"Worksheet name exceeds Excel's limit of 31 characters.",
"Worksheet name cannot contain invalid characters: '[ ] : * ? / \\'",
"Worksheet name cannot start or end with an apostrophe.",
Expand Down
5 changes: 5 additions & 0 deletions src/workbook.c
Original file line number Diff line number Diff line change
Expand Up @@ -2607,6 +2607,11 @@ workbook_unset_default_url_format(lxw_workbook *self)
lxw_error
workbook_validate_sheet_name(lxw_workbook *self, const char *sheetname)
{

/* Check for blank worksheet name. */
if (strlen(sheetname) == 0)
return LXW_ERROR_SHEETNAME_IS_BLANK;

/* Check the UTF-8 length of the worksheet name. */
if (lxw_utf8_strlen(sheetname) > LXW_SHEETNAME_MAX)
return LXW_ERROR_SHEETNAME_LENGTH_EXCEEDED;
Expand Down
14 changes: 14 additions & 0 deletions test/unit/workbook/test_workbook_validate_worksheet_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,17 @@ CTEST(workbook, validate_worksheet_name09) {

lxw_workbook_free(workbook);
}

/* Test for blank sheet name. */
CTEST(workbook, validate_worksheet_name10) {

const char* sheetname = "";

lxw_workbook *workbook = workbook_new(NULL);
lxw_error exp = LXW_ERROR_SHEETNAME_IS_BLANK;
lxw_error got = workbook_validate_sheet_name(workbook, sheetname);

ASSERT_EQUAL(exp, got);

lxw_workbook_free(workbook);
}

0 comments on commit e3f39fd

Please # to comment.