Skip to content

Instantly share code, notes, and snippets.

@p15martin
Created August 13, 2020 16:25
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 p15martin/734d6ca6ff776d619c44e08444799041 to your computer and use it in GitHub Desktop.
Save p15martin/734d6ca6ff776d619c44e08444799041 to your computer and use it in GitHub Desktop.
Clear and untrack a range
name: Clear and untrack a range
description: Clear and untrack a range
host: EXCEL
api_set: {}
script:
content: |
$("#setup").click(() => tryCatch(setup));
$("#insert-range").click(() => tryCatch(insertRange));
$("#delete-range").click(() => tryCatch(deleteRange));
$("#clear-range").click(() => tryCatch(clearRange));
async function insertRange() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
await unprotect(context, sheet)
const range = sheet.getRange("B4:E4");
range.insert(Excel.InsertShiftDirection.down);
await context.sync();
await protect(context, sheet)
});
}
async function deleteRange() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
const range = sheet.getRange("B4:E4");
range.delete(Excel.DeleteShiftDirection.up);
await context.sync();
});
}
async function clearRange() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("E2:E5");
range.clear(Excel.ClearApplyTo.contents);
range.untrack();
await context.sync();
});
}
async function setup() {
await Excel.run(async (context) => {
context.workbook.worksheets.getItemOrNullObject("Sample").delete();
const sheet = context.workbook.worksheets.add("Sample");
const data = [
["Product", "Qty", "Unit Price", "Total Price"],
["Almonds", 2, 7.5, "=C3 * D3"],
["Coffee", 1, 34.5, "=C4 * D4"],
["Chocolate", 5, 9.56, "=C5 * D5"]
];
const range = sheet.getRange("B2:E5");
range.values = data;
range.format.autofitColumns();
const header = range.getRow(0);
header.format.fill.color = "#4472C4";
header.format.font.color = "white";
sheet.activate();
await context.sync();
await protect(context, sheet)
});
}
async function protect(context, sheet) {
const protectOptions = { allowEditObjects: true }
sheet.load('protection/protected')
await context.sync()
if (!sheet.protection.protected) {
sheet.protection.protect(protectOptions)
}
}
async function unprotect(context, sheet) {
sheet.load('protection/protected')
await context.sync()
if (sheet.protection.protected) {
sheet.protection.unprotect()
}
}
/** 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 insert, delete and clear the contents of a range.</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=\"insert-range\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Insert range</span>\n </button>\n\t<button id=\"delete-range\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Delete range</span>\n </button>\n\t<button id=\"clear-range\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Clear range</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