Write UNO JAVA API get cursor current position in the table

Creating a macro - Writing a Script - Using the API

Write UNO JAVA API get cursor current position in the table

Postby carlos.lima » Wed Jul 11, 2018 1:20 pm

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
carlos.lima
 
Posts: 2
Joined: Tue Jul 10, 2018 3:18 pm

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

Postby FJCC » Wed Jul 11, 2018 3:40 pm

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   Expand viewCollapse view
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.
AOO 3.4 or 4.1 on MS Windows XP ( before 2013-08-03) or Windows 7
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
FJCC
Moderator
 
Posts: 6581
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 3 guests