Skip to content

Instantly share code, notes, and snippets.

@areed1192
Created May 26, 2019 23:56
Show Gist options
  • Save areed1192/ab867da7eb6292e0fcb81c98dee4cda9 to your computer and use it in GitHub Desktop.
Save areed1192/ab867da7eb6292e0fcb81c98dee4cda9 to your computer and use it in GitHub Desktop.
Formats the vertical and horizontal axes in a chart.
name: Axis formatting
description: Formats the vertical and horizontal axes in a chart.
host: EXCEL
api_set: {}
script:
content: >
$("#setup1").click(() => tryCatch(playWithWorkbook));
$("#setup2").click(() => tryCatch(playWithWorkbookOld));
$("#setup3").click(() => tryCatch(playWithWorkbookProperties));
// define a function that will run all the code related to the workbook.
async function playWithWorkbook() {
// we will use Excel.Run to create a function where we can specify the actions
// we want to perform against the Excel Object model. This will, behind the scenes,
// create a new promise object that will automatically resolve once the code
// finishes running.
// Use the await key word because we want an Async function to behave in a sync
// fashion. In other words if we load properties and such we want that to resolve
// before moving to the next line.
await Excel.run(async (context) => {
// define the workbook
let xlWorkbook = context.workbook;
// load the name property
xlWorkbook.load("name");
// use await to sync the load.
await context.sync();
// Now you can write it.
console.log(`The workbook name is ${xlWorkbook.name}.`);
});
}
// define a function that will run all the code related to the workbook.
// this time instead of doing an async function we will do a regular
function
function playWithWorkbookOld() {
// we will use Excel.Run to create a function where we can specify the actions
// we want to perform against the Excel Object model. This will, behind the scenes,
// create a new promise object that will automatically resolve once the code
// finishes running.
// Here because I didn't use an async function I remove the await keyword, this
// means I will need to load the properties in a different fashion.
Excel.run(function(context) {
// define the workbook
let xlWorkbook = context.workbook;
// load the name property
xlWorkbook.load("name");
// this is the part that is different, here I return the context object, sync it,
// then proceed to the next function which console logs the workbook name.
return context.sync().then(function() {
// Now you can write it.
console.log(`The workbook name is ${xlWorkbook.name}.`);
});
});
}
// Okay let's actually start with actually playing with some properties.
async function playWithWorkbookProperties() {
await Excel.run(async (context) => {
// define the workbook
let xlWorkbook = context.workbook;
// let's take the active cell, reference the entire row, and then select it.
// Make a note when you go to reference selected ranges.
// xlWorkbook.getActiveCell().getEntireRow().select();
// load the name property, the worksheet property, and all of it's children.
xlWorkbook.load(["name", "worksheets/"]);
// let's grab the sheet count, this will return a OfficeExtension.Client Result
// this object has a property called value that we must sync before we call it.
let mySheetCount = xlWorkbook.worksheets.getCount();
// define the selected range
let selectedRangeObj = xlWorkbook.getSelectedRange();
// load the properties of our Range object.
selectedRangeObj.load("cellCount");
// use await to sync the loading of properties and the value property of the
// OfficeExtension.Client Result
await context.sync();
// Now you can write it.
console.log(`The workbook name is ${xlWorkbook.name}.`);
console.log(`There are ${mySheetCount.value} sheets.`);
console.log(`There are ${selectedRangeObj.cellCount} cells.`);
});
}
/** 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=\"setup ms-font-m\">\n\t<button id=\"setup1\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run Workbook Async</span>\n </button>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<button id=\"setup2\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run Workbook No Async</span>\n </button>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<button id=\"setup3\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Run Workbook</span>\n </button>\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
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
@microsoft/[email protected]/dist/office.helpers.min.js
@microsoft/[email protected]/dist/office.helpers.d.ts
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment