Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save chengtie/fb2988c59ea18eb89f6b3494ed51bc60 to your computer and use it in GitHub Desktop.
Save chengtie/fb2988c59ea18eb89f6b3494ed51bc60 to your computer and use it in GitHub Desktop.
Create a column clustered chart - Shared with Script Lab
name: Create a column clustered chart
description: Create a column clustered chart
host: EXCEL
api_set: {}
script:
content: |+
$("#setup").click(setup);
$("#create-column-clustered-chart").click(createChart);
async function createChart() {
try {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const salesTable = sheet.tables.getItem("SalesTable");
const dataRange = salesTable.getDataBodyRange();
let chart = sheet.charts.add("ColumnClustered", dataRange, "auto");
chart.setPosition("A15", "F30");
chart.title.text = "Quarterly sales 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");
await context.sync();
});
}
catch (error) {
OfficeHelpers.Utilities.log(error);
}
}
async function setup() {
try {
await Excel.run(async (context) => {
await OfficeHelpers.ExcelUtilities.forceCreateSheet(context.workbook, "Sample");
const sheet = context.workbook.worksheets.getItem("Sample");
let expensesTable = sheet.tables.add('A1:E1', true);
expensesTable.name = "SalesTable";
expensesTable.getHeaderRowRange().values = [["Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"]];
expensesTable.rows.add(null, [
["Frames", 5000, 7000, 6544, 4377],
["Saddles", 400, 323, 276, 651],
["Brake levers", 12000, 8766, 8456, 9812],
["Chains", 1550, 1088, 692, 853],
["Mirrors", 225, 600, 923, 544],
["Spokes", 6005, 7634, 4589, 8765]
]);
if (Office.context.requirements.isSetSupported("ExcelApi", 1.2)) {
sheet.getUsedRange().format.autofitColumns();
sheet.getUsedRange().format.autofitRows();
}
sheet.activate();
await context.sync();
});
}
catch (error) {
OfficeHelpers.Utilities.log(error);
}
}
language: typescript
template:
content: |-
<section class="ms-font-m">
<p>This sample shows how to create a column clustered chart using the Excel JavaScript API.</p>
</section>
<section class="setup ms-font-m">
<h3>Set up</h3>
<button id="setup" class="ms-Button">
<span class="ms-Button-label">Create table</span>
</button>
</section>
<section class="samples ms-font-m">
<h3>Try it out</h3>
<button id="create-column-clustered-chart" class="ms-Button">
<span class="ms-Button-label">Create a column clustered chart</span>
</button>
</section>
language: html
style:
content: "section.samples {\r\n margin-top: 20px;\r\n}\r\n\r\nsection.samples .ms-Button, section.setup .ms-Button {\r\n display: block;\r\n margin-bottom: 5px;\r\n margin-left: 20px;\r\n min-width: 80px;\r\n}\r\n"
language: css
libraries: |
// Office.js
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
// CSS Libraries
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
// NPM libraries
[email protected]/client/core.min.js
@microsoft/[email protected]/dist/office.helpers.min.js
[email protected]
// IntelliSense: @types/library or node_modules paths or URL to d.ts files
@types/office-js
@types/core-js
@microsoft/office-js-helpers/dist/office.helpers.d.ts
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment