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

invalid cast exception Calc C#

 
Post new topic   Reply to topic    OOoForum.org Forum Index -> OpenOffice.org Macros and API
View previous topic :: View next topic  
Author Message
l@bus
Newbie
Newbie


Joined: 08 Oct 2010
Posts: 3
Location: Czech Republic

PostPosted: Fri Oct 08, 2010 11:05 am    Post subject: invalid cast exception Calc C# Reply with quote

Hi I am new to OO programming so i found some sample codes for Calc. But when I try to run it I get invalid cast exception.

here is the code:
Code:


using unoidl.com.sun.star.lang;
using unoidl.com.sun.star.uno;
using unoidl.com.sun.star.frame;
using unoidl.com.sun.star.beans;
using unoidl.com.sun.star.sheet;
using unoidl.com.sun.star.container;
using unoidl.com.sun.star.table;

XComponentContext oStrap = uno.util.Bootstrap.bootstrap();
XMultiServiceFactory oServMan = (XMultiServiceFactory)oStrap.getServiceManager();
XComponentLoader oDesk = (XComponentLoader)oServMan.createInstance("com.sun.star.frame.Desktop");

string url = @"private:factory/scalc";
PropertyValue[] propVals = new PropertyValue[0];
XComponent oComp = oDesk.loadComponentFromURL(url, "_blank", 0, propVals);
// it works fine until here. New empty Calc document is opened.

XSpreadsheetDocument oDoc = (XSpreadsheetDocument)oComp;
XSpreadsheets oSheets = oDoc.getSheets();
XIndexAccess oSheetsIA = (XIndexAccess)oSheets;
XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value; // this is the line vhen i got invalid cast exception

XCell oCell;
oCell = oSheet.getCellByPosition( 1, 0 ); //B1
oCell.setValue(200);
           
string fileName = @"C:/Reports/data.ods";
((XStorable)oComp).storeAsURL(fileName, propVals);

oComp.dispose();
oComp = null;
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Sat Oct 09, 2010 5:54 am    Post subject: Reply with quote

Which line generates the exception?
_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
l@bus
Newbie
Newbie


Joined: 08 Oct 2010
Posts: 3
Location: Czech Republic

PostPosted: Sat Oct 09, 2010 8:21 am    Post subject: Reply with quote

this one:
Code:
XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0).Value; // this is the line vhen i got invalid cast exception


I am using Visual studio 2010 and cli dlls from OO SDK
Back to top
View user's profile Send private message
pitonyak
Administrator
Administrator


Joined: 09 Mar 2004
Posts: 3655
Location: Columbus, Ohio, USA

PostPosted: Sat Oct 09, 2010 2:48 pm    Post subject: Reply with quote

Now I see that you have it labeled in your original post (smack head).

Odd, the return type looks OK there. I wrote this little script to call my data inspection routines:
Code:
 Inspect ThisComponent.getSheets().getByIndex(0)

I found the following based on the return type.
Quote:
** INTERFACES **
com.sun.star.beans.XMultiPropertySet
com.sun.star.beans.XPropertySet
com.sun.star.beans.XPropertyState
com.sun.star.chart.XChartData
com.sun.star.chart.XChartDataArray
com.sun.star.container.XNamed
com.sun.star.drawing.XDrawPageSupplier
com.sun.star.lang.XServiceInfo
com.sun.star.lang.XTypeProvider
com.sun.star.lang.XUnoTunnel
com.sun.star.sheet.XArrayFormulaRange
com.sun.star.sheet.XArrayFormulaTokens
com.sun.star.sheet.XCellFormatRangesSupplier
com.sun.star.sheet.XCellRangeAddressable
com.sun.star.sheet.XCellRangeData
com.sun.star.sheet.XCellRangeFormula
com.sun.star.sheet.XCellRangeMovement
com.sun.star.sheet.XCellRangesQuery
com.sun.star.sheet.XCellSeries
com.sun.star.sheet.XDataPilotTablesSupplier
com.sun.star.sheet.XExternalSheetName
com.sun.star.sheet.XFormulaQuery
com.sun.star.sheet.XMultipleOperation
com.sun.star.sheet.XPrintAreas
com.sun.star.sheet.XScenario
com.sun.star.sheet.XScenarioEnhanced
com.sun.star.sheet.XScenariosSupplier
com.sun.star.sheet.XSheetAnnotationsSupplier
com.sun.star.sheet.XSheetAuditing
com.sun.star.sheet.XSheetCellRange
com.sun.star.sheet.XSheetFilterable
com.sun.star.sheet.XSheetFilterableEx
com.sun.star.sheet.XSheetLinkable
com.sun.star.sheet.XSheetOperation
com.sun.star.sheet.XSheetOutline
com.sun.star.sheet.XSheetPageBreak
com.sun.star.sheet.XSpreadsheet
com.sun.star.sheet.XSubTotalCalculatable
com.sun.star.sheet.XUniqueCellFormatRangesSupplier
com.sun.star.table.XAutoFormattable
com.sun.star.table.XCellRange
com.sun.star.table.XColumnRowRange
com.sun.star.table.XTableChartsSupplier
com.sun.star.util.XImportable
com.sun.star.util.XIndent
com.sun.star.util.XMergeable
com.sun.star.util.XModifyBroadcaster
com.sun.star.util.XProtectable
com.sun.star.util.XReplaceable
com.sun.star.util.XSearchable
com.sun.star.util.XSortable

