• R/O
  • SSH
  • HTTPS

jpicosheet: 提交


Commit MetaInfo

修订版79 (tree)
时间2015-06-17 17:10:31
作者ki-chi

Log Message

AVERAGE関数を修正

更改概述

差异

--- trunk/src/com/nissy_ki_chi/jpicosheet/function/AVERAGE.java (revision 78)
+++ trunk/src/com/nissy_ki_chi/jpicosheet/function/AVERAGE.java (revision 79)
@@ -23,11 +23,15 @@
2323
2424 import java.math.BigDecimal;
2525 import java.math.MathContext;
26+import java.util.Collection;
2627
28+import com.nissy_ki_chi.jpicosheet.core.Cell;
2729 import com.nissy_ki_chi.jpicosheet.core.Element;
2830 import com.nissy_ki_chi.jpicosheet.core.Function;
31+import com.nissy_ki_chi.jpicosheet.core.ReferenceNotFoundException;
2932 import com.nissy_ki_chi.jpicosheet.core.Resolver;
3033 import com.nissy_ki_chi.jpicosheet.core.Element.ElementType;
34+import com.nissy_ki_chi.jpicosheet.core.Element.ErrorType;
3135
3236
3337 /**
@@ -41,16 +45,43 @@
4145 */
4246 @Override
4347 public Element call(Element[] elements, MathContext mc, Resolver resolver) {
48+
49+ int summaryCnt = 0;
50+
4451 // elementsの値の平均を返す
45- BigDecimal result = BigDecimal.ZERO;
46-
47- if (elements.length > 0) {
48- for(Element e: elements) {
49- result = result.add(e.getNumber(), mc);
52+ // 加算の対象とするのはNUMBER、BOOLEANのセルのみ
53+ BigDecimal summary = BigDecimal.ZERO;
54+ for(Element elem: elements) {
55+ try {
56+ switch(elem.getType()) {
57+ case NUMBER:
58+ case BOOLEAN:
59+ summary = summary.add(elem.getNumber(), mc);
60+ summaryCnt++;
61+ break;
62+ case GROUP_REFERENCE:
63+ case TABLE_REFERENCE:
64+ // Elementがグループ参照またはテーブル参照だった場合、参照に含まれるセルを取得の上、合算する
65+ Collection<Cell> collection = null;
66+ if (elem.getType() == ElementType.GROUP_REFERENCE) {
67+ collection = resolver.getCellsFromGroup(getGroupReference(elem));
68+ } else if (elem.getType() == ElementType.TABLE_REFERENCE) {
69+ collection = resolver.getCellsFromTable(getTableReference(elem));
70+ }
71+ for (Cell cell: collection) {
72+ if (cell.getValueType() == ElementType.NUMBER ||
73+ cell.getValueType() == ElementType.BOOLEAN) {
74+ summary = summary.add(cell.getValue().getNumber());
75+ summaryCnt++;
76+ }
77+ }
78+ }
79+ } catch (ReferenceNotFoundException e) {
80+ return Element.newElement(ElementType.ERROR, ErrorType.INVALID_REFERENCES);
5081 }
51- result = result.divide(new BigDecimal(elements.length), mc);
5282 }
53- return Element.newElement(ElementType.NUMBER, result);
83+
84+ return Element.newElement(ElementType.NUMBER, summary.divide(new BigDecimal(summaryCnt), mc));
5485 }
5586
5687
--- trunk/src/test/FunctionTest.java (revision 78)
+++ trunk/src/test/FunctionTest.java (revision 79)
@@ -124,6 +124,14 @@
124124 assertEquals(new BigDecimal(5), cell.getValue().getNumber());
125125 }
126126
127+ public void testAverageByReference() {
128+ sheet.addCell("ref1").setNumberValue("100");
129+ sheet.addCell("ref2").setNumberValue("200");
130+ sheet.addCell("ref3").setFormula("ref2");
131+ sheet.addCell("formula").setFormula("AVERAGE(ref1, ref3)");
132+ assertEquals(new BigDecimal(300), sheet.getCell("formula").getValue().getNumber());
133+ }
134+
127135 public void testABS() {
128136 cell.setFormula("ABS(10)");
129137 assertEquals(new BigDecimal("10"), cell.getValue().getNumber());
Show on old repository browser