数据验证:下拉列表、提示和警告弹出窗口
创作时间:
作者:
@小白创作中心
数据验证:下拉列表、提示和警告弹出窗口
引用
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);
}
本文原文来自微软官方文档
热门推荐
给预付费“上锁” ,数字人民币智能应用守护消费者权益
东晋王朝的短暂统治者——司马丕
每 10 人中就有 1 人患有肾脏病!了解肾脏知识,早检查,保健康
2024年中国二次元产业发展历程剖析:从亚文化到主流
什么是润滑油等级?一文读懂润滑油的分类与选择要点
基础摄影知识:光圈、快门和ISO的关系
期货空单的定义是什么?期货空单的操作策略有哪些?
收到违章停车单告知单怎么处理
美国第六代战斗机F-47的战略博弈与隐忧挑战
花开不败春满城 | 昆明旅游超全攻略,解锁自然与人文的双重魅力
心血管常用药物服用时间全解析:阿司匹林、降压药、他汀类药物何时服用效果最佳?
三大运营商保号套餐全攻略:5/8元保底,每月省百元
业主如何打赢物业合同纠纷赔偿
兰州中川国际机场三期扩建工程建成投运
机器人与“脊髓假体”首次无缝集成,可恢复瘫痪者运动能力
王维10首经典禅意诗:行到水穷处,坐看云起时!首首有名句,一首一禅理!
-3%→9.99%!A股,尾盘异动!
计算机网络物理层全解析:从线缆到信号的奥秘
二两九钱男命详解:运势、事业、感情、财运全解析
武汉租房攻略:从找房到入住全方位指南
HKICPA报名费全解析:费用明细、缴纳与退费流程
劳务外包和劳务派遣的区别及时间规定
杭州市消保委发布物业服务质量评测报告:高价小区在三方面需改进
美"尼米兹"号航母明年转隶诺福克 进入退役流程
生产车间员工效率提升:计件工资怎么算才公平?送你一套可落地的薪酬设计模型
眼睛复视能自己恢复吗?不同原因的处理方法详解
从“抢车位”到“智慧停”, 智能泊位管理器让城市停车更智能、更从容
osu!游戏模式和玩法详解
针灸减肥:效果与注意事项全解析
針灸原理功效中醫全解構|健脾助減肥、改善暗瘡臭狐|梅花針解決脫髮問題