Determining merged cells in XTextTable

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
alex157
Posts: 2
Joined: Tue Jul 03, 2018 2:48 am

Determining merged cells in XTextTable

Post by alex157 »

The java code below outputs a list of cell names using `xtable.getCellNames()` followed by an array of cell names using `xCellRange.getCellByPosition(k, j)`. The attached .odt file has an empty table with merged and split cells. The output is:
A1
B1
A2
B2
A3
B3
C3
D3
B4

A1 B1
A2 B2 C2
A3 B3 C3 D3
A4 B4 C4 D4
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.

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);                    
        }
    }
}
Attachments
split_table1.odt
(8.69 KiB) Downloaded 92 times
Open Office 4.1.5
Linux
UnklDonald418
Volunteer
Posts: 1548
Joined: Wed Jun 24, 2015 12:56 am
Location: Colorado, USA

Re: Determining merged cells in XTextTable

Post by UnklDonald418 »

Perhaps you can find something helpful in the “Useful Macro Information” document by Andrew Pitonyak. Look at chapters 6.25 and 8.2.3
http://www.pitonyak.org/oo.php
If your problem has been solved, please edit this topic's initial post and add "[Solved]" to the beginning of the subject line
Apache OpenOffice 4.1.14 & LibreOffice 7.6.2.1 (x86_64) - Windows 10 Professional- Windows 11
Post Reply