** SERVICES **
com.sun.star.sheet.Spreadsheet
com.sun.star.sheet.SheetCellRange
com.sun.star.table.CellRange
com.sun.star.table.CellProperties
com.sun.star.style.CharacterProperties
com.sun.star.style.ParagraphProperties
com.sun.star.document.LinkTarget

Does the location change if you change the code to be something like this:

Code:

oSheetsIA.getByIndex(0);
oSheetsIA.getByIndex(0).Value;


If this works, are you able to use something like this to get the object type? I have never tried it, but I think that something similar should work.

Code:
oSheetsIA.getByIndex(0).Value.GetType().Name

_________________
--
Andrew Pitonyak
http://www.pitonyak.org/oo.php
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address
l@bus
Newbie
Newbie


Joined: 08 Oct 2010
Posts: 3
Location: Czech Republic

PostPosted: Sun Oct 10, 2010 7:03 am    Post subject: Reply with quote

When i change the line to this
Code:
XSpreadsheet oSheet = (XSpreadsheet)oSheetsIA.getByIndex(0);

I get this error while coding: Cannot convert type 'uno.Any' to 'unoidl.com.sun.star.sheet.XSpreadsheet'


I also tried:
Code:
string s = oSheetsIA.getByIndex(0).Value.GetType().Name;
XSpreadsheet oSheet = (XSpreadsheet)oSheets.getByName("list1").Value;

but both give me same invalid cast exception when i run them.

oSheetsIA and oSheets types are ok.
Back to top
View user's profile Send private message
bjozac
Newbie
Newbie


Joined: 10 Nov 2010
Posts: 3

PostPosted: Wed Nov 10, 2010 12:13 am    Post subject: Reply with quote

I have the exact same problem on:
Windows 7
VS 2008

Trying to run the provided samples,
ViewSample.cs together with SpreadsheetDocHelper.cs

Code:

      Console.WriteLine(" count: " + xIndex.getCount()); // okay ( in my case '4')
      Object o = xIndex.getByIndex(0).Type.Name; // failes
                uno.Any aPane = xIndex.getByIndex(1);


With the exception "InvalidCastException", "return argument has an Illegal type".
Back to top
View user's profile Send private message
adatpro
Newbie
Newbie


Joined: 11 Nov 2010
Posts: 1

PostPosted: Thu Nov 11, 2010 2:51 pm    Post subject: c# and automation calc problem Reply with quote

Hi

Os: WinXp, Open office (czech language)| 3.2 +SDK 3.2.1, C# express 2008

I had the same problem (compiler throw exception: invalid casting) on line :
XSpreadsheet oSheet = (XSpreadsheet) oSheetsIA.getByIndex(0).Value;

I changed Open office version on Open office us version| 3.2.1 +SDK 3.2.1
and then on the line
XSpreadsheet oSheet = (XSpreadsheet) oSheetsIA.getByIndex(0).Value;
compiler didn't throw exception, but next line
XCell oCell = oSheet.getCellByPosition( 0, 0 );
didn't work a compiler return error messge something like " oSheet isn't valid "

Stefan Slovakia
Back to top
View user's profile Send private message
glw
Newbie
Newbie


Joined: 12 Nov 2010
Posts: 1

PostPosted: Fri Nov 12, 2010 3:18 am    Post subject: Reply with quote

Hello,

Problem with: WinXp, Open office 3.2 SDK 3.2.1, C# express 2008

I run into a version-dependent error when trying to accees a sheet
by using the function:getByName().

My old program never caused any problems
with my previous installation of OpenOffice 2.4.3 and the SDK 2.4.3.

But when I tried to run the same program after installing
OpenOffice 3. and SDK 3.1 (and updating the CLI-references)
the function:getByName() returned NULL.

Switching to an old version is, of course, not the correct solution
but if did solve my problem.

Gerlof
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