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);
|
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
|
} //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(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 xl_rowcol_to_cell_fast(int row, int col);
|
||||||
|
|
||||||
|
XLSX_AUTOTEST_EXPORT QString createSafeSheetName(const QString &nameProposal);
|
||||||
|
|
||||||
} //QXlsx
|
} //QXlsx
|
||||||
#endif // XLSXUTILITY_H
|
#endif // XLSXUTILITY_H
|
||||||
|
|||||||
@@ -205,10 +205,10 @@ Worksheet *Workbook::addWorksheet(const QString &name, int sheetId)
|
|||||||
Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
Worksheet *Workbook::insertWorkSheet(int index, const QString &name)
|
||||||
{
|
{
|
||||||
Q_D(Workbook);
|
Q_D(Workbook);
|
||||||
QString worksheetName = name;
|
QString worksheetName = createSafeSheetName(name);
|
||||||
if (!name.isEmpty()) {
|
if (!worksheetName.isEmpty()) {
|
||||||
//If user given an already in-used name, we should not continue any more!
|
//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;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
do {
|
do {
|
||||||
|
|||||||
@@ -49,6 +49,9 @@ private Q_SLOTS:
|
|||||||
|
|
||||||
void test_datetimeFromNumber_data();
|
void test_datetimeFromNumber_data();
|
||||||
void test_datetimeFromNumber();
|
void test_datetimeFromNumber();
|
||||||
|
|
||||||
|
void test_createSafeSheetName_data();
|
||||||
|
void test_createSafeSheetName();
|
||||||
};
|
};
|
||||||
|
|
||||||
UtilityTest::UtilityTest()
|
UtilityTest::UtilityTest()
|
||||||
@@ -181,6 +184,32 @@ void UtilityTest::test_datetimeFromNumber()
|
|||||||
QCOMPARE(QXlsx::datetimeFromNumber(num, is1904), dt);
|
QCOMPARE(QXlsx::datetimeFromNumber(num, is1904), dt);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UtilityTest::test_createSafeSheetName_data()
|
||||||
|
{
|
||||||
|
QTest::addColumn<QString>("original");
|
||||||
|
QTest::addColumn<QString>("result");
|
||||||
|
|
||||||
|
QTest::newRow("[Hello]") << QString("[Hello]")<<QString("Hello");
|
||||||
|
QTest::newRow("[Hello:Qt]") << QString("[Hello:Qt]")<<QString("Hello Qt");
|
||||||
|
QTest::newRow("[Hello\\Qt/Xlsx]") << QString("[Hello\\Qt/Xlsx]")<<QString("Hello Qt Xlsx");
|
||||||
|
QTest::newRow("[Hello\\Qt/Xlsx:Lib]") << QString("[Hello\\Qt/Xlsx:Lib]")<<QString("Hello Qt Xlsx Lib");
|
||||||
|
QTest::newRow("'He'llo'") << QString("'He'llo'")<<QString("He'llo");
|
||||||
|
QTest::newRow("'He'llo*Qt'") << QString("'He'llo*Qt'")<<QString("He'llo Qt");
|
||||||
|
QTest::newRow("'He'llo*Qt?Lib'") << QString("'He'llo*Qt?Lib'")<<QString("He'llo Qt Lib");
|
||||||
|
|
||||||
|
QTest::newRow(":'[Hello']") << QString(":'[Hello']")<<QString("Hello");
|
||||||
|
QTest::newRow("':'[Hello']") << QString("':'[Hello']")<<QString("Hello");
|
||||||
|
QTest::newRow(" ' : '[ Hello ' ] ") << QString(" ' : '[ Hello ' ] ")<<QString("Hello");
|
||||||
|
}
|
||||||
|
|
||||||
|
void UtilityTest::test_createSafeSheetName()
|
||||||
|
{
|
||||||
|
QFETCH(QString, original);
|
||||||
|
QFETCH(QString, result);
|
||||||
|
|
||||||
|
QCOMPARE(QXlsx::createSafeSheetName(original), result);
|
||||||
|
}
|
||||||
|
|
||||||
QTEST_APPLESS_MAIN(UtilityTest)
|
QTEST_APPLESS_MAIN(UtilityTest)
|
||||||
|
|
||||||
#include "tst_utilitytest.moc"
|
#include "tst_utilitytest.moc"
|
||||||
|
|||||||
Reference in New Issue
Block a user