Setting CodeName?

I’m currently using ASPOSE.cells 7.2.2.0. It appears in this version that the Workbook.Worksheet.CodeName field is readonly? What is the reasoning behind this? Is there anyway to set it (through a setter method maybe)? It appears to default to “Sheet#”, but if I rename my worksheet it does not appear to update in any way.

Hi,

Thanks for using Aspose.Cells for .NET.

Please download and use the latest version:
Aspose.Cells
for .NET v7.3.1.3
. It is working fine. I have checked it.

You need to set the Name property, it will change the CodeName property automatically.

Please see the following code and its output below for your reference.

As you see from the output, if you change the Name property, then this change is reflected inside the CodeName property too.

C#


//Create a workbook

Workbook workbook = new Workbook();


//Access the first worksheet.

//Change the Name property

Worksheet worksheet = workbook.Worksheets[0];

worksheet.Name = “NewSheet”;


//Change will be reflected inside the CodeName property

Debug.WriteLine(worksheet.CodeName);


Output:
NewSheet

That does not appear to be working for me (using the new DLL you provided).

I’m using a WorkbookDesigner and loading a template file, and after doing the following:

WorkbookDesigner wd = new WorkbookDesigner();
wd.Workbook = new Workbook(“C:\Template\MyTemplate.xls”);
wd.Workbook.Worksheets[0].Name = “NewName”;
wd.Workbook.Worksheets[0].Cells[0, 0].Value = wd.Workbook.Worksheets[0].CodeName;

The CodeName remains “Sheet1”, which is what it was set to in the template file.

Hi,

Thanks for your feedback.

Please provide us your template file used by you in your code and which is causing this issue.

I was unable to reproduce this issue using the following code.

C#


WorkbookDesigner wd = new WorkbookDesigner();

wd.Workbook = new Workbook();

wd.Workbook.Worksheets[0].Name = “NewName”;

wd.Workbook.Worksheets[0].Cells[0, 0].Value = wd.Workbook.Worksheets[0].CodeName;



Hmmm, maybe I’m mistaking what CodeName is supposed to be. I just ran your code without using a template, and although it spit out “NewName” in the appropriate cell, bringing up the VBA editor (ALT-F11), shows me “Sheet1 (NewName)”. I was expecting to see “NewName (NewName)”. I guess my real question is, is there any way I get the VBA name of the sheet to match the actual name I’ve set it to?

Hi,

Thanks for your elaboration.

I looked into this issue again and I found, actually, this question is related to macros. Xlsx does not save any macros, only the Xlsm saves macros.

Aspose.Cells for .NET does not support injecting/manipulating/creating macros. It just preserve it.

It means, you can only read the CodeName but you cannot set the CodeName, so it is a read-only property.

Please see the following code and see the source.xlsm file attached by me. Please see the output. As you see, the Sheet name and code name are different.

C#


string filePath = @“F:\Shak-Data-RW\Downloads\source.xlsm”;


Workbook workbook = new Workbook(filePath);


Worksheet worksheet = workbook.Worksheets[0];


Debug.WriteLine(worksheet.Name);


Debug.WriteLine(worksheet.CodeName);


Output:
Sheet1
TestSheet