summaryrefslogtreecommitdiffhomepage
path: root/examples/list_validation_example.md
blob: cf375c08347fef44bb3a92230f087f037e6b4da0 (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
53
54
55
56
57
58
59
60
61
62
63
## 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 ['Fixed list', 'small']
  sheet.add_row ['Dynamic list', 'red']
  sheet.add_row ['No dropdown', 'north']
  sheet.add_row
  sheet.add_row ['Allowed values in B2 (dynamic list):', 'red', 'orange', 'navy blue']
  sheet.add_data_validation('B1:B1',
    type: :list,
    formula1: '"small, medium, large"',
    showErrorMessage: true,
    errorTitle: '',
    error: 'Allowed values: small, medium, large',
    errorStyle: :stop,
    showInputMessage: true,
    promptTitle: '',
    prompt: 'Choose a size:')

  sheet.add_data_validation('B2:B2',
    type: :list,
    formula1: 'B5:D5',
    showErrorMessage: true,
    errorTitle: '',
    error: 'Only values from B5:D5 allowed!',
    errorStyle: :stop,
    showInputMessage: true,
    prompt: 'Choose a color (from list in B5:D5):')

  sheet.add_data_validation('B3:B3',
    type: :list,
    formula1: '"north, east, south, west"',
    hideDropDown: true,
    showErrorMessage: true,
    errorTitle: '',
    error: 'Allowed values: north, east, south, west',
    errorStyle: :stop,
    showInputMessage: true,
    prompt: 'Enter a direction:')
end

p.serialize 'list_validation_example.xlsx'
```

## Output

Validating with dropdown:

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

Validating without dropdown:

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