| View previous topic :: View next topic |
| Author |
Message |
Daniel_Andres General User

Joined: 06 Jul 2012 Posts: 5 Location: Germany
|
Posted: Fri Jul 06, 2012 6:20 am Post subject: [Solved] [Calc] Select current sheet and cell within formula |
|
|
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 
Last edited by Daniel_Andres on Sat Jul 07, 2012 7:28 am; edited 1 time in total |
|
| Back to top |
|
 |
karolus OOo Advocate

Joined: 22 Jun 2011 Posts: 208
|
Posted: Fri Jul 06, 2012 6:28 am Post subject: |
|
|
Hallo
| Code: | | firstrow = ....getCellRangeByName("A1").CellAddress.Row |
Karo |
|
| Back to top |
|
 |
Daniel_Andres General User

Joined: 06 Jul 2012 Posts: 5 Location: Germany
|
Posted: Fri Jul 06, 2012 6:39 am Post subject: |
|
|
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 |
|
 |
karolus OOo Advocate

Joined: 22 Jun 2011 Posts: 208
|
Posted: Fri Jul 06, 2012 9:18 am Post subject: |
|
|
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 |
|
 |
Daniel_Andres General User

Joined: 06 Jul 2012 Posts: 5 Location: Germany
|
Posted: Sat Jul 07, 2012 1:10 am Post subject: |
|
|
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 |
|
 |
karolus OOo Advocate

Joined: 22 Jun 2011 Posts: 208
|
Posted: Sat Jul 07, 2012 2:39 am Post subject: |
|
|
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 |
|
 |
Daniel_Andres General User

Joined: 06 Jul 2012 Posts: 5 Location: Germany
|
Posted: Sat Jul 07, 2012 7:27 am Post subject: |
|
|
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 |
|
 |
karolus OOo Advocate

Joined: 22 Jun 2011 Posts: 208
|
Posted: Sat Jul 07, 2012 9:28 am Post subject: |
|
|
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 |
|
 |
|