数据验证:下拉列表、提示和警告弹出窗口
创作时间:
作者:
@小白创作中心
数据验证:下拉列表、提示和警告弹出窗口
引用
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);
}
本文原文来自微软官方文档
热门推荐
苗木包装与运输6大要领,要记牢
眉毛有白色的怎么回事
如何通过数据分析优化项目采购决策?
一文了解高低温对锂电池性能的影响
痛经怎么缓解?喝红糖水能止痛吗?
种植牙大揭秘:种牙等待三个月和“即拔即种”到底是什么意思?
allon4种植牙是固定的吗?allon4种植牙技术详解
当儿女不尊重你不理你时,不要愤怒,也不要讲道理,记住6个字就够了
移民国家与非移民国家:政策、文化与生活差异的深度剖析
C语言如何实现互斥
Excel中调出十字光标的方法与技巧
Beckmann重排反应:有机合成中的酰胺合成途径
物企降本增效实战案例:专注四个方面优化
从北京南站到天坛口腔医院的交通方法
血常规与生化检查:详细对比与应用范围
如何投诉法院立案窗口及正确走法律程序立案
暖气片 vs 地暖:两种主流取暖方式的全面对比
狗狗和猫咪上床睡觉“很脏”?其实有4个好处很明显,但要注意这6点
PPT设计原则:从理论到实践的全面指南
升压电路的基本原理是什么?
朱峥嵘:医学与足球领域的双料精英
英国电脑怎么设置中文
网络诈骗报案需提供哪些证据材料
中国团队研发出耐高温电池隔膜制备新工艺 助力提升锂电池安全性
银行的中间业务包括哪些?
聚氨酯地坪漆有什么特点和作用
甲亢患者的饮食指南:8个实用建议助力病情控制
怎么对付欠钱不还的人
隐血是什么原因造成的
C++一分钟之-数组与指针基础