Removing named range

Hi,

i have a requirement to remove the named ranges from an excel file. While going through the posts I
noticed that you have implemented this feature using a new API: Worksheets.removeName(String text).
I would like to know if this is available in the .Net version too. I am using the following code:

Workbook book = new Workbook();
book.Open(tbExcelPath.Text);
foreach (Name namedRange in book.Worksheets.Names)
{
if (string.Compare(namedRange.RefersTo, “=#REF!”, false) == 0)
{
// Remove this range.
How-To-Remove??
}
else
Console.WriteLine(namedRange.RefersTo);
}

Hi,

Thanks for your inquiry.

Well, I think you may use Worksheets.Names.Remove() and Worksheets.Names.RemoveAt() methods to remove a named range. Moreover, you may use Worksheets.Names.Clear() to remove all the named ranges in the workbook.

Thank you.

Not working … i am getting exception…The exception comes at this line:


book.Save(tbExcelPath.Text + “1”, FileFormatType.Excel2007Xlsx);

The code is pretty simple:

Workbook book = new Workbook();
book.Open(tbExcelPath.Text);
book.Worksheets.Names.Clear();
book.Save(tbExcelPath.Text + “1”, FileFormatType.Excel2007Xlsx);


Exception details are:

Index was out of range. Must be non-negative and less than the size of the collection.\r\nParameter name: index

at System.Collections.ArrayList.get_Item(Int32 index)
at Aspose.Cells.Names.get_Item(Int32 index)
at ନ.௩.಩(Byte[] ଠ, Int32 ಪ)
at ନ.௩.ಫ(Int32 ಪ, Int32 ֻ, Byte[] ଠ, Int32 ಭ, Int32 ಮ, Boolean ಯ)
at ନ.௩.ಫ(Int32 ಪ, Byte[] ଠ, Int32 ಭ, Int32 ಮ, Boolean ಯ)
at ନ.௩.ಫ(Cell ಬ)
at Aspose.Cells.Cell.get_Formula()
at ྄.⿟.ㆢ(XmlTextWriter ⃔)
at ྄.⿟.᝴(XmlTextWriter ⃔)
at ྄.や.ゟ(⺟ ᪑)
at ྄.や.ゑ(⺟ ᪑)
at ྄.や.⹽()
at ྄.や.᝴()
at ྄.め.も(Workbook ࿒, Stream ޥ, FileFormatType ゃ)

Hi,

We have found an issue regarding Names.Clear() method, we will figure it out soon.

Thank you.

Just to help you guys out, here is a more detilaed code to see what is going wrong.


This is the full code:
private void btnRemoveBadRefs_Click(object sender, EventArgs e)
{
if (File.Exists(tbExcelPath.Text))
{
Workbook book = new Workbook();
book.Open(tbExcelPath.Text);
foreach (Name namedRange in book.Worksheets.Names)
{
if (string.Compare(namedRange.RefersTo, “=#REF!”, false) == 0)
{
// Remove this range.
if (namedRange.GetRanges() == null) <<<<< This line causes the exception
continue;

Range[] ranges = namedRange.GetRanges();
foreach (Range curRange in ranges)
{
book.Worksheets.Names.Remove(curRange.Name);
}
}
else
Console.WriteLine(namedRange.RefersTo);
}
book.Save(tbExcelPath.Text + “1”, FileFormatType.Excel2003);
}
else
MessageBox.Show(“File not found”);
}

This the exception message:
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

This is the exception:
at System.Collections.ArrayList.get_Item(Int32 index)
at Aspose.Cells.Worksheets.get_Item(Int32 index)
at ନ.Ⴖ.჌(Byte[] ଠ, Int32 ಪ, Int32 Ⴘ, Worksheets ೃ, Cells ື)
at Aspose.Cells.Name.GetRanges()
at Excel1.Form1.btnRemoveBadRefs_Click(Object sender, EventArgs e) in

The point in code where the exception originates has also been marked.

I am kind lost here, because my requirement is to remove invalid references and that is not happening because of this exception. Getting Clear() to work will not be of help to me because I need the valid references to remain.

thanks,
Sunil

Hi Sunil,

Please try the attached version, we have fixed the issue regarding named range.

Thank you.

Thanks for all the efforts that your team is putting in. But my problem does not really get fixed. I am still getting the exception (see detailed code above). The only difference is that the exception comes much later in the processing now.


I am attaching a screenshot of the incorrect references that I am trying to remove using my code above. The data is confidential or I would have shared the original file itself.

thanks,

Hi,

We appreciate if you could create a simple project with sample template file(you may have dummy data in it), zip it and post it here to reproduce the issue, we will check it soon. You may also check the Checkbox labelled "Keep this post private", doing so, only you and Aspose staff would be able to see/download your attachments.

Thank you.

The issues you have found earlier (filed as 10002) have been fixed in this update.


This message was posted using Notification2Forum from Downloads module by aspose.notifier.

Note: Just for your knowledge. In the new release v4.8.0, we have merged Aspose.Grid suite to Aspose.Cells for .NET msi installer as Aspose.Grid would be no longer offered as a separate product now. You need to install the installer (uninstall older one first if you have) and use only Aspose.Cells.dll library in your project for your need. You may also take advantage using Aspose.Cells Grid suite though.