Skip to content

Instantly share code, notes, and snippets.

@ShellyShulei
Created December 9, 2019 18:32
Show Gist options
  • Select an option

  • Save ShellyShulei/a7d2cb40bfaaf2fe1ce0a2dc95c623e1 to your computer and use it in GitHub Desktop.

Select an option

Save ShellyShulei/a7d2cb40bfaaf2fe1ce0a2dc95c623e1 to your computer and use it in GitHub Desktop.
Orders the plotting of series in a chart.
name: search insert value
description: Orders the plotting of series in a chart.
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
$("#order-series-plot").click(() => tryCatch(addSeries));
$("#findCompleted").click(() => tryCatch(findCompleted));
$("#findinsert").click(() => tryCatch(findInserted));
async function findCompleted() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const foundRanges = sheet.findAllOrNullObject("Brake levers", {
completeMatch: true,
matchCase: false
});
await context.sync();
if (foundRanges.isNullObject) {
console.log("No complete projects");
} else {
foundRanges.format.fill.color = "red";
}
});
}
async function findInserted() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
// NOTE: In this sample, there are no canceled projects.
// Calling sheet.findAll(...) instead of sheet.findAllOrNullObject(...)
// would throw an ItemNotFound error.
console.log($("#bravo").val());
let s = $("#bravo")
.val()
.toString();
const foundRanges = sheet.findAllOrNullObject(s, {
completeMatch: true,
matchCase: false
});
await context.sync();
if (foundRanges.isNullObject) {
console.log("No complete projects");
} else {
foundRanges.format.fill.color = "red";
}
});
}
async function addSeries() {
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let seriesCollection = sheet.charts.getItemAt(0);
let rangeSelection = sheet.getRange("C2:C7");
let xRangeSelection = sheet.getRange("A1:A7");
// Add series.
let newSeries = seriesCollection.series.add("Qtr2");
newSeries.setValues(rangeSelection);
newSeries.setXAxisValues(xRangeSelection);
let newSeries2 = seriesCollection.series.add("Qtr3");
newSeries2.setValues(rangeSelection);
newSeries2.setXAxisValues(xRangeSelection);
// Order the plotting of the series.
// In this case, Qtr 3 appears first,
// followed by Qtr 2, then Qtr 1.
newSeries2.plotOrder = 0;
newSeries.plotOrder = 1;
await context.sync();
});
}
async function setup() {
await Excel.run(async (context) => {
context.workbook.worksheets.getItemOrNullObject("Sample").delete();
const sheet = context.workbook.worksheets.add("Sample");
let salesTable = sheet.tables.add("A1:E1", true);
salesTable.name = "SalesTable";
salesTable.getHeaderRowRange().values = [["Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"]];
salesTable.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]
]);
sheet.getUsedRange().format.autofitColumns();
sheet.getUsedRange().format.autofitRows();
createChart(context);
sheet.activate();
await context.sync();
});
}
async function createChart(context: Excel.RequestContext) {
const sheet = context.workbook.worksheets.getItem("Sample");
const salesTable = sheet.tables.getItem("SalesTable");
let dataRange = sheet.getRange("A1:B7");
let chart = sheet.charts.add("ColumnClustered", dataRange, "Auto");
chart.setPosition("A15", "E30");
chart.legend.position = "Right";
chart.legend.format.fill.setSolidColor("white");
chart.dataLabels.format.font.size = 15;
chart.dataLabels.format.font.color = "black";
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
} catch (error) {
// Note: In a production add-in, you'd want to notify the user through your add-in's UI.
console.error(error);
}
}
language: typescript
template:
content: "<section class=\"ms-font-m\">\n\t<p>This sample shows how to change the plot order of series in a chart.</p>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<h3>Set up</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Add sample data</span>\n </button>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<button id=\"order-series-plot\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Order series plot</span>\n </button>\n\t<h3>Try it out</h3>\n\t<button id=\"findCompleted\" class=\"ms-Button\">\n\t\t <span class=\"ms-Button-label\">Highlight complete projects</span>\n\t\t </button>\n\t<p />\n\n\t<button id=\"findinsert\" class=\"ms-Button\">\n\t\t <span class=\"ms-Button-label\">What do you want to find?</span>\n\t\t </button>\n\t<input id=\"bravo\"></input>\n</section>"
language: html
style:
content: |-
section.samples {
margin-top: 20px;
}
section.samples .ms-Button, section.setup .ms-Button {
display: block;
margin-bottom: 5px;
margin-left: 20px;
min-width: 80px;
}
language: css
libraries: |
https://appsforoffice.microsoft.com/lib/1/hosted/office.js
@types/office-js
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment