0
votes

This code works well in Excel online but not works in Excel desktop. In Excel Desktop it generate error in browser log : "This operation is not permitted for the current object." on the line "chart.legend.legendEntries.getItemAt(i).visible".

Here the sample of the code where the problem occurs :

   // Create an chart.
chart = activeSheet.charts.add(chartType, chartRange, 'Auto');
...
// Add a serie
for (let i = 0; i < nbSeries; ++i) {        
    const newSeries = chart.series.add(legend);
}
...
    // Set legend visibility
for (let i = 0; i < nbSeries; i += 1) {
    chart.legend.legendEntries.getItemAt(i).visible = false;
}

The legendEntry is available since ExcelApi 1.7. My Excel is an version 1912.

Thank you for you help !

1

1 Answers

4
votes

When you want to hide the legend, you could use chart.legend.visible = false;.

The difference between Excel desktop and Excel online is that for Excel desktop, the new created chart doesn't include Legend, which included by default for Excel online.

If we try to get chart.legend.legendEntries when there's no Legend in Chart, we will get this error message.