summaryrefslogtreecommitdiffhomepage
path: root/examples/list_validation_example.md
blob: 1f2cfc0cb3823bb7abb77b8bde1609ee9bb612d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
## Description

You could validate data based on an explicit list or a given range of cells.

## Code

```ruby
require 'axlsx'

p = Axlsx::Package.new
wb = p.workbook

wb.add_worksheet(name: 'Basic Worksheet') do |sheet|
  sheet.add_row ['First', 'Second', 'Third']
  sheet.add_row [1, 6, 11]

  sheet.add_data_validation('A2:A2',
    type: :list,
    formula1: 'A1:C1',
    showDropDown: false,
    showErrorMessage: true,
    errorTitle: '',
    error: 'Only values from A1:C1',
    errorStyle: :stop,
    showInputMessage: true,
    promptTitle: '',
    prompt: 'Only values from A1:C1')

  sheet.add_data_validation('B2:B2',
    type: :list,
    formula1: '"Red, Orange, NavyBlue"',
    showDropDown: false,
    showErrorMessage: true,
    errorTitle: '',
    error: 'Please use the dropdown selector to choose the value',
    errorStyle: :stop,
    showInputMessage: true,
    prompt: 'Choose the value from the dropdown')
end

p.serialize 'list_validation_example.xlsx'
```

## Output

Validating by cells:

![Output](images/list_validation_example_1.png "Output")

Validating by list:

![Output](images/list_validation_example_2.png "Output")