I assume that the cells whose names appear in the array below but not in the list above have been merged. How can I determine which? i.e. how can I tell that C2 has been merged with B1. I thought of possibly using the column separators to deduce this. Would this be the best way? I also tried using `XTextTableCursor` but it seems to take an odd route at the bottom right of the table.A1
B1
A2
B2
A3
B3
C3
D3
B4
A1 B1
A2 B2 C2
A3 B3 C3 D3
A4 B4 C4 D4
Code: Select all
import com.sun.star.uno.UnoRuntime;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.uno.XComponentContext;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.lang.XComponent;
import com.sun.star.comp.helper.Bootstrap;
import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.FrameSearchFlag;
import com.sun.star.text.XTextTablesSupplier;
import com.sun.star.container.XNameAccess;
import com.sun.star.container.XIndexAccess;
import com.sun.star.beans.XPropertySet;
import com.sun.star.text.XTextTable;
import com.sun.star.uno.Any;
import com.sun.star.table.XTableRows;
import com.sun.star.table.XCell;
import com.sun.star.text.TableColumnSeparator;
import com.sun.star.table.XCellRange;
import java.io.File;
public class CellMatrix {
public static void main( String args[] ) {
try {
XComponentContext xContext = Bootstrap.bootstrap();
XMultiComponentFactory xMCF = xContext.getServiceManager();
Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
XComponentLoader xCompLoader = (XComponentLoader) UnoRuntime.queryInterface(XComponentLoader.class, oDesktop);
File file = new File("split_table1.odt");
String path = file.getAbsolutePath();
String sUrl = "file://" + path;
PropertyValue propertyValues[] = new PropertyValue[1];
propertyValues[0] = new PropertyValue();
propertyValues[0].Name = "Hidden";
propertyValues[0].Value = new Boolean(true);
XComponent xdoc = xCompLoader.loadComponentFromURL(sUrl, "_blank", FrameSearchFlag.ALL, propertyValues);
XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xdoc);
XNameAccess xNamedTables = xTablesSupplier.getTextTables();
XIndexAccess xIndexedTables = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xNamedTables);
Any any = (Any) xIndexedTables.getByIndex(0);
XTextTable xtable = (XTextTable) any.getObject();
XTableRows rows = (XTableRows) xtable.getRows();
XIndexAccess xIndexedRows = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, rows);
XCellRange xCellRange = (XCellRange)UnoRuntime.queryInterface(XCellRange.class, xtable);
any = (Any) xIndexedRows.getByIndex(0);
XCell xcell = null;
String[] cell_names = xtable.getCellNames();
for(int cnum = 0; cnum < cell_names.length; cnum++) {
System.out.println(cell_names[cnum]);
}
System.out.println();
for (int j = 0; j < xIndexedRows.getCount(); j++) {
String row_names = new String("");
any = (Any) xIndexedRows.getByIndex(j);
XPropertySet xrow = (XPropertySet) any.getObject();
TableColumnSeparator[] colSeps = (TableColumnSeparator[]) xrow.getPropertyValue("TableColumnSeparators");
xcell = xCellRange.getCellByPosition(0, j);
XPropertySet cell_props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcell);
row_names += " " + cell_props.getPropertyValue("CellName");
for (int k = 1; k <= colSeps.length; k++) {
xcell = xCellRange.getCellByPosition(k, j);
cell_props = (XPropertySet)UnoRuntime.queryInterface(XPropertySet.class, xcell);
row_names += " " + cell_props.getPropertyValue("CellName");
}
System.out.println(row_names);
}
System.exit(0);
} catch( Exception e ) {
e.printStackTrace(System.err);
}
}
}