Skip to content

Instantly share code, notes, and snippets.

@areed1192
Created June 16, 2019 23:11
Show Gist options
  • Save areed1192/42570312fb88123124f3fe442565f727 to your computer and use it in GitHub Desktop.
Save areed1192/42570312fb88123124f3fe442565f727 to your computer and use it in GitHub Desktop.
This tutorial covers different operations we can perform on a worksheet using the JavaScript API.
name: Working With Worksheets
description: >-
This tutorial covers different operations we can perform on a worksheet using
the JavaScript API.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
async function run() {
await Excel.run(async (context) => {
// grab the specific worksheets
const sheet1 = context.workbook.worksheets.getItem("SheetOne");
const sheet2 = context.workbook.worksheets.getItem("SheetTwo");
// copy sheet one
sheet1.copy(Excel.WorksheetPositionType.before, sheet2)
// delete the newly copied sheet
context.workbook.worksheets.getActiveWorksheet().delete();
// change the sheet visibility
sheet2.visibility = "Visible";
// turn off gridlines
sheet2.showGridlines = false;
// turn off headers
sheet2.showHeadings = false;
// change the name
// sheet2.load('name');
// await context.sync();
// sheet2.name = "MyNewSheet"
// add a new worksheet
let sheet3 = context.workbook.worksheets.add();
// load a bunch of different properties
sheet3.load(['name','protection','position','verticlaPageBreaks']);
await context.sync();
// console log it
console.log(sheet3.name);
console.log(sheet3.protection);
console.log(sheet3.position);
console.log(sheet3.verticalPageBreaks);
// turn off the calculations, no formulas will work
sheet3.enableCalculation = false;
// grab the freeze pane object
let panes = sheet3.freezePanes;
// freeze the first 4 rows
panes.freezeRows(4);
// freeze the first 2 columns
panes.freezeColumns(2);
// freeze a range of cells
panes.freezeAt("C4:C10");
});
}
/** 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: |
<button id="run" class="ms-Button">
<span class="ms-Button-label">Run</span>
</button>
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