如何通过文本字段将图表目标位置作为用户在Excel VSTO加载项中的输入? [英] How to take chart target position as input from the user in an Excel VSTO Add-in through a textfield?
问题描述
我想在Excel加载项上创建条形图,但我想从文本字段中的用户那里获取图表的目标位置,如何使用该值并将其分配给chart.setPosition?是否可以将setPosition函数的参数添加为一个变量,该变量包含从文本字段中输入的值?
I want to create a Bar chart on Excel Add-in, but I want to get the target position of the chart from the user in a textfield, how can I use that value and assign it to the chart.setPosition ? Is it possible to add the parameter for the setPosition function as a variable that contains the value taken as input from the textfield?
这是我的代码:
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));
}
});
}
推荐答案
您可以使用
<input type="text" id="leftTop"/>
<input type="text" id="bottomRight"/>
HTML中的
供用户输入参数并使用
in the HTML for user to enter the param, and use
let value = $("#leftTop").val();
let lt = String(value);
value = $("#bottomRight").val();
let br = String(value);
chart.setPosition(lt, br);
将该文本字段解析为chart.setPosition.
to parse the textfield into chart.setPosition.
这是一个示例代码,您可以通过导入ScriptLab来运行它.
Here's a sample code, you could run it by import into ScriptLab.
这篇关于如何通过文本字段将图表目标位置作为用户在Excel VSTO加载项中的输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!