OpenOffice.org Forum at OOoForum.orgThe OpenOffice.org Forum
 
 [Home]   [FAQ]   [Search]   [Memberlist]   [Usergroups]   [Register
 [Profile]   [Log in to check your private messages]   [Log in

[Solved] [Calc] Select current sheet and cell within formula

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
Daniel_Andres
General User
General User


Joined: 06 Jul 2012
Posts: 5
Location: Germany

PostPosted: Fri Jul 06, 2012 6:20 am    Post subject: [Solved] [Calc] Select current sheet and cell within formula Reply with quote

Hi,

i'm developing a Calc Addin for receiving online data in Java.

I want to make a function that receives the data and append it at the end of a list.
I want to give a named Cell like 'A1' in a String a Argument and then build the list at this startpoint. So first i want to access the cell 'A1' and the the next time 'A1'+1 and so on...
The reason for choosing the start cell as a String is, that i have a special function that works, and as last argument i have a optional string. but i don't want to add another function (cause of usability) and i can't get formula overloading work.

My Question now:
- How can I access an arbitrary cell when i have the XPropertySet of the formula and i can access the XComponentContext of the addin?

Thanks for any help Smile


Last edited by Daniel_Andres on Sat Jul 07, 2012 7:28 am; edited 1 time in total
Back to top
View user's profile Send private message
karolus
OOo Advocate
OOo Advocate


Joined: 22 Jun 2011
Posts: 210

PostPosted: Fri Jul 06, 2012 6:28 am    Post subject: Reply with quote

Hallo

Code:
firstrow = ....getCellRangeByName("A1").CellAddress.Row


Karo
Back to top
View user's profile Send private message
Daniel_Andres
General User
General User


Joined: 06 Jul 2012
Posts: 5
Location: Germany

PostPosted: Fri Jul 06, 2012 6:39 am    Post subject: Reply with quote

Hi Karo,

thx for help.

But that is not the main problem.
I have no idea how to get the current sheet so that i can use the method 'getCellRangeByName'...

I thought about this process:
- Input XPropertySet xProp
- Get current sheet from xProp
- then use getCellRangeByName("A1")
Back to top
View user's profile Send private message
karolus
OOo Advocate
OOo Advocate


Joined: 22 Jun 2011
Posts: 210

PostPosted: Fri Jul 06, 2012 9:18 am    Post subject: Reply with quote

Hallo

I've tried MRI
with:
→ThisComponent→CurrentSelection→Spreadsheet

Mri generate the following java-code:

Code:
import com.sun.star.frame.XModel;
import com.sun.star.sheet.XSheetCellRange;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;
import com.sun.star.uno.XInterface;

public static void snippet(XComponentContext xComponentContext, Object oInitialTarget)
{
   XModel xModel = UnoRuntime.queryInterface(
      XModel.class, oInitialTarget);
   XInterface oXInterface = xModel.getCurrentSelection();
   
   XSheetCellRange xSheetCellRange = UnoRuntime.queryInterface(
      XSheetCellRange.class, oXInterface);
   XSpreadsheet xSpreadsheet = xSheetCellRange.getSpreadsheet();
   
}


Karo
Back to top
View user's profile Send private message
Daniel_Andres
General User
General User


Joined: 06 Jul 2012
Posts: 5
Location: Germany

PostPosted: Sat Jul 07, 2012 1:10 am    Post subject: Reply with quote

HI Karo,

i'm not sure which Object oInitialTarget is.
I've tried this with an XComponentContext, but with this i can't get an XModel...
When I use XPropertySet, I get one. But I don't get an XSheeCellRange.
I've casted the XInterface

Thats my code... In line for is a NullPointerException.
Code:

XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xPropertySet);
       XInterface oXInterface = (XInterface) xModel.getCurrentSelection();
       XSheetCellRange xSheetCellRange = UnoRuntime.queryInterface(XSheetCellRange.class, oXInterface);
       XSpreadsheet xSheet = xSheetCellRange.getSpreadsheet();


Last edited by Daniel_Andres on Sat Jul 07, 2012 2:48 am; edited 1 time in total
Back to top
View user's profile Send private message
karolus
OOo Advocate
OOo Advocate


Joined: 22 Jun 2011
Posts: 210

PostPosted: Sat Jul 07, 2012 2:39 am    Post subject: Reply with quote

Hallo

[ Voweg: Ich habe von Java Null Ahnung, es wäre daher besser wenn du deine Fragen hier in diesem Forum in englischer Sprache formulierst damit dir auch andere antworten können. ]

initialTarget ist hier das aktuell geöffnete Calcdokument, in Basic ist das ansprechbar mit 'thisComponent', in Python per 'XSCRIPTCONTEXT.getDocument()', in Java ...? .

Karo
Back to top
View user's profile Send private message
Daniel_Andres
General User
General User


Joined: 06 Jul 2012
Posts: 5
Location: Germany

PostPosted: Sat Jul 07, 2012 7:27 am    Post subject: Reply with quote

Hi Karo,
thx for that hint. I'v translated my message above...

your last Post was the final point. Now i got it....

Thats the final solution:
Code:

XComponentContext context; // received by the RUntime in each Service
XMultiComponentFactory xRemoteServiceManager = context.getServiceManager();
   Object desktop = xRemoteServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context);
   XDesktop xDesktop = (XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, desktop);
   XComponent xComp = xDesktop.getCurrentComponent();
   XSpreadsheetDocument xDoc = (XSpreadsheetDocument) UnoRuntime.queryInterface(XSpreadsheetDocument.class, xComp);
   XModel xModel = (XModel) UnoRuntime.queryInterface(XModel.class, xDoc);
   XInterface oXInterface = (XInterface) xModel.getCurrentSelection();

   XSheetCellRange xSheetCellRange = UnoRuntime.queryInterface(XSheetCellRange.class, oXInterface);
   XSpreadsheet xSheet = xSheetCellRange.getSpreadsheet();

       XSheetCellCursor xSheetCellCursor = xSheet.createCursor();
       XCellCursor xCursor = (XCellCursor) UnoRuntime.queryInterface(XCellCursor.class, xSheetCellCursor);

       XCell xCell = xCursor.getCellByPosition(0, 0);
       xCell.setFormula("Test");


Last edited by Daniel_Andres on Sat Jul 07, 2012 9:52 am; edited 1 time in total
Back to top
View user's profile Send private message
karolus
OOo Advocate
OOo Advocate


Joined: 22 Jun 2011
Posts: 210

PostPosted: Sat Jul 07, 2012 9:28 am    Post subject: Reply with quote

Hallo

Thats why i never will start with Java: Same Stuff is done by Five short readable lines of code in Python.

Code:
def setformel():
    doc = XSCRIPTCONTEXT.getDocument()
    selection = doc.getCurrentSelection()
    sheet = selection.getSpreadsheet()
    cell = sheet.getCellByPosition(0,0)
    cell.setFormula("Test")


Karo
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API All times are GMT - 8 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group