Identify Active Chart and Selected Chart Element i

2019-07-20 23:57发布

I cobbled together some JavaScript to label the last point of each series in a chart (see below). This is a simplified version of a much-used function in a VBA add-in.

I used let mychart = mysheet.charts.getItemAt(0); to specify that the code should run on the first chart object on the worksheet. It would be more useful to run the code on the chart selected by the user.

  1. How do I identify which chart the user has selected (ActiveChart in VBA-speak)?

Similarly I used for (var iseries = 0; iseries < nseries; iseries++) to run the code on all series in the chart. It would be more useful to run the code on the specific series selected by the user.

  1. How do I identify which series in the chart has been selected by the user (related to TypeName(Selection) in VBA)?

Here is my Office-JS code:

    $("#run").click(() => tryCatch(labelLastPoint));
    //$("#label-last-point").click(labelLastPoint);
    async function labelLastPoint() {
        await Excel.run(async (context) => {
            let mysheet = context.workbook.worksheets.getActiveWorksheet();
            let mychart = mysheet.charts.getItemAt(0);

            let seriescollection = mychart.series;
            seriescollection.load("count");
            await context.sync();
            console.log("Number of Series: " + seriescollection.count);
            let nseries = seriescollection.count;

            for (var iseries = 0; iseries < nseries; iseries++) {
                console.log("- Series Number " + iseries);
                let myseries = seriescollection.getItemAt(iseries);
                let pointcollection = myseries.points;
                pointcollection.load("count");
                await context.sync();
                let npoints = myseries.points.count;
                let mypoint = myseries.points.getItemAt(npoints - 1);
                mypoint.hasDataLabel = true;
                mypoint.dataLabel.showSeriesName = true;
                mypoint.dataLabel.showValue = false;
            }
        });
    }

1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-07-21 00:09

As I understand it you're looking for API methods that might be called "getSelectedChart" and "getSelectedSeries". I'm afraid that there are no APIs in Office JS that do that. But it is a good idea. Please suggest that at Office Developer User Voice.

There is a getSelectedDataAsync method in the Shared Office JS APIs, but it does not support charts or series.

In the meantime, would it be possible in your scenario to have a drop down in the task pane, in which the user can specify, by title, the chart/series that is of interest?

查看更多
登录 后发表回答