我想在 Excel 加载项上创建条形图,但我想在文本字段中从用户那里获取图表的目标位置,如何使用该值并将其分配给 chart.setPosition ?是否可以将 setPosition 函数的参数添加为包含从文本字段输入的值的变量?
这是我的代码:
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));
}
});
}