programmatically_creating_complex_secsformat00_objects_from_excel_files

This is an old revision of the document!


Excel-to-SECS Structure Conversion Methods

These utility methods convert Excel sheets into hierarchical SecsFormat00 structures using `SecsFormat00` as the container for nested data and typed leaf values. They leverage ```java List<Map<String,Object» ExcelImporter.importExcel(File importFile, String sheetName) ``` to handle all low‑level parsing (including sparse cells). Depending on the use case, different parsing strategies can be applied.


  • Imports the sheet via `ExcelImporter`, producing a `List<Map<String,Object»`, where each map represents a row mapping header → cell value.
  • Iterates each row’s entries; every value is converted to `String` and wrapped in SecsFormat20 (ASCII).
  • Sparse rows (where some columns were empty) simply omit those entries, resulting in variable‑length sublists.
public static SecsFormat00 sf00FromExcel(File importFile, String sheetName) {
    SecsFormat00 outerList = new SecsFormat00();
    // Import as a list of maps: header → cell value
    List<Map<String, Object>> rows = ExcelImporter.importExcel(importFile, sheetName);
    for (Map<String, Object> row : rows) {
        SecsFormat00 rowList = new SecsFormat00();
        // Each present cell becomes a string leaf
        for (Map.Entry<String, Object> entry : row.entrySet()) {
            String value = entry.getValue().toString();
            rowList.add(new SecsFormat20(value));
        }
        outerList.add(rowList);
    }
    return outerList;
}
VID100 VID101 VID102
——–——–——–
55 ON 12.5
60 OFF
[
  [<A '55'>, <A 'ON'>,  <A '12.5'>],
  [<A '60'>, <A 'OFF'>]
]

  • Imports rows the same way as above, then for each cell value (as string):

1. If the string is empty, wrap as SecsFormat20(“”).

  2. Otherwise, try parsing as **SecsFormat34** (integer).  
  3. If that fails, try parsing as **SecsFormat44** (float).  
  4. If both fail, fall back to **SecsFormat20** (string).  
* Sparse cells (missing in the map) are skipped, so no entry is added for them.
public static SecsFormat00 sf00FromExcelAutomaticTypes(File importFile, String sheetName) {
    SecsFormat00 outerList = new SecsFormat00();
    List<Map<String, Object>> rows = ExcelImporter.importExcel(importFile, sheetName);
    for (Map<String, Object> row : rows) {
        SecsFormat00 rowList = new SecsFormat00();
        for (Map.Entry<String, Object> entry : row.entrySet()) {
            String trimmed = entry.getValue().toString().trim();
            if (trimmed.isEmpty()) {
                rowList.add(new SecsFormat20(""));
                continue;
            }
            try {
                int intVal = Integer.parseInt(trimmed);
                rowList.add(new SecsFormat34(intVal));
            } catch (NumberFormatException e1) {
                try {
                    double doubleVal = Double.parseDouble(trimmed);
                    rowList.add(new SecsFormat44(doubleVal));
                } catch (NumberFormatException e2) {
                    rowList.add(new SecsFormat20(trimmed));
                }
            }
        }
        outerList.add(rowList);
    }
    return outerList;
}
VID200 VID201 VID202
——–——–——–
123 HELLO 45.6
WORLD 789
[
  [<I4 123>,  <A 'HELLO'>, <F4 45.60000>],
  [<A 'WORLD'>, <I4 789>]
]

  • Expects exactly three columns per row (e.g. a map of size 3).
  • For each row:

1. If the map’s size ≠ 3, skip with a warning.

  2. Otherwise, trim and parse in order: **String**, **Double**, **Integer**.  
  3. On parse errors, skip with a warning.
public static SecsFormat00 sf00FromExcelTypedExample(File importFile, String sheetName) {
    SecsFormat00 outerList = new SecsFormat00();
    List<Map<String, Object>> rows = ExcelImporter.importExcel(importFile, sheetName);
    for (Map<String, Object> row : rows) {
        if (row.size() != 3) {
            System.err.println("Skipping row: expected 3 columns, got " + row.size() + " → " + row);
            continue;
        }
        try {
            Iterator<Object> it = row.values().iterator();
            String str = it.next().toString().trim();
            double dbl = Double.parseDouble(it.next().toString().trim());
            int i = Integer.parseInt(it.next().toString().trim());
            SecsFormat00 rowList = new SecsFormat00();
            rowList.add(new SecsFormat20(str));
            rowList.add(new SecsFormat44(dbl));
            rowList.add(new SecsFormat34(i));
            outerList.add(rowList);
        } catch (NumberFormatException e) {
            System.err.println("Skipping row due to type mismatch: " + row);
        }
    }
    return outerList;
}
VID300 VID301 VID302
——–——–——–
alpha 1.23 100
beta 4.56 200
[
  [<A 'alpha'>, <F4 1.23000>, <I4 100>],
  [<A 'beta'>,  <F4 4.56000>, <I4 200>]
]
  • programmatically_creating_complex_secsformat00_objects_from_excel_files.1745274092.txt.gz
  • Last modified: 2025/04/21 17:21
  • by colinr