Skip to content

Instantly share code, notes, and snippets.

@rickdg
Created May 8, 2022 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rickdg/a3006e2a69b489f65751c68d88bd9047 to your computer and use it in GitHub Desktop.
Save rickdg/a3006e2a69b489f65751c68d88bd9047 to your computer and use it in GitHub Desktop.
Gets and sets the active worksheet.
name: Active worksheet
description: Gets and sets the active worksheet.
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
$("#get-active-worksheet").click(() => tryCatch(getActiveWorksheet));
$("#set-active-worksheet").click(() => tryCatch(setActiveWorksheet));
async function getActiveWorksheet() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("name");
await context.sync();
console.log(`The active worksheet is "${sheet.name}"`);
});
}
async function setActiveWorksheet() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("name");
const sheets = context.workbook.worksheets;
sheets.load("items");
await context.sync();
let newSheet = sheet;
if (sheets.items.length > 1) {
if (sheet.id === sheets.items[0].id) {
newSheet = sheets.items[1];
} else {
newSheet = sheets.items[0];
}
newSheet.load("name");
newSheet.activate();
await context.sync();
} else {
console.log("Cannot change the active worksheet since there is only one sheet in the workbook");
}
console.log(`The active worksheet is "${newSheet.name}"`);
});
}
async function setup() {
await Excel.run(async (context) => {
const sheets = context.workbook.worksheets;
sheets.load("items");
await context.sync();
if (sheets.items.length < 2) {
sheets.add();
await context.sync();
}
});
}
/** 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 get and set the active worksheet.</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\">Ensure two worksheets</span>\n </button>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<button id=\"get-active-worksheet\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Get active worksheet</span>\n </button>\n\t<button id=\"set-active-worksheet\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Set active worksheet</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
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