diff --git a/include/xlsxwriter/common.h b/include/xlsxwriter/common.h index 368ae1e6..0610d959 100644 --- a/include/xlsxwriter/common.h +++ b/include/xlsxwriter/common.h @@ -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, diff --git a/include/xlsxwriter/workbook.h b/include/xlsxwriter/workbook.h index 7d462dda..07faf8f3 100644 --- a/include/xlsxwriter/workbook.h +++ b/include/xlsxwriter/workbook.h @@ -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. @@ -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. @@ -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. diff --git a/src/utility.c b/src/utility.c index 29fe8cf2..88110a16 100644 --- a/src/utility.c +++ b/src/utility.c @@ -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.", diff --git a/src/workbook.c b/src/workbook.c index 71945abd..3f5cd375 100644 --- a/src/workbook.c +++ b/src/workbook.c @@ -2607,6 +2607,13 @@ workbook_unset_default_url_format(lxw_workbook *self) lxw_error workbook_validate_sheet_name(lxw_workbook *self, const char *sheetname) { + if (sheetname == NULL) + return LXW_ERROR_NULL_PARAMETER_IGNORED; + + /* 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; diff --git a/test/unit/workbook/test_workbook_validate_worksheet_name.c b/test/unit/workbook/test_workbook_validate_worksheet_name.c index 9399a79e..7140b8f5 100644 --- a/test/unit/workbook/test_workbook_validate_worksheet_name.c +++ b/test/unit/workbook/test_workbook_validate_worksheet_name.c @@ -12,6 +12,7 @@ #include "../../../include/xlsxwriter/workbook.h" #include "../../../include/xlsxwriter/shared_strings.h" + /* Test a valid sheet name. */ CTEST(workbook, validate_worksheet_name01) { @@ -145,3 +146,31 @@ 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); +} + +/* Test for NULL sheet name. */ +CTEST(workbook, validate_worksheet_name11) { + + const char* sheetname = NULL; + + lxw_workbook *workbook = workbook_new(NULL); + lxw_error exp = LXW_ERROR_NULL_PARAMETER_IGNORED; + lxw_error got = workbook_validate_sheet_name(workbook, sheetname); + + ASSERT_EQUAL(exp, got); + + lxw_workbook_free(workbook); +}