diff --git a/IssueTest/WriteSharedFormula.py b/IssueTest/WriteSharedFormula.py new file mode 100644 index 0000000..0a8c742 --- /dev/null +++ b/IssueTest/WriteSharedFormula.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python + +# WriteSharedFormula.py + +# Installation: pip install XlsXcessive +# https://pypi.org/project/XlsXcessive/ + +import io + +from xlsxcessive import xlsx +from xlsxcessive.worksheet import Cell + +def main(): + workbook = xlsx.Workbook() + sheet1 = workbook.new_sheet('Sheet 1') + sheet1.cell('B1', value=7) + sheet1.cell('C1', value=8) + sheet1.cell('D1', value=9) + formula = sheet1.formula('B1 + C1', shared=True) + sheet1.cell('D2', formula) # master + sheet1.cell('E2', formula) # shared, references the master formula + xlsx.save(workbook, 'test.xlsx') # save local file + +if __name__ == "__main__": + # execute only if run as a script + main() diff --git a/IssueTest/main.cpp b/IssueTest/main.cpp index fe5dbb3..9e2a824 100644 --- a/IssueTest/main.cpp +++ b/IssueTest/main.cpp @@ -27,7 +27,7 @@ int main(int argc, char *argv[]) QVector testParams; int ret = test(testParams); - qDebug() << "test return value : " << ret; + // qDebug() << "test return value : " << ret; return 0; } diff --git a/IssueTest/run.cmd b/IssueTest/run.cmd new file mode 100644 index 0000000..a339ab2 --- /dev/null +++ b/IssueTest/run.cmd @@ -0,0 +1,2 @@ +python WriteSharedFormula.py + diff --git a/IssueTest/test.cpp b/IssueTest/test.cpp index ad356db..5ce4801 100644 --- a/IssueTest/test.cpp +++ b/IssueTest/test.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include using namespace std; @@ -19,14 +20,24 @@ using namespace std; #include "xlsxworkbook.h" using namespace QXlsx; -int test(QVector params) +int test( QVector params ) { + qDebug() << " current path : " << QDir::currentPath(); + Document doc( "test.xlsx" ); + if ( ! doc.load() ) + { + qDebug() << "Failed to load 'test.xlsx'"; + return (-1); + } + qDebug() << "load 'test2.xlsx'"; - - - - + if ( ! doc.saveAs( "test2.xlsx" ) ) + { + qDebug() << "Failed to save 'test2.xlsx'"; + return (-2); + } + qDebug() << "save 'test2.xlsx'"; return 0; } diff --git a/IssueTest/test.xlsx b/IssueTest/test.xlsx new file mode 100644 index 0000000..b3e85e6 Binary files /dev/null and b/IssueTest/test.xlsx differ diff --git a/QXlsx/source/xlsxworkbook.cpp b/QXlsx/source/xlsxworkbook.cpp index 5890ac1..dc450e4 100644 --- a/QXlsx/source/xlsxworkbook.cpp +++ b/QXlsx/source/xlsxworkbook.cpp @@ -1,27 +1,5 @@ -/**************************************************************************** -** Copyright (c) 2013-2014 Debao Zhang -** All right reserved. -** -** Permission is hereby granted, free of charge, to any person obtaining -** a copy of this software and associated documentation files (the -** "Software"), to deal in the Software without restriction, including -** without limitation the rights to use, copy, modify, merge, publish, -** distribute, sublicense, and/or sell copies of the Software, and to -** permit persons to whom the Software is furnished to do so, subject to -** the following conditions: -** -** The above copyright notice and this permission notice shall be -** included in all copies or substantial portions of the Software. -** -** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND -** NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE -** LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -** OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION -** WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -** -****************************************************************************/ +// xlsxworkbook.cpp + #include "xlsxworkbook.h" #include "xlsxworkbook_p.h" #include "xlsxsharedstrings_p.h" @@ -39,6 +17,7 @@ #include #include #include +#include QT_BEGIN_NAMESPACE_XLSX @@ -560,13 +539,20 @@ bool Workbook::loadFromXmlFile(QIODevice *device) QXmlStreamReader reader(device); while (!reader.atEnd()) { QXmlStreamReader::TokenType token = reader.readNext(); - if (token == QXmlStreamReader::StartElement) { - if (reader.name() == QLatin1String("sheet")) { + if (token == QXmlStreamReader::StartElement) + { + if (reader.name() == QLatin1String("sheet")) + { QXmlStreamAttributes attributes = reader.attributes(); + const QString name = attributes.value(QLatin1String("name")).toString(); + int sheetId = attributes.value(QLatin1String("sheetId")).toString().toInt(); + const QString rId = attributes.value(QLatin1String("r:id")).toString(); + const QStringRef &stateString = attributes.value(QLatin1String("state")); + AbstractSheet::SheetState state = AbstractSheet::SS_Visible; if (stateString == QLatin1String("hidden")) state = AbstractSheet::SS_Hidden; @@ -577,29 +563,47 @@ bool Workbook::loadFromXmlFile(QIODevice *device) AbstractSheet::SheetType type = AbstractSheet::ST_WorkSheet; if (relationship.type.endsWith(QLatin1String("/worksheet"))) + { type = AbstractSheet::ST_WorkSheet; + } else if (relationship.type.endsWith(QLatin1String("/chartsheet"))) + { type = AbstractSheet::ST_ChartSheet; + } else if (relationship.type.endsWith(QLatin1String("/dialogsheet"))) + { type = AbstractSheet::ST_DialogSheet; + } else if (relationship.type.endsWith(QLatin1String("/xlMacrosheet"))) + { type = AbstractSheet::ST_MacroSheet; + } else - qWarning("unknown sheet type"); + { + qWarning() << "unknown sheet type : " << relationship.type ; + } AbstractSheet *sheet = addSheet(name, sheetId, type); sheet->setSheetState(state); const QString fullPath = QDir::cleanPath(splitPath(filePath())[0] +QLatin1String("/")+ relationship.target); sheet->setFilePath(fullPath); - } else if (reader.name() == QLatin1String("workbookPr")) { + } + else if (reader.name() == QLatin1String("workbookPr")) + { QXmlStreamAttributes attrs = reader.attributes(); if (attrs.hasAttribute(QLatin1String("date1904"))) d->date1904 = true; - } else if (reader.name() == QLatin1String("bookviews")) { - while (!(reader.name() == QLatin1String("bookviews") && reader.tokenType() == QXmlStreamReader::EndElement)) { + } + else if (reader.name() == QLatin1String("bookviews")) + { + while (!(reader.name() == QLatin1String("bookviews") && + reader.tokenType() == QXmlStreamReader::EndElement)) + { reader.readNextStartElement(); - if (reader.tokenType() == QXmlStreamReader::StartElement) { - if (reader.name() == QLatin1String("workbookView")) { + if (reader.tokenType() == QXmlStreamReader::StartElement) + { + if (reader.name() == QLatin1String("workbookView")) + { QXmlStreamAttributes attrs = reader.attributes(); if (attrs.hasAttribute(QLatin1String("xWindow"))) d->x_window = attrs.value(QLatin1String("xWindow")).toString().toInt(); @@ -616,7 +620,9 @@ bool Workbook::loadFromXmlFile(QIODevice *device) } } } - } else if (reader.name() == QLatin1String("externalReference")) { + } + else if (reader.name() == QLatin1String("externalReference")) + { QXmlStreamAttributes attributes = reader.attributes(); const QString rId = attributes.value(QLatin1String("r:id")).toString(); XlsxRelationship relationship = d->relationships->getRelationshipById(rId);