Created
February 10, 2020 12:56
-
-
Save lumine2008/827ab26a65b76a5826331d960323c43b to your computer and use it in GitHub Desktop.
Sets data validation rules on ranges, prompts users to enter valid data, and displays messages when invalid data is entered.
This file contains 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: 'How to add Data validation ' | |
description: >- | |
Sets data validation rules on ranges, prompts users to enter valid data, and | |
displays messages when invalid data is entered. | |
host: EXCEL | |
api_set: {} | |
script: | |
content: | | |
$("#setup").click(() => tryCatch(setup)); | |
$("#require-approved-location").click(() => tryCatch(requireApprovedName)); | |
async function requireApprovedName() { | |
await Excel.run(async (context) => { | |
const sheet = context.workbook.worksheets.getItem("Decision"); | |
const nameRange = sheet.tables | |
.getItem("NameOptionsTable") | |
.columns.getItem("Status") | |
.getDataBodyRange(); | |
// When you are developing, it is a good practice to | |
// clear the dataValidation object with each run of your code. | |
nameRange.dataValidation.clear(); | |
const nameSourceRange = context.workbook.worksheets.getItem("Status").getRange("A1:A3"); | |
let approvedListRule = { | |
list: { | |
inCellDropDown: true, | |
source: nameSourceRange | |
} | |
}; | |
nameRange.dataValidation.rule = approvedListRule; | |
await context.sync(); | |
}); | |
} | |
async function setup() { | |
await Excel.run(async (context) => { | |
context.workbook.worksheets.getItemOrNullObject("Decision").delete(); | |
const decisionSheet = context.workbook.worksheets.add("Decision"); | |
const optionsTable = decisionSheet.tables.add("A1:C4", true /*hasHeaders*/); | |
optionsTable.name = "NameOptionsTable"; | |
optionsTable.showBandedRows = false; | |
optionsTable.getHeaderRowRange().values = [["Status", "Ranking", "Comments"]]; | |
decisionSheet.getUsedRange().format.autofitColumns(); | |
decisionSheet.getUsedRange().format.autofitRows(); | |
// The names that will be allowed in the Baby Name column are | |
// listed in a range on the Names sheet. | |
context.workbook.worksheets.getItemOrNullObject("Status").delete(); | |
const namesSheet = context.workbook.worksheets.add("Status"); | |
namesSheet.getRange("A1:A3").values = [["Selected"], ["Rejected"], ["On-hold"]]; | |
decisionSheet.activate(); | |
await context.sync(); | |
}); | |
} | |
/** 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 class=\"ms-font-m\">This sample shows how to apply data validation to cells.</p>\n</section>\n\n<section class=\"setup ms-font-m\">\n\t<h3>Set up</h3>\n\t<button id=\"setup\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Add table</span>\n </button>\n</section>\n\n<section class=\"samples ms-font-m\">\n\t<h3>Try it out</h3>\n\t<p class=\"ms-font-m\">Press <b>Status</b> and then click on a cell in the <b>Status</b> column and\n\t\tuse the drop down to enter an approved value.</p>\n\t<button id=\"require-approved-location\" class=\"ms-Button\">\n <span class=\"ms-Button-label\">Require Status name</span>\n </button>\n\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