ExcelScript.DataValidation interface

表示应用于当前范围的数据验证。

方法

clear()

清除当前区域中的数据有效性。

getErrorAlert()

用户输入无效数据时,出现错误警报。

getIgnoreBlanks()

指定是否对空白单元格执行数据验证。 默认值为 true

getInvalidCells()

返回一个 RangeAreas 对象,该对象包含一个或多个矩形区域,其单元格值无效。 如果所有单元格值都有效,则此函数将返回 null

getPrompt()

当用户选择单元格时提示。

getRule()

包含不同类型的数据验证条件的数据验证规则。

getType()

数据验证的类型,有关详细信息,请参阅 ExcelScript.DataValidationType

getValid()

表示所有单元格值根据数据有效性规则是否全部有效。 true如果所有单元格值都有效,或者false所有单元格值都无效,则返回 。 null如果区域内同时存在有效单元格值和无效单元格值,则返回 。

setErrorAlert(errorAlert)

用户输入无效数据时,出现错误警报。

setIgnoreBlanks(ignoreBlanks)

指定是否对空白单元格执行数据验证。 默认值为 true

setPrompt(prompt)

当用户选择单元格时提示。

setRule(rule)

包含不同类型的数据验证条件的数据验证规则。

方法详细信息

clear()

清除当前区域中的数据有效性。

clear(): void;

返回

void

getErrorAlert()

用户输入无效数据时,出现错误警报。

getErrorAlert(): DataValidationErrorAlert;

返回

getIgnoreBlanks()

指定是否对空白单元格执行数据验证。 默认值为 true

getIgnoreBlanks(): boolean;

返回

boolean

getInvalidCells()

返回一个 RangeAreas 对象,该对象包含一个或多个矩形区域,其单元格值无效。 如果所有单元格值都有效,则此函数将返回 null

getInvalidCells(): RangeAreas;

返回

getPrompt()

当用户选择单元格时提示。

getPrompt(): DataValidationPrompt;

返回

getRule()

包含不同类型的数据验证条件的数据验证规则。

getRule(): DataValidationRule;

返回

getType()

数据验证的类型,有关详细信息,请参阅 ExcelScript.DataValidationType

getType(): DataValidationType;

返回

示例

/**
 * This sample reads and logs the data validation type of the currently selected range.
 */
function main(workbook: ExcelScript.Workbook) {
  // Get the currently selected range.
  let range = workbook.getSelectedRange();

  // Get the type (`DataValidationType`) of data validation applied to the range.
  let validationType = range.getDataValidation().getType();

  /*
   * Log the data validation type.
   * If the range has a single value, it logs that type.
   * If the range doesn't have data validation applied, it logs "None".
   * If the range has multiple different types of data validation, it logs "Inconsistent" or "MixedCriteria".
   */
  console.log(validationType.toString());
}

getValid()

表示所有单元格值根据数据有效性规则是否全部有效。 true如果所有单元格值都有效,或者false所有单元格值都无效,则返回 。 null如果区域内同时存在有效单元格值和无效单元格值,则返回 。

getValid(): boolean;

返回

boolean

setErrorAlert(errorAlert)

用户输入无效数据时,出现错误警报。

setErrorAlert(errorAlert: DataValidationErrorAlert): void;

参数

返回

void

setIgnoreBlanks(ignoreBlanks)

指定是否对空白单元格执行数据验证。 默认值为 true

setIgnoreBlanks(ignoreBlanks: boolean): void;

参数

ignoreBlanks

boolean

返回

void

setPrompt(prompt)

当用户选择单元格时提示。

setPrompt(prompt: DataValidationPrompt): void;

参数

返回

void

示例

/**
 * 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.
    let selectedSheet = workbook.getActiveWorksheet();
    let 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.
    dataValidation.setPrompt({ 
        showPrompt: true, 
        title: "First names only", 
        message: "Only enter the first name of the employee, not the full name." 
    });
}    

setRule(rule)

包含不同类型的数据验证条件的数据验证规则。

setRule(rule: DataValidationRule): void;

参数

返回

void

示例

/**
 * This script creates a data validation rule for the range B1: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 B1:B5 in the active worksheet.
  const currentSheet = workbook.getActiveWorksheet();
  const positiveNumberOnlyCells = currentSheet.getRange("B1: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);
}