Identify Active Chart and Selected Chart Element in Office-JS

泪湿孤枕 提交于 2019-12-11 03:04:23

问题


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:


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?



来源:https://stackoverflow.com/questions/49226837/identify-active-chart-and-selected-chart-element-in-office-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!