Sub topic 12
Set Validation Rules with Apps Script
This chapter assumes that you can select ranges in Google Sheets, enter basic formulas, and use absolute and relative references such as $A$2 and A2. It also assumes that you can open the Apps Script editor from Extensions > Apps Script.
Apps Script is Google’s JavaScript-based automation environment for Google Workspace. You can use it to create, update, and remove data validation rules without configuring every range manually.
Create a list-based validation rule
The SpreadsheetApp service gives a script access to the current spreadsheet. The newDataValidation() method starts building a validation rule. You then choose a criterion, configure the rule, and finish it with build().
This function puts a three-item drop-down in cells C2:C100 on the active sheet:
function addStatusDropdown() {
const sheet = SpreadsheetApp.getActiveSheet();
const rule = SpreadsheetApp.newDataValidation()
.requireValueInList(['Not started', 'In progress', 'Done'], true)
.setAllowInvalid(false)
.setHelpText('Pick a status from the list.')
.build();
sheet.getRange('C2:C100').setDataValidation(rule);
}requireValueInList() receives an array of allowed values. Its second argument, true, displays the values as a drop-down. setAllowInvalid(false) rejects an entry that is not in the list. setHelpText() adds guidance that appears when a user interacts with the cell.
To use the function:
- Open Extensions > Apps Script.
- Replace the editor’s sample function with the code.
- Save the project.
- Select
addStatusDropdownin the function menu. - Click Run and approve the requested permissions.
The first run usually asks for authorization because the script changes spreadsheet content.
Use a range as the source
A typed list works well for a short, stable set of choices. For a list that changes, store the choices in cells and use requireValueInRange().
function addCategoryDropdown() {
const spreadsheet = SpreadsheetApp.getActive();
const sheet = spreadsheet.getSheetByName('Orders');
const lists = spreadsheet.getSheetByName('Lists');
const rule = SpreadsheetApp.newDataValidation()
.requireValueInRange(lists.getRange('A2:A20'), true)
.setAllowInvalid(false)
.setHelpText('Choose a category from the list.')
.build();
sheet.getRange('B2:B100').setDataValidation(rule);
}The rule is applied to Orders!B2:B100, while the allowed values come from Lists!A2:A20. Keeping options on a separate sheet makes maintenance easier. You can add, rename, or remove options there without editing the script.
The script stores objects for the spreadsheet, destination sheet, and source sheet in separate constants. This makes the relationship between the source range and destination range clear.
Use other validation criteria
The builder supports criteria beyond drop-downs. For example, these rules require a number and a date:
function addNumberAndDateRules() {
const sheet = SpreadsheetApp.getActiveSheet();
const quantityRule = SpreadsheetApp.newDataValidation()
.requireNumberBetween(1, 1000)
.setAllowInvalid(false)
.setHelpText('Enter a whole quantity from 1 to 1000.')
.build();
const dateRule = SpreadsheetApp.newDataValidation()
.requireDateBetween(new Date('2025-01-01'), new Date())
.setAllowInvalid(false)
.setHelpText('Enter a date from 1 January 2025 through today.')
.build();
sheet.getRange('D2:D100').setDataValidation(quantityRule);
sheet.getRange('E2:E100').setDataValidation(dateRule);
}Choose the builder method that matches the rule you need, such as requireNumberBetween(), requireTextIsEmail(), or requireCheckbox(). Validation criteria are checked when a user enters or edits a value.
Remove validation without deleting values
clearDataValidations() removes the rules from a range but leaves the existing values in place:
function clearStatusValidation() {
SpreadsheetApp.getActiveSheet()
.getRange('C2:C100')
.clearDataValidations();
}This is useful when you need to change the design of an input area. Clear the old rules, create a new rule, and apply it to the intended range.
Understand the script’s execution flow
Exercise
Change addStatusDropdown() so that it applies the same rule to D2:D50 and allows invalid values with a warning.
Show answer
Change the destination range to D2:D50 and replace .setAllowInvalid(false) with .setAllowInvalid(true). The drop-down remains available, but other values produce a warning instead of being rejected.