Skip to content

Instantly share code, notes, and snippets.

@lumine2008
Last active August 24, 2021 08:03
Show Gist options
  • Save lumine2008/38b0fa4d1f28cf8869ab4472009dddfc to your computer and use it in GitHub Desktop.
Save lumine2008/38b0fa4d1f28cf8869ab4472009dddfc to your computer and use it in GitHub Desktop.
Create a new snippet from a blank template.
name: Worksheet protection event
description: Create a new snippet from a blank template.
host: EXCEL
api_set: {}
script:
content: |
$("#run").click(() => tryCatch(run));
$("#protect").click(() => tryCatch(protect));
$("#unprotect").click(() => tryCatch(unprotect));
$("#run2").click(() => tryCatch(run2));
$("#run3").click(() => tryCatch(run3));
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets;
sheet.onProtectionChanged.add((event) => {
console.log(JSON.stringify(event));
});
document.getElementById("run").disabled = true;
document.getElementById("protect").disabled = false;
document.getElementById("unprotect").disabled = false;
console.log("registered successfully");
});
}
async function protect() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("protection/protected");
await context.sync();
if (!sheet.protection.protected) {
sheet.protection.protect();
document.getElementById("protect").disabled = true;
document.getElementById("unprotect").disabled = false;
document.getElementById("run2").disabled = true;
}
});
}
async function unprotect() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
sheet.load("protection/protected");
await context.sync();
if (sheet.protection.protected) {
sheet.protection.unprotect();
document.getElementById("protect").disabled = false;
document.getElementById("unprotect").disabled = true;
document.getElementById("run2").disabled = false;
}
});
}
async function run2() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
let pw = $("#password")
.val()
.toString();
var r = sheet.getRange("B3");
r.load();
await context.sync();
r.values = pw;
});
}
/** 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">Register event</span>
</button>
<button id="protect" class="ms-Button">
<span class="ms-Button-label">Protect current sheet</span>
</button>
<button id="unprotect" class="ms-Button">
<span class="ms-Button-label">Unprotect current sheet</span>
</button>
<h4> Additional Dimension </h4>
<input id="password" size="5"/>
<button id="run2" class="ms-Button">
<span class="ms-Button-label">Add Dimension</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://unpkg.com/@microsoft/[email protected]/dist/office.js
https://unpkg.com/@microsoft/[email protected]/dist/office.d.ts
[email protected]/dist/css/fabric.min.css
[email protected]/dist/css/fabric.components.min.css
[email protected]/client/core.min.js
@types/core-js
[email protected]
@types/[email protected]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment