Fix issue #24: remove invalid characters in worksheet names
This commit is contained in:
@@ -177,4 +177,30 @@ QString xl_rowcol_to_cell_fast(int row, int col)
|
||||
return col_str + QString::number(row);
|
||||
}
|
||||
|
||||
/*
|
||||
Creates a valid sheet name
|
||||
minimum length is 1
|
||||
maximum length is 31
|
||||
doesn't contain special chars: / \ ? * ] [ :
|
||||
Sheet names must not begin or end with ' (apostrophe)
|
||||
|
||||
Invalid characters are replaced by one space character ' '.
|
||||
*/
|
||||
QString createSafeSheetName(const QString &nameProposal)
|
||||
{
|
||||
if (nameProposal.isEmpty())
|
||||
return QString();
|
||||
|
||||
QString ret = nameProposal;
|
||||
if (nameProposal.contains(QRegularExpression(QStringLiteral("[/\\\\?*\\][:]+"))))
|
||||
ret.replace(QRegularExpression(QStringLiteral("[/\\\\?*\\][:]+")), QStringLiteral(" "));
|
||||
while(ret.contains(QRegularExpression(QStringLiteral("^\\s*'\\s*|\\s*'\\s*$"))))
|
||||
ret.remove(QRegularExpression(QStringLiteral("^\\s*'\\s*|\\s*'\\s*$")));
|
||||
ret = ret.trimmed();
|
||||
if (ret.size() > 31)
|
||||
ret = ret.left(31);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
} //namespace QXlsx
|
||||
|
||||
@@ -60,5 +60,7 @@ XLSX_AUTOTEST_EXPORT int xl_col_name_to_value(const QString &col_str);
|
||||
XLSX_AUTOTEST_EXPORT QString xl_rowcol_to_cell(int row, int col, bool row_abs=false, bool col_abs=false);
|
||||
XLSX_AUTOTEST_EXPORT QString xl_rowcol_to_cell_fast(int row, int col);
|
||||
|
||||
XLSX_AUTOTEST_EXPORT QString createSafeSheetName(const QString &nameProposal);
|
||||
|
||||
} //QXlsx
|
||||
#endif // XLSXUTILITY_H
|
||||
|
||||
@@ -205,10 +205,10 @@ Worksheet *Workbook::addWorksheet(const QString &name, int sheetId)
|
||||
Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
||||
{
|
||||
Q_D(Workbook);
|
||||
QString worksheetName = name;
|
||||
if (!name.isEmpty()) {
|
||||
QString worksheetName = createSafeSheetName(name);
|
||||
if (!worksheetName.isEmpty()) {
|
||||
//If user given an already in-used name, we should not continue any more!
|
||||
if (d->worksheetNames.contains(name))
|
||||
if (d->worksheetNames.contains(worksheetName))
|
||||
return 0;
|
||||
} else {
|
||||
do {
|
||||
|
||||
Reference in New Issue
Block a user