When copying a sheet from an excel file to an excel object in memory, the named ranges in the formula's get interpreted as functions causing opening en closing brackets to be added: r100 becomes r100().
The code:
Aspose.Cells.Workbook newWorkbook = new Aspose.Cells.Workbook(); newWorkbook.Open(@"C:\new.xls"); Aspose.Cells.Workbook master = new Aspose.Cells.Workbook(); master.Open(@"C:\old.xls"); newWorkbook.Worksheets["Sheet1"].Copy(master.Worksheets["Sheet1"]); newWorkbook.Save(@"C:\new.xls", FileFormatType.Excel2003);
The problem only occurs with sheet-specific named ranges i.e. a named range that is only available for a specific sheet. You can add such a named range by choosing Insert>Name>Define in Excel and then typing 'Sheet1'!r_100 in the name text box (this will add the named range r_100 to the sheet with name Sheet1) or you can do this programatically.
When a sheet containing such a local named range is copied to a new xls file by using Aspose, the sheet specific named range that is used in formula's is referenced as a formula i.e. Aspose.Cells adds '()' to the name (i.e. r_400 becomes r_400()).
See files: Sheet1 of file TestAspose.xls is copied into sheet1 of TestAspose_result.xls. The problem occurs for named range r_400.
Still an issue when referring to a sheet-specific named range from another sheet:
If we have a sheet [Sheet 1] with sheet specific named ranges and we now want to programatically add formulas referencing these named ranges, we get following problem:
Four parse errors: two single quotes ['] at the start, no space between the two named ranges, a single quote after NAMEDRANGECOL2 and () added at the end.
We do not actually need Aspose to be able to calculate the formula.
But Aspose should leave the Formula as it is and not try and parse it and change it into a function.
Adding an additional option indicating not to change the formula would already be enough. Excel will deal with the formula correctly when the user opens the excel file.
We really need this functionality as soon as possible.
How do you create this file? Could you please post your project here?
When setting the formula, Aspose.Cells has to parse the formula to tokens. It takes much more time than setting the value. It's impossible to set formulas without parsing them. Formulas are saved as tokens in the Excel files, not the formula string.
I downloaded aspose.cells version 4.5.1.19 and it was able to resolve my parenthesis issue.
However, I am still having a problem where the formula does not want to calculate properly until i go into the formula bar, click with my mouse and press enter. Once I do that the formula calculates correctly.
It seems like its acting as if i put the formula in as a text value. However I did use the formula field of the named range ex) testrange.formula
this is my code for testing aspose abilities. just to get data into the cells
Private Sub Named_Ranges(ByVal sheet As Worksheet, ByVal dt As DataTable, ByVal o As Integer) Dim r As Integer, c As Integer, rangeHolder As Range r = 0 For Each RowItem In dt.Rows c = 0 For Each ColItem In dt.Columns rangeHolder = sheet.Cells.CreateRange(sheet.Cells(r + 1, c + 1).Name, sheet.Cells(r + 1, c + 1).Name) rangeHolder.Name = sheet.Name & "." & dt.Rows(r).Item(0) & "." & sheet.Cells(0, c + 1).Value If o = 1 Then rangeHolder(0, 0).Formula = testFormulas(r) End If c = c + 1 Next r = r + 1 Next End Sub
when i open the excel file all of the cells have an error of #NAME? when i click in the formula bar at the top of excel for that cell and press enter the formula calculates correctly even for the datastruct2.
I am trying to create the 3 sheets. the first two sheets containing formulas and the third sheet containing data. The formulas will use named ranges to refer to cells instead of A3,A4, and so forth. Which is why im going through all of the cells and creating named ranges
i have attached the code i was using. nothing special, hard coding a bunch of the data so i can test aspose.cells
Please try this fix, it will solve your issue. Actually in the older versions, if you set a formula (which contains a Name) to the cell with the Name undefined as well, you will get this issue.