Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add suport of print area and page setup #31

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 53 additions & 12 deletions src/xlsx/xlsxdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ bool Document::setColumnHidden(int colFirst, int colLast, bool hidden)
double Document::columnWidth(int column)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->columnWidth(column);
return sheet->columnWidth(column);
return 0.0;
}

Expand All @@ -612,7 +612,7 @@ double Document::columnWidth(int column)
Format Document::columnFormat(int column)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->columnFormat(column);
return sheet->columnFormat(column);
return Format();
}

Expand All @@ -622,7 +622,7 @@ Format Document::columnFormat(int column)
bool Document::isColumnHidden(int column)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->isColumnHidden(column);
return sheet->isColumnHidden(column);
return false;
}

Expand All @@ -646,7 +646,7 @@ bool Document::setRowFormat(int row, const Format &format)
bool Document::setRowFormat(int rowFirst, int rowLast, const Format &format)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->setRowFormat(rowFirst, rowLast, format);
return sheet->setRowFormat(rowFirst, rowLast, format);
return false;
}

Expand All @@ -670,7 +670,7 @@ bool Document::setRowHidden(int row, bool hidden)
bool Document::setRowHidden(int rowFirst, int rowLast, bool hidden)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->setRowHidden(rowFirst, rowLast, hidden);
return sheet->setRowHidden(rowFirst, rowLast, hidden);
return false;
}

Expand All @@ -696,7 +696,7 @@ bool Document::setRowHeight(int row, double height)
bool Document::setRowHeight(int rowFirst, int rowLast, double height)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->setRowHeight(rowFirst, rowLast, height);
return sheet->setRowHeight(rowFirst, rowLast, height);
return false;
}

Expand All @@ -705,8 +705,8 @@ bool Document::setRowHeight(int rowFirst, int rowLast, double height)
*/
double Document::rowHeight(int row)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->rowHeight(row);
if (Worksheet *sheet = currentWorksheet())
return sheet->rowHeight(row);
return 0.0;
}

Expand All @@ -716,8 +716,8 @@ double Document::rowHeight(int row)
Format Document::rowFormat(int row)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->rowFormat(row);
return Format();
return sheet->rowFormat(row);
return Format();
}

/*!
Expand All @@ -726,8 +726,8 @@ Format Document::rowFormat(int row)
bool Document::isRowHidden(int row)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->isRowHidden(row);
return false;
return sheet->isRowHidden(row);
return false;
}

/*!
Expand Down Expand Up @@ -815,6 +815,47 @@ bool Document::defineName(const QString &name, const QString &formula, const QSt
return d->workbook->defineName(name, formula, comment, scope);
}

/*!
* \brief Create a print area on sheet \a sheetName as cell range \a range
*
* \param sheetName Sheet name to set print ares
* \param range Cell range for print area
*/
bool Document::setPrintArea(const CellRange& range, const QString& sheetName)
{
Q_D(Document);

QString sheet = sheetName.isEmpty()
? currentWorksheet()->sheetName()
: sheetName;

return d->workbook->defineName(QStringLiteral("_xlnm.Print_Area"),
QString::fromUtf8("%1!%2")
.arg(sheet)
.arg(range.toString(true, true)),
QString(),
sheet);
}

bool Document::setPageSetup(double percent,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Document the public methods x2

QPageSize::PageSizeId size,
QPageLayout::Orientation orientation)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->setPageSetup(percent, size, orientation);
return false;
}

bool Document::setPageSetup(int fitToWidth,
int fitToHeight,
QPageSize::PageSizeId size,
QPageLayout::Orientation orientation)
{
if (Worksheet *sheet = currentWorksheet())
return sheet->setPageSetup(fitToWidth, fitToHeight, size, orientation);
return false;
}

/*!
Return the range that contains cell data.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/xlsx/xlsxdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include "xlsxworksheet.h"
#include <QObject>
#include <QVariant>
#include <QPageLayout>
#include <QPageSize>
class QIODevice;
class QImage;

Expand Down Expand Up @@ -98,6 +100,14 @@ class Q_XLSX_EXPORT Document : public QObject
Cell *cellAt(int row, int col) const;

bool defineName(const QString &name, const QString &formula, const QString &comment=QString(), const QString &scope=QString());
bool setPrintArea(const CellRange &range, const QString &sheetName=QString());
bool setPageSetup(double percent,
QPageSize::PageSizeId size,
QPageLayout::Orientation orientation);
bool setPageSetup(int fitToWidth,
int fitToHeight,
QPageSize::PageSizeId size,
QPageLayout::Orientation orientation);

CellRange dimension() const;

Expand Down
14 changes: 12 additions & 2 deletions src/xlsx/xlsxsharedstrings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,23 @@ void SharedStrings::saveToXmlFile(QIODevice *device) const
writer.writeStartElement(QStringLiteral("si"));
if (string.isRichString()) {
//Rich text string
Format lastNotEmptyFragmentFormat;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is unrelated to the pull request. Not a big deal but next time please try to keep them separated

for (int i=0; i<string.fragmentCount(); ++i) {
writer.writeStartElement(QStringLiteral("r"));
if (string.fragmentFormat(i).hasFontData()) {
Format fragmentFormat = string.fragmentFormat(i);
if (string.fragmentText(i).trimmed().isEmpty() && !fragmentFormat.hasFontData()) {
// Fragment contains only spaces and doesn't have font data.
// Excel will set default font for this fragment.
// It's better to set font of last fragment, which contains some text.
fragmentFormat = lastNotEmptyFragmentFormat;
}
if (fragmentFormat.hasFontData()) {
writer.writeStartElement(QStringLiteral("rPr"));
writeRichStringPart_rPr(writer, string.fragmentFormat(i));
writeRichStringPart_rPr(writer, fragmentFormat);
writer.writeEndElement();// rPr
}
lastNotEmptyFragmentFormat = fragmentFormat;

writer.writeStartElement(QStringLiteral("t"));
if (isSpaceReserveNeeded(string.fragmentText(i)))
writer.writeAttribute(QStringLiteral("xml:space"), QStringLiteral("preserve"));
Expand Down
Loading