Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class TableSheets
- Object
- TableSheets
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));Methods
public static boolean hasFormulas(Table t) | Whether a table holds any formula. |
public static Sheet toSheet(Table t, Workbook book, String name) | Copies a table into a new sheet of a workbook. |
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. |
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. |
public static Document recalculate(Document d) | Calculates every table formula in a document and shows each value in its field. |
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. |
public static String address(int row, int column) | The spreadsheet address of a cell, such as B3 for row 2, column 1. |
Inherited methods
Method details
hasFormulas
public static boolean hasFormulas(Table t)Whether a table holds any formula.
Parameters
tTable- 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
tTable- the table
bookWorkbook- the workbook
nameString- 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
sSheet- the sheet
idsDocumentModel- 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
tTable- the table
sSheet- the sheet, as
toSheetmade it and then edited
Returns
the updated table, or
t itself when nothing changedrecalculate
public static Document recalculate(Document d)Calculates every table formula in a document and shows each value in its field.
Parameters
dDocument- the document
Returns
the document with current values, or
d itself when none changedexcelFormula
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
formulaString- the formula, with or without
= tTable- the table it is in
rowint- its row
columnint- 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
rowint- from zero
columnint- from zero
Returns
the address