数据验证:下拉列表、提示和警告弹出窗口
创作时间:
作者:
@小白创作中心
数据验证:下拉列表、提示和警告弹出窗口
引用
1
来源
1.
https://learn.microsoft.com/zh-cn/office/dev/scripts/resources/samples/data-validation-samples
在Excel中,数据验证是一个非常实用的功能,可以帮助用户确保数据输入的准确性和一致性。本文将通过三个具体示例,介绍如何在Excel中使用数据验证功能:创建下拉列表、添加提示注释以及设置输入错误时的警告。
数据验证:下拉列表、提示和警告弹出窗口
数据验证可帮助用户确保工作表中的一致性。 使用这些功能可以限制可在单元格中输入的内容,并在不符合这些条件时向用户提供警告或错误。 若要详细了解 Excel 中的数据验证,请参阅向单元格应用数据验证。
使用数据验证创建下拉列表
以下示例为单元格创建下拉列表。 它使用所选区域的现有值作为列表的选项。
function main(workbook: ExcelScript.Workbook) {
// Get the values for data validation.
const selectedRange = workbook.getSelectedRange();
const rangeValues = selectedRange.getValues();
// Convert the values into a comma-delimited string.
let dataValidationListString = "";
rangeValues.forEach((rangeValueRow) => {
rangeValueRow.forEach((value) => {
dataValidationListString += value + ",";
});
});
// Clear the old range.
selectedRange.clear(ExcelScript.ClearApplyTo.contents);
// Apply the data validation to the first cell in the selected range.
const targetCell = selectedRange.getCell(0,0);
const dataValidation = targetCell.getDataValidation();
// Set the content of the dropdown list.
dataValidation.setRule({
list: {
inCellDropDown: true,
source: dataValidationListString
}
});
}
向区域添加提示
此示例创建一个提示注释,当用户输入给定单元格时显示该注释。 这用于提醒用户有关输入要求的信息,而无需严格执行。
/**
* This script creates a text prompt that's shown in C2:C8 when a user enters the cell.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the data validation object for C2:C8 in the current worksheet.
const selectedSheet = workbook.getActiveWorksheet();
const dataValidation = selectedSheet.getRange("C2:C8").getDataValidation();
// Clear any previous validation to avoid conflicts.
dataValidation.clear();
// Create a prompt to remind users to only enter first names in this column.
const prompt: ExcelScript.DataValidationPrompt = {
showPrompt: true,
title: "First names only",
message: "Only enter the first name of the employee, not the full name."
}
dataValidation.setPrompt(prompt);
}
输入无效数据时向用户发出警报
以下示例脚本阻止用户将除正数以外的任何内容输入到某个范围中。 如果他们尝试放置任何其他内容,将弹出错误消息并指示问题。
/**
* This script creates a data validation rule for the range B2:B5.
* All values in that range must be a positive number.
* Attempts to enter other values are blocked and an error message appears.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range B2:B5 in the active worksheet.
const currentSheet = workbook.getActiveWorksheet();
const positiveNumberOnlyCells = currentSheet.getRange("B2:B5");
// Create a data validation rule to only allow positive numbers.
const positiveNumberValidation: ExcelScript.BasicDataValidation = {
formula1: "0",
operator: ExcelScript.DataValidationOperator.greaterThan
};
const positiveNumberOnlyRule: ExcelScript.DataValidationRule = {
wholeNumber: positiveNumberValidation
};
// Set the rule on the range.
const rangeDataValidation = positiveNumberOnlyCells.getDataValidation();
rangeDataValidation.setRule(positiveNumberOnlyRule);
// Create an alert to appear when data other than positive numbers are entered.
const positiveNumberOnlyAlert: ExcelScript.DataValidationErrorAlert = {
message: "Positive numbers only.",
showAlert: true,
style: ExcelScript.DataValidationAlertStyle.stop,
title: "Invalid data"
};
rangeDataValidation.setErrorAlert(positiveNumberOnlyAlert);
}
本文原文来自微软官方文档
热门推荐
干细胞移植费用及治疗疾病详解
干细胞移植治疗肺纤维化费用详解
干细胞移植费用多少,哪些因素会影响费用
过年怎么吃喝才健康?
今年猪肉价格便宜,为啥做腊肠的减少了?肉贩:4个原因,很现实
腊肠的营养功效与选购技巧
情绪波动延缓眼袋手术恢复,或致肿胀出血风险增加
瓜子金:一种具有多种药用价值的植物
家庭燃气安全使用指南:从检测到应急处理
三招解决燃气灶打火难题,附安全使用要点
二手交易指南:线上平台 vs 线下市场,如何选择更安全
新手机自带软件清理攻略:Android、iOS操作指南
安卓iOS双平台:自带软件清理全攻略
深圳酒店现粪便污渍,律师解析:精神损失赔偿该赔多少
住酒店如何防范隐私泄露?三个实用检查方法
中国三大侏罗纪世界:云南、川渝、辽宁的恐龙传奇
墨西哥发现7200万年前暴龙科新成员,填补演化空白
解读乙肝表面抗体:高数值意味着免疫力良好
兼职副业:利用社交媒体发现商业机会
中国新型商业模式为全球经济与贸易提供了哪些机遇?
创业小白必看:如何快速上手并赚钱的小本生意指南
趣味历史课:凯叔马未都讲述636集中国通史
韩三平掌舵中影:中国电影票房增长17倍,银幕数超8万
从峨眉厂到中影集团,韩三平引领中国电影产业化转型
韩三平:从照明工到中影掌门人,见证中国电影黄金十年
解构三大二手平台模式,AI大模型带来交易新可能
多方联动规范二手家电市场,国家标准制定提速
工伤赔偿仲裁指南:8步走完申请到裁决全流程
工伤保险赔偿标准全解析:辽宁南京案例对比
嗓子疼爆红热搜,这些食物救星了解一下?