-
-
Notifications
You must be signed in to change notification settings - Fork 36
newDataValidation
Julian Halliwell edited this page Mar 6, 2024
·
5 revisions
Returns a new dataValidation object to be applied to a workbook.
newDataValidation()
- DataValidations allow you to restrict the values which can be entered in specific cells, typically via a drop-down UI.
- The allowed values can be specified as an array, or can be pulled from existing cells in the same or a different sheet in the workbook.
- The object allows you to customize the error box which pops up when invalid values are entered.
- The drop-down UI is optional.
- DataValidations are created using a "builder" syntax (see examples below).
- You can apply the validation object either by passing in the workbook to the
addToWorkbook()
builder method, or by returning the new DataValidation object and passing it to the library's addDataValidation() method. - New in version 4.0: Date and integer range validations - specify minimum and maximum dates or integers (see examples 6 and 7 below).
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withValues( [ "London", "Paris", "New York" ] )
.addToWorkbook( wb );
wb = spreadsheet.new();
// the first column will contain the allowed values
spreadsheet.addColumn( wb, [ "London", "Paris", "New York" ] );
spreadsheet.newDataValidation()
.onCells( "B1:C1" )
.withValuesFromCells( "A1:A3" )
.addToWorkbook( wb );
wb = spreadsheet.new();
// create a sheet with the valid values in the first column
spreadsheet.createSheet( wb, "cities" );
spreadsheet.setActiveSheetNumber( wb, 2 );
spreadsheet.addColumn( wb, [ "London", "Paris", "New York" ] );
// create and apply the dataValidation object to the first sheet
spreadsheet.setActiveSheetNumber( wb, 1 );
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withValuesFromSheetName( "cities" )
.withValuesFromCells( "A1:A3" )
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withValues( [ "London", "Paris", "New York" ] )
.withErrorTitle( "City not allowed" )
.withErrorMessage( "Please choose from the list of allowed cities" )
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withValues( [ "London", "Paris", "New York" ] )
.withNoDropdownArrow()
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withMinDate( CreateDate( 2020, 1, 1 ) )
.withMaxDate( CreateDate( 2020, 12, 31 ) )
.withErrorTitle( "Invalid date" )
.withErrorMessage( "Please enter a date in 2020" )
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withMinInteger( 1 )
.withMaxInteger( 100 )
.withErrorTitle( "Invalid number" )
.withErrorMessage( "Please enter a whole number between 1 and 100" )
.addToWorkbook( wb );