With DsExcel, you can configure tags for worksheets which allow you to store private data in a cell, row, column, range or spreadsheet. The tags can store any type of data and are not visible to the end user. Tags are retained while performing the import or export operations from or to JSON.
Tags for worksheets can be configured using the Tag method of IWorksheet interface. Whereas for a cell or a range of cells, the tags can be configured using the Tag method of IRange interface. If the tag values are different in a range of cells, the tag value of the top-left cell of the range will be returned. The EntireColumn and EntireRow method of IRange interface, along with the Tag method can be used to get or set tags for columns and rows respectively.
You can also configure custom tags by instantiating a class. A json serializer or deserializer should be provided to support json I/O by implementing IJsonSerializer.
Refer to the following code to use tags:
Java |
Copy Code |
---|---|
private static void Tags() { // Initialize workbook Workbook workbook = new Workbook(); // Fetch default worksheet IWorksheet worksheet = workbook.getWorksheets().get(0); // Add Tag for worksheet worksheet.setTag("This is a Tag for sheet."); // Add Tag for Cell C1 worksheet.getRange("C1").setTag("This is a Tag for Cell C1"); // Add Tag for Row 4 worksheet.getRange("A4").getEntireRow().setTag("This is a Tag for Row 4"); // Add Tag for Column F worksheet.getRange("F5").getEntireColumn().setTag("This is a Tag for Column F"); // Add tag for Range A1:B2 worksheet.getRange("A1:B2").setTag("This is a Tag for A1:B2"); // Exporting workbook to JSON stream String jsonstr = workbook.toJson(); // Initialize another workbook Workbook workbook2 = new Workbook(); // Importing JSON stream in workbook workbook2.fromJson(jsonstr); // Get Tag of Range A1:B2 Object tag = workbook2.getWorksheets().get(0).getRange("A1:B2").getTag(); // Tags are preserved while exporting and importing json stream System.out.println(" Tag for CellRange[A1:B2] is : " + tag); |
Refer to the following code to use custom tags:
Java |
Copy Code |
---|---|
private static void CustomTag() { // Initialize workbook Workbook workbook = new Workbook(); // Fetch default worksheet IWorksheet worksheet = workbook.getWorksheets().get(0); // Set custom json serializer Workbook.setTagJsonSerializer(new MyJsonSerializer()); // Set Tag of "A1" as custom type "Student" worksheet.getRange("A1").setTag(new Student("Robin", 7)); // Exporting workbook to JSON stream String json = workbook.toJson(); // Initialize another workbook Workbook workbook2 = new Workbook(); // Importing JSON stream in workbook workbook2.fromJson(json); // Get Tag as JObject Object tag = workbook2.getWorksheets().get(0).getRange("A1").getTag(); // Convert JObject to "Student" Student student = new Gson().fromJson((JsonElement) tag, Student.class); // Tags are preserved while exporting and importing json stream System.out.println(" Tag for CellRange[A1] is of class: " + student); System.out.println(" Tag for CellRange[A1] is : " + tag); } public static class Student { public String name; public int age; public Student(String name, int age) { this.name = name; this.age = age; } } public static class MyJsonSerializer implements IJsonSerializer { Gson gson = new Gson(); @Override public String serialize(Object value) { return gson.toJson(value); } @Override public Object deserialize(String json) { return gson.fromJson(json, JsonElement.class); } } |