diff options
Diffstat (limited to 'sc/source/ui/dataprovider/datatransformation.cxx')
-rw-r--r-- | sc/source/ui/dataprovider/datatransformation.cxx | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/sc/source/ui/dataprovider/datatransformation.cxx b/sc/source/ui/dataprovider/datatransformation.cxx index 84df313aacdc..fd779bf6587a 100644 --- a/sc/source/ui/dataprovider/datatransformation.cxx +++ b/sc/source/ui/dataprovider/datatransformation.cxx @@ -10,6 +10,7 @@ #include <datatransformation.hxx> #include <document.hxx> +#include <limits> namespace sc { @@ -268,6 +269,103 @@ TransformationType TextTransformation::getTransformationType() const return TransformationType::TEXT_TRANSFORMATION; } +AggregateFunction::AggregateFunction(const std::set<SCCOL>& rColumns, const AGGREGATE_FUNCTION rType): + maColumns(rColumns), + maType(rType) +{ +} + +void AggregateFunction::Transform(ScDocument& rDoc) const +{ + SCROW nEndRow = 0; + for (auto& itr : maColumns) + { + nEndRow = getLastRow(rDoc, itr); + } + + for (auto& rCol : maColumns) + { + switch (maType) + { + case AGGREGATE_FUNCTION::SUM: + { + double nSum = 0; + for (SCROW nRow = 0; nRow <= nEndRow; ++nRow) + { + CellType eType; + rDoc.GetCellType(rCol, nRow, 0, eType); + if (eType == CELLTYPE_VALUE) + { + double nVal = rDoc.GetValue(rCol, nRow, 0); + nSum += nVal; + } + } + rDoc.SetValue(rCol, nEndRow + 1, 0, nSum); + } + break; + case AGGREGATE_FUNCTION::AVERAGE: + { + double nSum = 0; + for (SCROW nRow = 0; nRow <= nEndRow; ++nRow) + { + CellType eType; + rDoc.GetCellType(rCol, nRow, 0, eType); + if (eType == CELLTYPE_VALUE) + { + double nVal = rDoc.GetValue(rCol, nRow, 0); + nSum += nVal; + } + } + + double nAvg = nSum / (nEndRow + 1); + rDoc.SetValue(rCol, nEndRow + 1, 0, nAvg); + } + break; + case AGGREGATE_FUNCTION::MIN: + { + double nMin = std::numeric_limits<double>::max(); + for (SCROW nRow = 0; nRow <= nEndRow; ++nRow) + { + CellType eType; + rDoc.GetCellType(rCol, nRow, 0, eType); + if (eType == CELLTYPE_VALUE) + { + double nVal = rDoc.GetValue(rCol, nRow, 0); + if(nVal < nMin) + nMin = nVal; + } + } + rDoc.SetValue(rCol, nEndRow + 1, 0, nMin); + } + break; + case AGGREGATE_FUNCTION::MAX: + { + double nMax = std::numeric_limits<double>::lowest(); + for (SCROW nRow = 0; nRow <= nEndRow; ++nRow) + { + CellType eType; + rDoc.GetCellType(rCol, nRow, 0, eType); + if (eType == CELLTYPE_VALUE) + { + double nVal = rDoc.GetValue(rCol, nRow, 0); + if(nMax < nVal) + nMax = nVal; + } + } + rDoc.SetValue(rCol, nEndRow + 1, 0, nMax); + } + break; + default: + break; + } + } +} + +TransformationType AggregateFunction::getTransformationType() const +{ + return TransformationType::AGGREGATE_FUNCTION; +} + } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |