首页 > 解决方案 > 如何通过文本字段在 Excel VSTO 加载项中获取用户的数据范围输入?

问题描述

我想在 Excel 加载项上创建一个条形图,但我想在文本字段中从用户那里获取数据范围,如何使用该值并将其分配给 dataRange 变量?这是我的代码:在 home.html 中, <input type="text" id="chart-range" class="right" /> 在 Home.js 中:

       $("#chart-range").change(function ()
      var input = document.getElementById("chart-range");
      var value = input.options[input.selectedIndex].value;
      createBarChart();
       })
                       
   })
  function createBarChart() {
        Excel.run(function (context) {

            const sheet = context.workbook.worksheets.getItem("Sheet1");
            const salesTable = sheet.tables.getItem("Table1");
            const dataRange = salesTable.getDataBodyRange();
            let chart = sheet.charts.add("ColumnClustered", dataRange, "Auto");

            chart.setPosition("A9", "F20");
            chart.title.text = "Farm Sales Bar chart";
            chart.legend.position = "Right";
            chart.legend.format.fill.setSolidColor("white");
            chart.dataLabels.format.font.size = 15;
            chart.dataLabels.format.font.color = "black";
            let points = chart.series.getItemAt(0).points;
            points.getItemAt(0).format.fill.setSolidColor("pink");
            points.getItemAt(1).format.fill.setSolidColor("indigo");

            return context.sync();
        })
            .catch(function (error) {
                console.log("Error: " + error);
                if (error instanceof OfficeExtension.Error) {
                    console.log("Debug info: " + JSON.stringify(error.debugInfo));
                }
            });
    }

标签: vstooffice-jsoffice-addinsexcel-addinsexcel-charts

解决方案


推荐阅读