This repository has been archived by the owner on Aug 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
FunctionFile.js
35 lines (28 loc) · 1.53 KB
/
FunctionFile.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/*
* Copyright (c) Microsoft. All rights reserved. Licensed under the MIT license.
* See LICENSE in the project root for license information.
*/
function addChart(event) {
// Run a batch operation against the Excel object model
Excel.run(function (ctx) {
// Create a proxy object for the selected range and load its address and values properties
var sourceRange = ctx.workbook.getSelectedRange().load("address");
// Run the queued-up commands, and return a promise to indicate task completion
return ctx.sync()
.then(function () {
var sheet = ctx.workbook.worksheets.getActiveWorksheet();
// Get the table
var masterTable = ctx.workbook.tables.getItem("ExpensesTable");
// Queue a command to add the new chart
var chartDataRangeColumn1 = masterTable.columns.getItemAt(0).getDataBodyRange();
var chartDataRangeColumn2 = masterTable.columns.getItemAt(1).getDataBodyRange();
// Insert the chart in the sheet and format the chart
var chartDataRange = chartDataRangeColumn1.getBoundingRect(chartDataRangeColumn2);
var chart = sheet.charts.add("Line", chartDataRange, Excel.ChartSeriesBy.auto);
chart.setPosition(sourceRange.address);
chart.title.text = "Expense Trends";
chart.title.format.font.color = "#41AEBD";
chart.series.getItemAt(0).format.line.color = "#2E81AD";
})
});
}