JKPieterse's avatar
JKPieterse
Silver Contributor
Oct 19, 2022
Status:
New

Add GetItemOrNullObject method to more objects in Excel JS API

Hi,

 

I am developing an Excel taskpane add-in that allows to search for strings in many Excel objects. When I was writing the function to retrieve the texts from the various chart objects, I discovered that in order for this process to work I need many context.sync calls inside a try-catch block, because there is no way to detect whether the object in question exists.

 

A code snippet to demonstrate my predicament (this is inside a loop traversing the charts on a sheet):

 

    let chartOnSht: Excel.Chart = selectedSheet.charts.getItemAt(i);
    chartOnSht.load("name,title,title/text,axes/categoryAxis/title,axes/categoryAxis/title/text");
    chartOnSht.title.load("text");
    chartOnSht.axes.categoryAxis.load("title,title/text");
    await context.sync();
    let valueAxisPri = null;
    let valueAxisSec = null;
    try {
      valueAxisPri = chartOnSht.axes.getItem("Value", "Primary");
      valueAxisPri.load("title,title/text");
      await context.sync();
    } catch {
      valueAxisPri = null;
    }
    try {
      valueAxisSec = chartOnSht.axes.getItem("Value", "Secondary");
      valueAxisSec.load("title,title/text");
      await context.sync();
    } catch {
      valueAxisSec = null;
    }

 

 

As you can see I'm using .axes.getItem("Value", "Secondary") statement to get information about the secondary chart axis, an axis that is rarely present. To be able to avoid errors and to minimize calls to the Excel process it would be good if there is a getItemOrNullObject for all relevant chart elements. To name a few:

- axes

- axes titles

- Chart title

- Data labels

- Series

In short, any chart object member which may not necessarily be present on a chart needs this method.

No CommentsBe the first to comment