0

(Using Apose.Cells 8.3.2.1)

We have a large number of Excel 97-2003 spreadsheets with large numbers of named ranges in them. We need to upgrade them all to Excel 2010. Unfortunately a lot of the named ranges are of the form ABC1234 which is now a valid cell reference in Excel 2010, so we're trying to rename all the named ranges and fix the relevant formulae with Aspose.Cells.

The problem we're having is that the formulae with external links just aren't saving.

This is the code I've been testing with :

var workbooks = new List<Workbook>
    {
        new Workbook(@"C:\Temp\Book1.xls"),
        new Workbook(@"C:\Temp\Book2.xls")
    };

var regex = new Regex(@"!([A-Za-z0-9_]+)");

foreach (var workbook in workbooks)
{
    // Named Ranges -> "FIX_" + name
    foreach (var name in workbook.Worksheets.Names)
    {
        name.Text = "FIX_" + name.Text;
    }

    // Update Formula References to renamed Named Ranges
    foreach (var worksheet in workbook.Worksheets)
    {
        foreach (Cell cell in worksheet.Cells)
        {
            if (!cell.IsFormula || cell.IsInTable || (cell.IsInArray && !cell.IsArrayHeader)) continue;
            if (!regex.IsMatch(cell.Formula)) continue;
            var newformula = regex.Replace(cell.Formula, "!FIX_$1");
            cell.Formula = newformula;
        }
    }
}

foreach (var workbook in workbooks)
{
    workbook.Save(workbook.FileName, SaveFormat.Excel97To2003);
}

"Book1.xls" and "Book2.xls" are excel 97-2003 spreadsheets with only one cell in each. A1 in Book1.xls contains the value "1337" and the workbook has a single named range referencing A1 called "MyNamedRange". A1 in Book2.xls contains the formula "='C:\Temp\Book1.xls'!MyNamedRange".

When I run the above code, the named range in Book1 is updated, but the formula in Book2 remains unchanged, even though I know that the line "cell.Formula = newformula" has actually been run on that formula.

What's going on? Is this a bug, or is there something I'm missing?

adhocgeek
  • 1,437
  • 1
  • 16
  • 30
  • Similar issue reported in Aspose forums at http://www.aspose.com/community/forums/thread/604996/updated-formula-not-saving-when-it-contains-an-external-reference.aspx, it will be fixed in the library. – Saqib Razzaq Feb 09 '15 at 08:30
  • Yup, that was me. Thanks. I didn't know whether I'd get a better response from the forum or here (I also didn't know whether it was a bug or my mistake). – adhocgeek Feb 10 '15 at 09:31

0 Answers0