Search code examples
excelepplus

EPPlus: set Axis title to use a formula


In EPPlus I can create a chart and set a title of its axis to a string in the following way (F# syntax):

// "chart" is of type "ExcelChart"
chart.XAxis.Title.Text <- "My beautiful label"

When using Excel manually, it is possible to include a formula in chart's labels so that they are dynamic. However, when I set chart.XAxis.Title.Text to a formula, e.g.

chart.XAxis.Title.Text <- "=Sheet1!$A$1"

Unformatted chart label

my chart ends up displaying the raw, uninterpreted formula. Is there a way to have the formula recalculated each time the underlying cell is modified?


Solution

  • My research shows that EPPlus uses RichText for Axis' title by default and this can't be changed:

    https://github.com/JanKallman/EPPlus/blob/55c5ba6169eaa6ae7867fefb36a84cc0b28be85a/EPPlus/Drawing/Chart/ExcelChartAxis.cs#L594

    (This is the source code of the older version, but I guess it hasn't changed in the most recent one)

    I decided to do some manual hacking with the XML structure, which is similar to what's described here: https://stackoverflow.com/a/36387166/2545980