Skip to content

Instantly share code, notes, and snippets.

@XiangGaoMSFT
Last active September 7, 2018 04:33
Show Gist options
  • Save XiangGaoMSFT/a36c748e958c1a212d118c1358484954 to your computer and use it in GitHub Desktop.
Save XiangGaoMSFT/a36c748e958c1a212d118c1358484954 to your computer and use it in GitHub Desktop.
Shared with Script Lab
name: Protection.protect()
description: ''
author: XiangGaoMSFT
host: EXCEL
api_set: {}
script:
content: |
$("#protect").click(() => tryCatch(protect));
$("#unprotect").click(() => tryCatch(unprotect));
async function protect() {
await Excel.run(async (context) => {
var sheet = context.workbook.worksheets.getActiveWorksheet();
var sheetRange = sheet.getRange();
sheetRange.format.protection.locked = false;
sheetRange.format.fill.clear();
var protectedRange = sheet.getRange("A1:B5");
protectedRange.format.fill.color = "red";
protectedRange.format.protection.locked = true;
sheet.protection.protect();
await context.sync();
});
}
async function unprotect() {
await Excel.run(async (context) => {
var sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.protection.unprotect();
sheet.getRange().format.fill.clear();
await context.sync();
});
}
/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
try {
await callback();
}
catch (error) {
OfficeHelpers.UI.notify(error);
OfficeHelpers.Utilities.log(error);
}
}
language: typescript
template:
content: |-
<button id="protect" class="ms-Button">
<span class="ms-Button-label">Protect</span>
</button>
<button id="unprotect" class="ms-Button">
<span class="ms-Button-label">Unprotect</span>
</button>
language: html
style:
content: |
/* Your style goes here */
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
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.min.js
@microsoft/office-js-helpers@0.7.4/dist/office.helpers.d.ts
jquery@3.1.1
@types/jquery
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment