识别Office-JS中的活动图表和选定的图表元素 [英] Identify Active Chart and Selected Chart Element in Office-JS

查看:62
本文介绍了识别Office-JS中的活动图表和选定的图表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拼凑了一些JavaScript,以在图表中标记每个系列的最后一点(请参见下文).这是VBA加载项中常用功能的简化版本.

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.

我使用let mychart = mysheet.charts.getItemAt(0);指定代码应在工作表上的第一个图表对象上运行.在用户选择的图表上运行代码会更加有用.

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. 如何确定用户选择了哪个图表(在VBA中为ActiveChart)?

类似地,我使用for (var iseries = 0; iseries < nseries; iseries++)在图表中的所有系列上运行代码.在用户选择的特定系列上运行代码会更加有用.

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. 如何确定用户已选择图表中的哪个系列(与VBA中的TypeName(Selection)相关)?
  1. How do I identify which series in the chart has been selected by the user (related to TypeName(Selection) in VBA)?

这是我的Office-JS代码:

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;
            }
        });
    }

推荐答案

据我了解,您正在寻找可能称为"getSelectedChart"和"getSelectedSeries"的API方法.恐怕Office JS中没有API可以做到这一点.但这是一个好主意.请建议在 Office开发人员用户语音.

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.

Shared Office JS API中有一个getSelectedDataAsync方法,但它不支持图表或系列.

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?

这篇关于识别Office-JS中的活动图表和选定的图表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