Skip to content

Instantly share code, notes, and snippets.

@lumine2008
Last active August 25, 2021 14:44
Show Gist options
  • Save lumine2008/732518b3e1de3633992be1c558e29a7e to your computer and use it in GitHub Desktop.
Save lumine2008/732518b3e1de3633992be1c558e29a7e 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));
var newBehavior = true;
async function run() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets;
sheet.onProtectionChanged.add((event) => {
console.log(event.isProtected);
protectionChanged(event.isProtected);
console.log(JSON.stringify(event));
});
console.log("registered successfully");
var protection = sheet.getActiveWorksheet().protection;
protection.load();
await context.sync();
if (!newBehavior) return;
document.getElementById("run").disabled = true;
if (protection.protected ){
document.getElementById("protect").disabled = true;
document.getElementById("unprotect").disabled = false;
}else{
document.getElementById("protect").disabled = false;
document.getElementById("unprotect").disabled = true;
}
});
}
async function protectionChanged(protectionStatus) {
if (!newBehavior) return;
if (protectionStatus) {
document.getElementById("protect").disabled = true;
document.getElementById("unprotect").disabled = false;
document.getElementById("run2").disabled = true;
} else {
document.getElementById("protect").disabled = false;
document.getElementById("unprotect").disabled = true;
document.getElementById("run2").disabled = false;
}
}
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();
}
});
}
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();
}
});
}
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://appsforoffice.microsoft.com/lib/beta/hosted/office.js
@types/office-js-preview
[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