1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
13 #include <reffact.hxx>
14 #include <TableFillingAndNavigationTools.hxx>
15 #include <DescriptiveStatisticsDialog.hxx>
16 #include <scresid.hxx>
17 #include <strings.hrc>
22 struct StatisticCalculation
{
23 TranslateId aCalculationNameId
;
27 const StatisticCalculation lclCalcDefinitions
[] =
29 { STRID_CALC_MEAN
, "=AVERAGE(%RANGE%)" },
30 { STRID_CALC_STD_ERROR
, "=SQRT(VAR(%RANGE%)/COUNT(%RANGE%))"},
31 { STRID_CALC_MODE
, "=MODE(%RANGE%)"},
32 { STRID_CALC_MEDIAN
, "=MEDIAN(%RANGE%)"},
33 { STRID_CALC_FIRST_QUARTILE
, "=QUARTILE(%RANGE%; 1)" },
34 { STRID_CALC_THIRD_QUARTILE
, "=QUARTILE(%RANGE%; 3)" },
35 { STRID_CALC_VARIANCE
, "=VAR(%RANGE%)"},
36 { STRID_CALC_STD_DEVIATION
, "=STDEV(%RANGE%)"},
37 { STRID_CALC_KURTOSIS
, "=KURT(%RANGE%)"},
38 { STRID_CALC_SKEWNESS
, "=SKEW(%RANGE%)"},
39 { STRID_CALC_RANGE
, "=MAX(%RANGE%)-MIN(%RANGE%)"},
40 { STRID_CALC_MIN
, "=MIN(%RANGE%)"},
41 { STRID_CALC_MAX
, "=MAX(%RANGE%)"},
42 { STRID_CALC_SUM
, "=SUM(%RANGE%)"},
43 { STRID_CALC_COUNT
, "=COUNT(%RANGE%)" },
49 ScDescriptiveStatisticsDialog::ScDescriptiveStatisticsDialog(
50 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
51 weld::Window
* pParent
, ScViewData
& rViewData
) :
52 ScStatisticsInputOutputDialog(
53 pSfxBindings
, pChildWindow
, pParent
, rViewData
,
54 u
"modules/scalc/ui/descriptivestatisticsdialog.ui"_ustr
,
55 u
"DescriptiveStatisticsDialog"_ustr
)
58 ScDescriptiveStatisticsDialog::~ScDescriptiveStatisticsDialog()
61 void ScDescriptiveStatisticsDialog::Close()
63 DoClose( ScDescriptiveStatisticsDialogWrapper::GetChildWindowId() );
66 TranslateId
ScDescriptiveStatisticsDialog::GetUndoNameId()
68 return STR_DESCRIPTIVE_STATISTICS_UNDO_NAME
;
71 ScRange
ScDescriptiveStatisticsDialog::ApplyOutput(ScDocShell
* pDocShell
)
73 AddressWalkerWriter
aOutput(mOutputAddress
, pDocShell
, mDocument
,
74 formula::FormulaGrammar::mergeToGrammar( formula::FormulaGrammar::GRAM_ENGLISH
, mAddressDetails
.eConv
));
75 FormulaTemplate
aTemplate(&mDocument
);
77 std::unique_ptr
<DataRangeIterator
> pIterator
;
78 if (mGroupedBy
== BY_COLUMN
)
79 pIterator
.reset(new DataRangeByColumnIterator(mInputRange
));
81 pIterator
.reset(new DataRangeByRowIterator(mInputRange
));
85 // Use explicit sheet name in case the input and output are on different sheets.
86 bool b3DAddress
= mInputRange
.aStart
.Tab() != mOutputAddress
.Tab();
88 // Write column/row labels
89 for( ; pIterator
->hasNext(); pIterator
->next() )
91 // tdf#128018 - add column/row labels to the output
92 OUString aColRowLabel
= mDocument
.GetString(pIterator
->get().aStart
);
93 if (aColRowLabel
.isEmpty())
95 if (mGroupedBy
== BY_COLUMN
)
96 aTemplate
.setTemplate(ScResId(STR_COLUMN_LABEL_TEMPLATE
));
98 aTemplate
.setTemplate(ScResId(STR_ROW_LABEL_TEMPLATE
));
100 aTemplate
.applyNumber(u
"%NUMBER%", pIterator
->index() + 1);
101 aOutput
.writeBoldString(aTemplate
.getTemplate());
105 aOutput
.writeBoldString(aColRowLabel
);
107 aOutput
.nextColumn();
110 aOutput
.resetColumn();
113 // Write calculation labels
114 for(sal_Int32 i
= 0; lclCalcDefinitions
[i
].aFormula
!= nullptr; i
++)
116 OUString
aLabel(ScResId(lclCalcDefinitions
[i
].aCalculationNameId
));
117 aOutput
.writeString(aLabel
);
120 aOutput
.nextColumn();
124 for( ; pIterator
->hasNext(); pIterator
->next() )
128 for(sal_Int32 i
= 0; lclCalcDefinitions
[i
].aFormula
!= nullptr; i
++)
130 aTemplate
.setTemplate(lclCalcDefinitions
[i
].aFormula
);
131 aTemplate
.applyRange(u
"%RANGE%", pIterator
->get(), b3DAddress
);
132 aOutput
.writeFormula(aTemplate
.getTemplate());
135 aOutput
.nextColumn();
138 return ScRange(aOutput
.mMinimumAddress
, aOutput
.mMaximumAddress
);
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */