1 min read

How to get the x-axis of an Excel scatter chart programmatically

Excel lets you manipulate its charts programmatically with VBA (Visual Basic for Applications) or with ExcelScript (TypeScript calling the Excel API). Unfortunately, I couldn't find any method that obviously lets you get the x-axis—because Excel uses a different ontology for naming the axes.

Charts in Excel's ontology have two axes: Category and Value. The name Category is misleading — in the case of a scatter plot, Category means the x-axis, regardless of whether you're plotting a categorical or a continuous variable there. It's basically the thing all your chart's series are plotted against. This axis shows a value in the common "domain" the series all share. The Value axis is the other one — the axis on which your series values are plotted.

For example, suppose we have the following data arranged in a 4x2 grid of cells in Excel:

Foo Bar
1.2 3.6
3.4 10.1
5 15.8

If we select this whole grid and make a scatter chart, Foo should end up the Category axis and Bar should end up the Value axis of the chart. So Foo will be plotted on the x-axis.

So to get the x-axis of an Excel chart programmatically, you want to get the axes and then specifically get the Categorical axis. In ExcelScript, I think that would look like:

let xAxis = chart.getAxes().getCategoryAxis();

If I understand correctly, the x-axis is actually the value axis in a few charts, for example in a 2D bar chart. So, for such a chart you would want to call .getValueAxis() instead of .getCategoryAxis().