1
0
Fork 0

Adding saving of spreadsheets as CSV.

main
Oliver Hummel 2023-12-14 08:31:20 +01:00
parent a127ce585b
commit a4e41180e1
2 changed files with 32 additions and 3 deletions

View File

@ -1,5 +1,6 @@
package de.hs_mannheim.informatik.spreadsheet; package de.hs_mannheim.informatik.spreadsheet;
import java.io.FileNotFoundException;
/** /**
* Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim. * Part of a simplified spreadsheet system for the PR1 programming lab at Hochschule Mannheim.
@ -8,7 +9,7 @@ package de.hs_mannheim.informatik.spreadsheet;
*/ */
public class Axel { public class Axel {
public static void main(String[] args) { public static void main(String[] args) throws FileNotFoundException {
Spreadsheet spr = new Spreadsheet(10,10); Spreadsheet spr = new Spreadsheet(10,10);
spr.put("A3", "123"); spr.put("A3", "123");
@ -20,7 +21,9 @@ public class Axel {
System.out.println(spr); System.out.println(spr);
// TODO: You might want to put "UI loop" for entering value and formulas here. spr.saveCsv("/tmp/test.csv");
// TODO: You might want to put "UI loop" for entering value and formulas here resp. in some UI methods.
} }
} }

View File

@ -2,6 +2,7 @@ package de.hs_mannheim.informatik.spreadsheet;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.PrintWriter;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -79,6 +80,31 @@ public class Spreadsheet {
// TODO: implement this // TODO: implement this
} }
/**
* A method for saving data to a CSV file.
* @param path The file to write.
* @return Nothing.
* @exception IOException If path does not exist.
*/
public void saveCsv(String path) throws FileNotFoundException {
PrintWriter out = new PrintWriter(path);
for (Cell[] row : cells) {
for (Cell cell : row) {
if (!cell.getFormula().isEmpty())
out.print("=" + cell.getFormula());
else
out.print(cell.getValue());
if (cell != row[cells.length-1])
out.print(",");
}
out.println();
}
out.close();
}
/** /**
* This method does the actual evaluation/calcluation of a specific cell * This method does the actual evaluation/calcluation of a specific cell
* @param cellName the name of the cell to be evaluated * @param cellName the name of the cell to be evaluated
@ -131,7 +157,7 @@ public class Spreadsheet {
Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula); Matcher m = Pattern.compile("([A-Z][0-9]*)|[-\\+\\*/]|[0-9]*").matcher(formula);
long res = 0; long res = 0;
// TODO implement // TODO implement
// uncomment the following to see an example how the elements of a formula can be accessed // uncomment the following to see an example how the elements of a formula can be accessed