[Solved] Calc - Get Name Ranges

The Application Programming Interface and the OASIS Open Document Format

[Solved] Calc - Get Name Ranges

Postby ikw_chen » Sat Jun 04, 2011 1:49 pm

Hi All,

I didn't find out how to get all name ranges in calc with XNamedRanges interface:

I've try with below code, but don't know how to retrieve the list of name range in Calc

Code: Select all   Expand viewCollapse view
XNamedRanges xNamedRanges = (XNamedRanges) UnoRuntime.queryInterface(XNamedRanges.class, xSheet);


Thanks for advice!
Last edited by ikw_chen on Sun Jun 05, 2011 6:46 am, edited 1 time in total.
Openoffice 3.3 on WinXP
ikw_chen
 
Posts: 17
Joined: Tue Apr 12, 2011 4:03 pm

Re: Calc - Get Name Ranges

Postby FJCC » Sat Jun 04, 2011 3:32 pm

I don't know anything about Java, but I recorded the following code using MRI. It gets the ElementNames array of the NamedRanges property of the Calc document. The oInitialTarget is the document.
Code: Select all   Expand viewCollapse view
import com.sun.star.beans.UnknownPropertyException;
import com.sun.star.beans.XPropertySet;
import com.sun.star.container.XNameAccess;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.lang.WrappedTargetException;
import com.sun.star.sheet.XNamedRanges;
import com.sun.star.uno.AnyConverter;
import com.sun.star.uno.RuntimeException;
import com.sun.star.uno.UnoRuntime;

static public void snippet(Object oInitialTarget)
{
   try
   {
      XPropertySet xPropertySet = UnoRuntime.queryInterface(
         XPropertySet.class, oInitialTarget);
      XNamedRanges xNamedRanges = UnoRuntime.queryInterface(XNamedRanges.class, xPropertySet.getPropertyValue("NamedRanges"));
      
      XNameAccess xNameAccess = UnoRuntime.queryInterface(
         XNameAccess.class, xNamedRanges);
      String[] sElementNames = xNameAccess.getElementNames();
      
   }
   catch (WrappedTargetException e1)
   {
      // getPropertyValue
      e1.printStackTrace();
   }
   catch (IllegalArgumentException e2)
   {
      //
      e2.printStackTrace();
   }
   catch (UnknownPropertyException e3)
   {
      // getPropertyValue
      e3.printStackTrace();
   }
   catch (RuntimeException e4)
   {
      // getElementNames
      e4.printStackTrace();
   }
}
Windows 10 and Linux Mint, since 2017
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: 7494
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Calc - Get Name Ranges

Postby ikw_chen » Sun Jun 05, 2011 6:45 am

Hi FJCC,

It works fine. I get each name range by sElementNames[i].

Thanks so much.
Openoffice 3.3 on WinXP
ikw_chen
 
Posts: 17
Joined: Tue Apr 12, 2011 4:03 pm


Return to UNO API and ODF

Who is online

Users browsing this forum: No registered users and 3 guests