This project has moved and is read-only. For the latest updates, please go here.

How do I create a new instance of Worksheet?

Mar 18, 2015 at 9:12 PM
Edited Mar 18, 2015 at 9:13 PM
I'd like to subclass the NetOffice.ExcelApi.Worksheet class because I need a "custom" worksheet with the extra fields. As part of the construction of an instance of this new class, I'd like to just call the base class' constructor. That is, basically I need a regular Worksheet but need to set a few extra fields and have a couple of extra functions on it.

How do I create and new Worksheet? So far, I've only used the Add method from the Sheets class, which automatically does this for you and then adds it to the sheets. As far as I can tell, the constructor of the the Worksheet class needs to get the parent object (which is not a problem to get) and a COM proxy passed in. How do I do this?
Mar 20, 2015 at 1:11 PM
not sure i understand as well...

You want inherite from Worksheet and replace the origin with your own in com proxy management?

If its true:
Override Worksheet as your will and spend necessary ctors(redirect all to base).
After the add method from sheets, use the replace ctor in your custom worksheet class.

here it is(from NetOffice.ExcelApi.Worksheet.cs):

///<param name="replacedObject">object to replaced. replacedObject are not usable after this action</param>
[EditorBrowsable(EditorBrowsableState.Never), Browsable(false)]
public Worksheet(COMObject replacedObject) : base(replacedObject)
{

}

Now, your custom instance is part of the proxy management instead of the origin instance.


*Sebastain
Apr 1, 2015 at 4:41 PM
Thank you Sebastian for your reply! I tried it out and it works. I now however have the following problem:

After creating my own, custom, derived Worksheet and "adding" it by using the replace constructor I can't seem to be able to get it back. That is, if I loop through the Sheets property or use something like Application.ActiveSheet (on a sheet that I know is my derived sheet) I can't cast that to my derived class. It seems to me that I should be able to do that, shouldn't I?

My whole purpose in extending the Worksheet class was that I could add some extra functionality to it, but if I can't get the added object from Sheets as my derived ws then it's all useless.
Thanks a lot for your continued help!
Apr 11, 2015 at 9:48 AM
test 123 test test
Apr 11, 2015 at 9:57 AM
The discussion board is back :)

(i used stackoverflow for a previous answer http://stackoverflow.com/questions/29131735/how-to-instantiate-a-new-worksheet )

I have created an CreateInstance event in NO 1.7.4 (preview available .NET 4.0, .NET 4.5).
Have a look in latest source state "Tests\Concept Tests\CreateInstanceEvent" to see how its possible to replace NO classes with your custom.


*Sebastian
Apr 13, 2015 at 5:29 PM
Thanks Sebastian again. I read your answer on Stackoverflow and I looked at the example in the new source. It seems to me however that your example would replace all newly created sheets with an instance of my custom Worksheet. The example you gave on Stackoverflow replaces only the ones with a specific name, but this would not work for me either.

In my case I basically would have "regular" Worksheets and my custom worksheets mixed in the same workbook and I want to be able to tell which ones are the custom ones (and use their custom functionality) depending on the sheet.