0

migrating an old VBA project to C# using Interop to manipulate Powerpoint and trying to find a way to replicate some of the old solution

I can get my Series Collection using:

NetOffice.PowerPointApi.Series test_sc = (NetOffice.PowerPointApi.Series)sh.Chart.SeriesCollection(1);

but trying to access the Values or XValues isn't working as I expected

I can get the content using either

string[] arr = ((IEnumerable)test_sc.Values).Cast<object>()
                       .Select(x => x.ToString())
                       .ToArray();

or

foreach (object item in (Array)test_sc.Values) { var aa = item;  }

but that doesn't let me update things.

For now I am using the ChartData.WorkBook.Worksheet.Cells.get_Range mechanism to update the underlying sheet but this is complicated by the fact the series isn't always using the same cell range in the worksheet depending on the source file so I can't simple map the old SeriesCollection[1].Values to a Range["b2..m2"] or whatever.

1/ Am I missing something obvious (c# isn't my usual platform, so quite possible) to get read/write access to the Values/XValues for a collection and 2/ Is there a better way that will give me relative access to the underlying range that the series represents

Offbeatmammal
  • 7,970
  • 2
  • 33
  • 52
  • Do you want to update just the values on the value axis? Or also on the category axis? In my experience the first one is doable, while the latter is pretty difficult to work with. – STHOH Mar 06 '22 at 19:04
  • I'd be happy with just value, though for now working with the underlying sheet - though slower - is working okay. Any pointers still welcome though – Offbeatmammal Mar 07 '22 at 10:06

1 Answers1

0

To update the values (on the value axis) you should be able to do that by e.g. a double array:

double[] someNewValues = {6,3,4,7,3};
PowerPoint.Series ser = chart.SeriesCollection(1);
ser.Values=someNewValues;

In my experience updating values on the category axis (i.e. ser.XValues) can be more tricky. If it is a scatter plot it doesn't seem to work with double values but converting to floats have worked for me.

STHOH
  • 261
  • 1
  • 12
  • for the `XValues`, looks like turning the string array into objects and applying to the first series is more reliable - `object[] objList = Array.ConvertAll(strArray, x => (object)x); ser.XValues = objList;` – Offbeatmammal Apr 02 '22 at 05:48