Write UNO JAVA API get cursor current position in the table

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
carlos.lima
Posts: 4
Joined: Tue Jul 10, 2018 3:18 pm

Write UNO JAVA API get cursor current position in the table

Post by carlos.lima »

Hi guys!

I've got a document with some text and a table with the cursor with the focus in the first cell.

How can I get the cursor current position?
OpenOffice 3.1 on Windows Vista
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Write UNO JAVA API get cursor current position in the ta

Post by FJCC »

If the View Cursor is in a text table, it has a non-void TextTable property and also a Cell property. I don't use Java but I recorded some Java code with MRI.

Code: Select all

import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNamed;
import com.sun.star.frame.XController;
import com.sun.star.frame.XModel;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.table.XCell;
import com.sun.star.text.XTextTable;
import com.sun.star.text.XTextViewCursor;
import com.sun.star.text.XTextViewCursorSupplier;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
	try
	{
		XModel xModel = UnoRuntime.queryInterface(
			XModel.class, oInitialTarget);
		XController xController = xModel.getCurrentController();
		
		XTextViewCursorSupplier xTextViewCursorSupplier = UnoRuntime.queryInterface(
			XTextViewCursorSupplier.class, xController);
		XTextViewCursor xTextViewCursor = xTextViewCursorSupplier.getViewCursor();
		
		XPropertySet xPropSet = UnoRuntime.queryInterface(
			XPropertySet.class, xTextViewCursor);
		XCell xCell = UnoRuntime.queryInterface(XCell.class, xPropSet.getPropertyValue("Cell"));
		
		XTextTable xTextTable = UnoRuntime.queryInterface(XTextTable.class, xPropSet.getPropertyValue("TextTable"));
		
		XNamed xNamed = UnoRuntime.queryInterface(
			XNamed.class, xTextTable);
		String sName = xNamed.getName();
		
		XPropertySet xPropSet2 = UnoRuntime.queryInterface(
			XPropertySet.class, xCell);
		String sCellName = AnyConverter.toString(xPropSet2.getPropertyValue("CellName"));
		
	}
	catch (WrappedTargetException e1)
	{
		// getPropertyValue
		e1.printStackTrace();
	}
	catch (IllegalArgumentException e2)
	{
		// 
		e2.printStackTrace();
	}
	catch (UnknownPropertyException e3)
	{
		// getPropertyValue
		e3.printStackTrace();
	}
}
When I ran MRI in my sample document, sName had the value Table1 and sCellName was A1. I hope that helps.
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
Post Reply