Class TableSheets

java.lang.Object
com.codename1.wordprocessing.TableSheets

public final class TableSheets extends Object

Moves tables between documents and spreadsheets, so a table's numbers and formulas are calculated by the same engine as a Workbook's.

A cell is a formula when its text starts with =, or when it holds a field whose instruction does -- the form Word writes for a table formula, such as = SUM(ABOVE). Word's position words become ranges: ABOVE is the cells over the formula in its column, below the header rows; LEFT the cells before it in its row; BELOW and RIGHT likewise. A number format switch such as \# "0.00" is dropped. Every other cell holds its text, or a number when its text is one.

DocumentModel model = new DocumentModel(new Document());
try {
    model.setDocument(Document.read(new ByteArrayInputStream(("<table><tr><th>Region</th>"
            + "<th>Q1</th></tr><tr><td>North</td><td>120</td></tr></table>").getBytes()),
            Document.Format.HTML));
} catch (IOException e) {
    Log.e(e);
}
// wordprocessing has a Table, and so does com.codename1.ui.table
com.codename1.wordprocessing.Table table = null;
for (int i = 0; i < model.size(); i++) {
    if (model.getDocument().get(i) instanceof com.codename1.wordprocessing.Table) {
        table = (com.codename1.wordprocessing.Table) model.getDocument().get(i);
    }
}
// every formula field shows its value
Document recalculated = TableSheets.recalculate(model.getDocument());
// com.codename1.ui has a Sheet too, hence the full name
com.codename1.spreadsheet.Sheet s = TableSheets.toSheet(table, new Workbook(), "Table 1");
Log.p(s.getCellText(1, 1));
  • Method Details

    • hasFormulas

      public static boolean hasFormulas(Table t)

      Whether a table holds any formula.

      Parameters
      • t: the table
      Returns

      true when a cell is a formula

    • toSheet

      public static Sheet toSheet(Table t, Workbook book, String name)

      Copies a table into a new sheet of a workbook.

      Parameters
      • t: the table

      • book: the workbook

      • name: the new sheet's name

      Returns

      the sheet; when the table has one header row, the sheet's first row is its header

    • fromSheet

      public static Table fromSheet(Sheet s, DocumentModel ids)

      A table made from a sheet: one paragraph per cell, formulas as fields showing their values, merged regions as merges.

      Parameters
      • s: the sheet

      • ids: the model whose document the table is for; its ids are reserved for the table and its paragraphs

      Returns

      the table, not yet in the document

    • update

      public static Table update(Table t, Sheet s)

      Writes a sheet's cells back into a table of the same shape, keeping each cell's paragraphs and formatting and changing only the text of what changed.

      Parameters
      • t: the table

      • s: the sheet, as toSheet made it and then edited

      Returns

      the updated table, or t itself when nothing changed

    • recalculate

      public static Document recalculate(Document d)

      Calculates every table formula in a document and shows each value in its field.

      Parameters
      • d: the document
      Returns

      the document with current values, or d itself when none changed

    • excelFormula

      public static String excelFormula(String formula, Table t, int row, int column)

      A Word table formula in the spreadsheet engine's syntax, for the cell at row, column.

      Parameters
      • formula: the formula, with or without =

      • t: the table it is in

      • row: its row

      • column: its column

      Returns

      the formula with a leading =

    • address

      public static String address(int row, int column)

      The spreadsheet address of a cell, such as B3 for row 2, column 1.

      Parameters
      • row: from zero

      • column: from zero

      Returns

      the address