From e9a9ac55274527c1871488f2cd3bb80b1e65e420 Mon Sep 17 00:00:00 2001 From: selim Date: Tue, 26 Dec 2023 05:20:21 +0100 Subject: [PATCH] Spreadsheet evaluateCell further implementation. --- .../informatik/spreadsheet/Spreadsheet.java | 88 +++++++++++++++---- 1 file changed, 69 insertions(+), 19 deletions(-) diff --git a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java index 94ddeb5..a606258 100644 --- a/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java +++ b/Axel/src/de/hs_mannheim/informatik/spreadsheet/Spreadsheet.java @@ -146,23 +146,50 @@ public class Spreadsheet { String formula = cells[row][col].getFormula(); String result = ""; + String substringStartCell1 = formula.substring(formula.length() - 6, formula.length() - 4); + String substringStart2 = formula.substring(formula.length() - 6, formula.length() - 3); + String substringEndCell = formula.substring(formula.length() - 3, formula.length() - 1); + + if (formula.startsWith("SUMME(")) // e.g. SUMME(A3:A8) if(formula.length()<13) - result = "" + sum(formula.substring(6, 8), formula.substring(9, 11)); - else if(formula.length()<14&&formula.substring(6, 9).contains(":")) - result = "" + sum(formula.substring(6, 8), formula.substring(9, 12)); - else if(formula.length()<14&&!formula.substring(6, 9).contains(":")) - result = "" + sum(formula.substring(6, 9), formula.substring(10, 12)); + result = "" + sum(substringStartCell1, substringEndCell); + else if(formula.length()<14&&substringStart2.contains(":")) + result = "" + sum(substringStartCell1, substringEndCell); + else if(formula.length()<14&&!substringStart2.contains(":")) + result = "" + sum(substringStart2, substringEndCell); else if(formula.length()<15) - result = "" + sum(formula.substring(6, 9), formula.substring(10, 13)); + result = "" + sum(substringStart2, substringEndCell); else result = "0"; else if (formula.startsWith("PRODUKT(")) // e.g. PRODUKT(A3:B9) - result = "TODO"; // TODO + if(formula.length()<15) + result = "" + product(substringStartCell1, substringEndCell); + else if(formula.length()<16&&substringStart2.contains(":")) + result = "" + product(substringStartCell1, substringEndCell); + else if(formula.length()<16&&!substringStart2.contains(":")) + result = "" + product(substringStart2, substringEndCell); + else if(formula.length()<17) + result = "" + product(substringStart2, substringEndCell); + else + result = "0"; + + else if (formula.startsWith("MITTELWERT(")) // e.g. MITTELWERT(A3:A5) - result = "TODO"; // TODO + if(formula.length()<17) + result = "" + average(substringStartCell1, substringEndCell); + else if(formula.length()<18&&substringStart2.contains(":")) + result = "" + average(substringStartCell1, substringEndCell); + else if(formula.length()<18&&!substringStart2.contains(":")) + result = "" + average(substringStart2, substringEndCell); + else if(formula.length()<19) + result = "" + average(substringStart2, substringEndCell); + else + result = "0"; + + else if (formula.startsWith("STABW(")) // e.g. STABW(C6:D8) -> Standardabweichung result = "TODO"; // TODO else if (formula.startsWith("MIN(")) // e.g. MIN(C13:H13) -> größter Wert @@ -198,6 +225,29 @@ public class Spreadsheet { return result; } + private long product(String startCellName, String endCellName) { + long result = Long.parseLong(cells[0][0].getValue());; + + for(int r = getRow(startCellName); r