Skip to content

Instantly share code, notes, and snippets.

@areed1192
Created May 23, 2019 16:01
Show Gist options
  • Save areed1192/d3b2b0001cfc5367977037c286e0cf34 to your computer and use it in GitHub Desktop.
Save areed1192/d3b2b0001cfc5367977037c286e0cf34 to your computer and use it in GitHub Desktop.
This walks through the worksheet object.
name: Using the Worksheet
description: This walks through the worksheet object.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(worksheetObject));
async function worksheetObject() {
await Excel.run(async (context) => {
// define our workbook
const xlWorkbook = context.workbook;
// define our worksheet
//const xlWorksheet = xlWorkbook.worksheets.getItem("Workbook Object");
const xlWorksheet = xlWorkbook.worksheets.getActiveWorksheet();
// select the used range.
xlWorksheet.getUsedRange().select();
// select the last column in the used range.
xlWorksheet
.getUsedRange()
.getLastColumn()
.select();
// select the last row in the used range.
xlWorksheet
.getUsedRange()
.getLastRow()
.select();
// select a single cell
//xlWorksheet.getCell(7, 3).select();
//load a property of our worksheet.
xlWorksheet.load("name");
await context.sync();
// print it out
console.log(xlWorksheet.name);
// copy our worksheet
//xlWorksheet.copy();
// turn off the gridlines
xlWorksheet.showGridlines = false;
await context.sync();
const xlRange = xlWorksheet.getRange("B15:D17");
xlRange.load("cellCount");
await context.sync();
for (var cell = 0; cell < xlRange.cellCount; cell++) {
xlWorksheet.getCell(7, 2).select();
console.log(cell);
}
// Select a sheet using key.
//xlWorkbook.worksheets.getItem("Range Object").activate();
});
}
/** 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