数据验证:下拉列表、提示和警告弹出窗口
创作时间:
作者:
@小白创作中心
数据验证:下拉列表、提示和警告弹出窗口
引用
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);
}
本文原文来自微软官方文档
热门推荐
比亚迪“花式”补贴、特斯拉限时优惠……“以旧换新”算是被车企玩明白了
Excel表格求和6种方法详解:从快捷键到跨表格汇总
酱油安全检测必测的20项指标清单
这个“红娘”不简单
海浪拍岸、黎歌悠扬……“声游海南”专列来了!
荷叶象征的美好寓意(从古代到现代)
Pi Day 看看我们曾分享过那些关于 π 的文章与视频
电脑文件怎么加密并设置密码?实用的四种方法
狗狗的耳朵种类有哪些
三所学校打造书香校园的创新实践
固态硬盘知识科普:坏块的产生、坏块表简介、固态健康度检测及坏固态修复
存储重要数据用固态移动硬盘好还是机械移动硬盘好?
墙面霉菌清除攻略:实用除霉技巧大公开
考研复试完全指南:从面试攻略到常见问题解答
腊八粥也有“甜咸之争”?除了喝粥,腊八节美食还有其它打开方式
静宁果农王恩科:什么是秸秆还田技术
光的行为与原理
现代战舰新手攻略 现代战舰手游新人如何开荒
非氧化性杀菌剂对污水生化系统影响
地理、历史与潮汕文化
Excel作文纸格式制作指南
如何用AI比较产品质量?简单好学!
别再催了! 你的“时间管理”,可能正在毁掉孩子的时间观!
黑鱼的营养价值与食用方法:从功效到注意事项全解析
交强险计算方式详解:基础费率、出险影响及赔偿限额全解析
2025年交强险收费标准最新政策,各种车型明确要求
揭秘红点奖:3%获奖率背后的设计传奇
两岁小孩动脉导管未闭的四种治疗方案
强化学习全解析:从基础概念到前沿方法
如何高效制定每周工作计划安排及完成情况报告?