Skip to content

Instantly share code, notes, and snippets.

@lumine2008
Created February 10, 2020 12:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lumine2008/827ab26a65b76a5826331d960323c43b to your computer and use it in GitHub Desktop.
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.
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
office-ui-fabric-js@1.4.0/dist/css/fabric.min.css
office-ui-fabric-js@1.4.0/dist/css/fabric.components.min.css
core-js@2.4.1/client/core.min.js
@types/core-js
jquery@3.1.1
@types/jquery@3.3.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment