diff --git a/QXlsx/header/xlsxdocument.h b/QXlsx/header/xlsxdocument.h index 2fcbca9..da50fcb 100644 --- a/QXlsx/header/xlsxdocument.h +++ b/QXlsx/header/xlsxdocument.h @@ -41,7 +41,7 @@ public: QVariant read(const CellReference &cell) const; QVariant read(int row, int col) const; - bool insertImage(int row, int col, const QImage &image); + int insertImage(int row, int col, const QImage &image); Chart *insertChart(int row, int col, const QSize &size); diff --git a/QXlsx/header/xlsxdrawinganchor_p.h b/QXlsx/header/xlsxdrawinganchor_p.h index 21ab5c3..2a90109 100644 --- a/QXlsx/header/xlsxdrawinganchor_p.h +++ b/QXlsx/header/xlsxdrawinganchor_p.h @@ -85,6 +85,10 @@ protected: QSharedPointer m_chartFile; int m_id; +public: + int getm_id(); + +protected: // liufeij {{ void setObjectShape(const QImage &img); // liufeij diff --git a/QXlsx/header/xlsxworksheet.h b/QXlsx/header/xlsxworksheet.h index 00429ec..8cf4aab 100644 --- a/QXlsx/header/xlsxworksheet.h +++ b/QXlsx/header/xlsxworksheet.h @@ -97,7 +97,7 @@ public: Cell *cellAt(const CellReference &row_column) const; Cell *cellAt(int row, int column) const; - bool insertImage(int row, int column, const QImage &image); + int insertImage(int row, int column, const QImage &image); Chart *insertChart(int row, int column, const QSize &size); bool mergeCells(const CellRange &range, const Format &format=Format()); diff --git a/QXlsx/source/xlsxdocument.cpp b/QXlsx/source/xlsxdocument.cpp index 5c06007..d1cb1e5 100644 --- a/QXlsx/source/xlsxdocument.cpp +++ b/QXlsx/source/xlsxdocument.cpp @@ -641,11 +641,12 @@ QVariant Document::read(int row, int col) const * Insert an \a image to current active worksheet at the position \a row, \a column * Returns ture if success. */ -bool Document::insertImage(int row, int column, const QImage &image) +int Document::insertImage(int row, int column, const QImage &image) { if (Worksheet *sheet = currentWorksheet()) return sheet->insertImage(row, column, image); - return false; + + return 0; } /*! diff --git a/QXlsx/source/xlsxdrawinganchor.cpp b/QXlsx/source/xlsxdrawinganchor.cpp index 3ed5a7d..409ac20 100644 --- a/QXlsx/source/xlsxdrawinganchor.cpp +++ b/QXlsx/source/xlsxdrawinganchor.cpp @@ -821,6 +821,11 @@ void DrawingAnchor::saveXmlObjectPicture(QXmlStreamWriter &writer) const writer.writeEndElement(); //xdr:pic } +int DrawingAnchor::getm_id() +{ + return (this->m_id); +} + void DrawingAnchor::saveXmlObjectShape(QXmlStreamWriter &writer) const { //{{ liufeijin diff --git a/QXlsx/source/xlsxworkbook.cpp b/QXlsx/source/xlsxworkbook.cpp index 5e5c170..bfae9a8 100644 --- a/QXlsx/source/xlsxworkbook.cpp +++ b/QXlsx/source/xlsxworkbook.cpp @@ -703,14 +703,19 @@ QList > Workbook::mediaFiles() const void Workbook::addMediaFile(QSharedPointer media, bool force) { Q_D(Workbook); - if (!force) { - for (int i=0; imediaFiles.size(); ++i) { - if (d->mediaFiles[i]->hashKey() == media->hashKey()) { + + if (!force) + { + for (int i=0; imediaFiles.size(); ++i) + { + if (d->mediaFiles[i]->hashKey() == media->hashKey()) + { media->setIndex(i); return; } } } + media->setIndex(d->mediaFiles.size()); d->mediaFiles.append(media); } diff --git a/QXlsx/source/xlsxworksheet.cpp b/QXlsx/source/xlsxworksheet.cpp index a85d111..b2bd4f6 100644 --- a/QXlsx/source/xlsxworksheet.cpp +++ b/QXlsx/source/xlsxworksheet.cpp @@ -1085,17 +1085,21 @@ bool Worksheet::addConditionalFormatting(const ConditionalFormatting &cf) * Insert an \a image at the position \a row, \a column * Returns true on success. */ -bool Worksheet::insertImage(int row, int column, const QImage &image) +int Worksheet::insertImage(int row, int column, const QImage &image) { Q_D(Worksheet); + int imageIndex = 0; + if (image.isNull()) - return false; + return imageIndex; if (!d->drawing) + { d->drawing = QSharedPointer(new Drawing(this, F_NewFromScratch)); + } - DrawingOneCellAnchor *anchor = new DrawingOneCellAnchor(d->drawing.data(), DrawingAnchor::Picture); + DrawingOneCellAnchor* anchor = new DrawingOneCellAnchor(d->drawing.data(), DrawingAnchor::Picture); /* The size are expressed as English Metric Units (EMUs). @@ -1107,7 +1111,10 @@ bool Worksheet::insertImage(int row, int column, const QImage &image) anchor->ext = QSize( int(image.width() * scaleX), int(image.height() * scaleY) ); anchor->setObjectPicture(image); - return true; + + imageIndex = anchor->getm_id(); + + return imageIndex; } /*! diff --git a/TestExcel/image.cpp b/TestExcel/image.cpp index 2a27c5f..8c32d5b 100644 --- a/TestExcel/image.cpp +++ b/TestExcel/image.cpp @@ -13,7 +13,10 @@ int image() QImage image(40, 30, QImage::Format_RGB32); image.fill(Qt::green); for (int i=0; i<10; ++i) - xlsx.insertImage(10*i, 5, image); + { + int index = xlsx.insertImage( 10*i, 5, image ); + qDebug() << " [image index] " << index; + } xlsx.saveAs("image1.xlsx"); QXlsx::Document xlsx2("image1.xlsx");