From b661d51b7cbaa9ba421fd18f46a5efff23fa0e42 Mon Sep 17 00:00:00 2001 From: Matt Davis Date: Thu, 5 Sep 2024 11:28:44 -0400 Subject: [PATCH] flush out data target examples more --- .../zulia/data/test/DataTargetExamples.java | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java b/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java index d6dc5808..9049b0c0 100644 --- a/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java +++ b/zulia-data/src/test/java/io/zulia/data/test/DataTargetExamples.java @@ -1,9 +1,11 @@ package io.zulia.data.test; +import io.zulia.data.output.FileDataOutputStream; import io.zulia.data.target.spreadsheet.SpreadsheetTarget; import io.zulia.data.target.spreadsheet.SpreadsheetTargetFactory; import io.zulia.data.target.spreadsheet.csv.CSVTarget; import io.zulia.data.target.spreadsheet.excel.ExcelTarget; +import io.zulia.data.target.spreadsheet.excel.ExcelTargetConfig; import io.zulia.data.target.spreadsheet.excel.cell.Link; import java.io.IOException; @@ -13,16 +15,49 @@ public class DataTargetExamples { - public static void main(String[] args) throws IOException { - + public void csvTarget() throws IOException { List headers = List.of("string column", "number", "decimal number", "boolean column", "list column", "link column", "some date"); - try (CSVTarget csvTarget = CSVTarget.withDefaultsFromFile("/data/test.csv", true, headers)) { + try (CSVTarget csvTarget = CSVTarget.withDefaultsFromFile("/data/test.xlsx", true, headers)) { writeSampleRecords(csvTarget); } - try (ExcelTarget excelTarget = ExcelTarget.withDefaultsFromFile("/data/test.xlsx", true, headers)) { + } + + public void excelTarget() throws IOException { + List headers = List.of("string column", "number", "decimal number", "boolean column", "list column", "link column", "some date"); + try (ExcelTarget excelTarget = ExcelTarget.withDefaultsFromFile("/data/testA.xlsx", true, headers)) { writeSampleRecords(excelTarget); } + ExcelTargetConfig excelTargetConfig = ExcelTargetConfig.from(FileDataOutputStream.from("/data/testB.xlsx", true)); + + // optionally set a type handler on the config to handle Dates, Numbers, Strings, Links, ... differently + + excelTargetConfig.withPrimarySheetName("Results"); + excelTargetConfig.withHeaders(List.of("Title", "Description", "Added")); + try (ExcelTarget excelTarget = ExcelTarget.withConfig(excelTargetConfig)) { + excelTarget.writeRow("some title", "some description", new Date()); + excelTarget.writeRow("some title 2", "some description 2", new Date()); + + excelTarget.newSheet("Other Stuff", List.of("Id", "Year", "Author")); + excelTarget.writeRow("1234", 2010, "John Doe"); + excelTarget.writeRow("456", 2011, "Jane Doe"); + + excelTarget.newSheet("Even More Stuff"); + excelTarget.appendValue(1); + excelTarget.appendValue("Lion"); + excelTarget.finishRow(); + + excelTarget.appendValue(2); + excelTarget.appendValue("Tigers"); + excelTarget.finishRow(); + + excelTarget.appendValue(3); + excelTarget.appendValue("Bears"); + excelTarget.finishRow(); + } + } + + public void genericTarget() throws IOException { try (SpreadsheetTarget spreadsheetTarget = SpreadsheetTargetFactory.fromFile("/data/test2.xlsx", true)) { writeSampleRecords(spreadsheetTarget); } @@ -31,10 +66,9 @@ public static void main(String[] args) throws IOException { List.of("header1", "header2"))) { writeSampleRecords(spreadsheetTarget); } - } - private static void writeSampleRecords(SpreadsheetTarget spreadsheetTarget) { + public void writeSampleRecords(SpreadsheetTarget spreadsheetTarget) { spreadsheetTarget.appendValue("some string"); spreadsheetTarget.appendValue(132); spreadsheetTarget.appendValue(10.332443f);