-
-
Notifications
You must be signed in to change notification settings - Fork 39
newDataValidation
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.
- You can define the criteria for what the cells can accept in various ways:
- by providing an array of acceptable values;
- by specifying existing cells in the same or a different sheet in the workbook which contain the acceptable values;
- by specifying the minimum and/or maximum acceptable values;
- by specifying a formula which must evaluate to true for the value to be accepted, e.g.
ISODD(A1)
to only allow odd numbers.
- The object allows you to customize the error alert which pops up when invalid values are entered.
- For sets of values an optional drop-down will be provided in the UI.
- 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.
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withValues( [ "London", "Paris", "New York" ] )
.addToWorkbook( wb );
// alternative syntax
dv = spreadsheet.newDataValidation()
.onCells( "A1:B1" )
.withValues( [ "London", "Paris", "New York" ] );
wb = spreadsheet.new();
spreadsheet.addDataValidation( wb, dv );
The following methods are available to build and apply your desired DataValidation.
Applies the current DataValidation object to the specified workbook object.
The cell or range of cells to which the validation will apply, eg "A1:E1".
Set this to false
to override the default behaviour and require a value to be entered.
The message used in the body of the validation error alert.
The title bar text of the validation error alert.
The formula should return true or false. If it returns false, the validation will fail.
The maximum acceptable date value.
The maximum acceptable decimal value.
The maximum acceptable whole number value.
The maximum acceptable text length.
The minimum acceptable date value.
The minimum acceptable decimal value.
The minimum acceptable whole number value.
The minimum acceptable text length.
Don't use a dropdown element with a list of values.
The array of acceptable values.
The range of cells containing the values.
The name of the sheet containing the values.
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 );
wb = spreadsheet.new();
spreadsheet.setCellValue( wb, 50, 1, 1 );//A1
spreadsheet.newDataValidation()
.onCells( "B1" )
.withMinInteger( "$A$1" ) //absolute reference to the cell containing the minimum value
.withErrorTitle( "Invalid number" )
.withErrorMessage( "The number must be at least that given in cell A1" )
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.addColumn( wb, [ 1, 2, 3 ] );//A1-3
spreadsheet.newDataValidation()
.onCells( "B1:B3" )
.withMinInteger( "A1" ) //relative reference to the column in the same row containing the minimum value
.withErrorTitle( "Invalid number" )
.withErrorMessage( "The number must be at least that given in column A" )
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A1" )
.withMaxLength( 50 )
.withErrorTitle( "Entry too long" )
.withErrorMessage( "Please enter no more than 50 characters including spaces" )
.addToWorkbook( wb );
wb = spreadsheet.new();
spreadsheet.newDataValidation()
.onCells( "A:A" )//all of column A
.withFormula( "ISEVEN(A1)" ) //applies to any row in column A
.withErrorTitle( "Invalid number" )
.withErrorMessage( "Please enter an even number only" )
.addToWorkbook( wb );