Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save theboyknowsclass/4447811f6b8d4a07304239d1317637d0 to your computer and use it in GitHub Desktop.
Save theboyknowsclass/4447811f6b8d4a07304239d1317637d0 to your computer and use it in GitHub Desktop.
Cell Changed with Data Validation
name: Cell Changed with Data Validation
description: Cell Changed with Data Validation
host: EXCEL
api_set: {}
script:
content: |
$("#register-click-handler").click(() => tryCatch(registerClickHandler));
$("#disable-click-handler").click(() => tryCatch(remove));
const myCellAddress = "E2";
const generateList = (size) => {
let list = "item 1"
for(let i = 2; i <= size; i++)
{
list += ",item " + i;
}
return list;
}
const sheetList = "=Lists!$A$1:$A$3"
let eventResult;
let clickHandlerEventResult;
async function registerClickHandler() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
eventResult = sheet.onSelectionChanged.add(handleSelectionChange);
await context.sync();
console.log("Event handler successfully registered for onSelectionChanged event in the worksheet.");
});
}
async function remove() {
await Excel.run(eventResult.context, async (context) => {
eventResult.remove();
await context.sync();
eventResult = null;
console.log("Event handler successfully removed.");
});
}
async function handleSelectionChange(event) {
const list = generateList(1748);
console.log("list length", list.length)
await Excel.run(async (context) => {
await context.sync();
if (event.address === "E2") {
const range = context.workbook.getSelectedRange();
range.dataValidation.rule = {
list: {
inCellDropDown: true,
source: list
}
};
} else {
console.log("removing");
const sheet = context.workbook.worksheets.getActiveWorksheet();
let range = sheet.getRange("B2:C5");
range.dataValidation.clear();
}
console.log("Address of current selection: " + event.address);
});
}
/** 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 register and use a handler for the single-click event.</p>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p>Use the button below to register the event handler. Then, left-click around the worksheet and check the console.\n\t</p>\n\t<button id=\"register-click-handler\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Register click handler</span>\n </button>\n\t<button id=\"disable-click-handler\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Disable click handler</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
[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