Created
February 16, 2023 15:27
-
-
Save theboyknowsclass/eaa70e12c00edcbfbec35289cdb34c43 to your computer and use it in GitHub Desktop.
Cell Changed with Data Validation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Cell Changed with Data Validation | |
description: Cell Changed with Data Validation | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#register-click-handler").click(() => tryCatch(registerClickHandler)); | |
const myCellAddress = "E2"; | |
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 handleSelectionChange(event) { | |
await Excel.run(async (context) => { | |
await context.sync(); | |
if(event.address === "E2") | |
{ | |
const range = context.workbook.getSelectedRange(); | |
range.dataValidation.rule = { | |
list: { | |
inCellDropDown: true, | |
source: "=Lists!$A$1:$A$3" | |
} | |
}; | |
} | |
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"> | |
<p>This sample shows how to register and use a handler for the single-click event.</p> | |
</section> | |
<section class="samples ms-font-m"> | |
<h3>Try it out</h3> | |
<p>Use the button below to register the event handler. Then, left-click around the worksheet and check the console. | |
</p> | |
<button id="register-click-handler" class="ms-Button"> | |
<span class="ms-Button-label">Register click handler</span> | |
</button> | |
</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