Class: Writexlsx::Worksheet::DataValidation

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/write_xlsx/worksheet/data_validation.rb

Overview

:nodoc:

Constant Summary

Constants included from Utility

Utility::CHAR_WIDTHS, Utility::COL_MAX, Utility::DEFAULT_COL_PIXELS, Utility::MAX_DIGIT_WIDTH, Utility::PADDING, Utility::PERL_TRUE_VALUES, Utility::ROW_MAX, Utility::SHEETNAME_MAX, Utility::STR_MAX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #color, #convert_date_time, #convert_font_args, #dash_types, delete_files, #escape_url, #fill_properties, #float_to_str, #get_font_latin_attributes, #get_font_style_attributes, #layout_properties, #legend_properties, #line_fill_properties, #line_properties, #palette_color_from_index, #params_to_font, #pattern_properties, #pixels_to_points, #ptrue?, #put_deprecate_message, #quote_sheetname, #r_id_attributes, #row_col_notation, #shape_style_base, #store_col_max_min_values, #store_row_max_min_values, #substitute_cellref, #underline_attributes, #v_shape_attributes_base, #v_shape_style_base, #value_or_raise, #write_a_body_pr, #write_a_def_rpr, #write_a_end_para_rpr, #write_a_lst_style, #write_a_p_formula, #write_a_p_pr_formula, #write_a_solid_fill, #write_a_srgb_clr, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_def_rpr_r_pr_common, #write_div, #write_fill, #write_font, #write_stroke, #write_tx_pr, #write_xml_declaration, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xl_string_pixel_width, #xml_str

Constructor Details

#initialize(*args) ⇒ DataValidation

Returns a new instance of DataValidation.

Raises:



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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 14

def initialize(*args)
  # Check for a cell reference in A1 notation and substitute row and column.
  if (row_col_array = row_col_notation(args.first))
    case row_col_array.size
    when 2
      row1, col1 = row_col_array
      row2, col2, options = args[1..-1]
    when 4
      row1, col1, row2, col2 = row_col_array
      options = args[1]
    end
  else
    row1, col1, row2, col2, options = args
  end

  if row2.respond_to?(:keys)
    options_to_instance_variable(row2.dup)
    row2 = row1
    col2 = col1
  elsif options.respond_to?(:keys)
    options_to_instance_variable(options.dup)
  else
    raise WriteXLSXInsufficientArgumentError
  end
  raise WriteXLSXInsufficientArgumentError if [row1, col1, row2, col2].include?(nil)

  check_for_valid_input_params

  check_dimensions(row1, col1)
  check_dimensions(row2, col2)
  @cells = [[row1, col1, row2, col2]]

  @value = @source  if @source
  @value = @minimum if @minimum

  @validate = valid_validation_type[@validate.downcase]

  # No action is required for validate type 'any'
  # unless there are input messages.
  if @validate == 'none' && !@input_message && !@input_title
    @validate_none = true
    return
  end

  # The any, list and custom validations don't have a criteria
  # so we use a default of 'between'
  if %w[none list custom].include?(@validate)
    @criteria  = 'between'
    @maximum   = nil
  end

  check_criteria_required
  check_valid_citeria_types
  @criteria = valid_criteria_type[@criteria.downcase]

  check_maximum_value_when_criteria_is_between_or_notbetween
  @error_type = has_key?(:error_type) ? error_type_hash[@error_type.downcase] : 0

  convert_date_time_value_if_required
  # Check that the input title doesn't exceed the maximum length.
  raise "Length of input title '#{@input_title}' exceeds Excel's limit of 32" if @input_title && @input_title.length > 32
  # Check that the input message doesn't exceed the maximum length.
  raise "Length of input message '#{@input_message}' exceeds Excel's limit of 255" if @input_message && @input_message.length > 255

  set_some_defaults

  # A (for now) undocumented parameter to pass additional cell ranges.
  @other_cells.each { |cells| @cells << cells } if has_key?(:other_cells)
end

Instance Attribute Details

#cellsObject (readonly)

Returns the value of attribute cells.



10
11
12
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 10

def cells
  @cells
end

#criteriaObject (readonly)

Returns the value of attribute criteria.



9
10
11
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 9

def criteria
  @criteria
end

Returns the value of attribute dropdown.



11
12
13
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 11

def dropdown
  @dropdown
end

#error_messageObject (readonly)

Returns the value of attribute error_message.



12
13
14
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 12

def error_message
  @error_message
end

#error_titleObject (readonly)

Returns the value of attribute error_title.



12
13
14
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 12

def error_title
  @error_title
end

#error_typeObject (readonly)

Returns the value of attribute error_type.



10
11
12
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 10

def error_type
  @error_type
end

#ignore_blankObject (readonly)

Returns the value of attribute ignore_blank.



11
12
13
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 11

def ignore_blank
  @ignore_blank
end

#input_messageObject (readonly)

Returns the value of attribute input_message.



12
13
14
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 12

def input_message
  @input_message
end

#input_titleObject (readonly)

Returns the value of attribute input_title.



12
13
14
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 12

def input_title
  @input_title
end

#maximumObject (readonly)

Returns the value of attribute maximum.



9
10
11
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 9

def maximum
  @maximum
end

#minimumObject (readonly)

Returns the value of attribute minimum.



9
10
11
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 9

def minimum
  @minimum
end

#other_cellsObject (readonly)

Returns the value of attribute other_cells.



10
11
12
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 10

def other_cells
  @other_cells
end

#show_errorObject (readonly)

Returns the value of attribute show_error.



11
12
13
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 11

def show_error
  @show_error
end

#show_inputObject (readonly)

Returns the value of attribute show_input.



11
12
13
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 11

def show_input
  @show_input
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 9

def source
  @source
end

#validateObject (readonly)

Returns the value of attribute validate.



9
10
11
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 9

def validate
  @validate
end

#valueObject (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 9

def value
  @value
end

Instance Method Details

#keysObject



90
91
92
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 90

def keys
  instance_variables.collect { |v| v.to_s.sub(/@/, '').to_sym }
end

#options_to_instance_variable(params) ⇒ Object



84
85
86
87
88
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 84

def options_to_instance_variable(params)
  params.each do |k, v|
    instance_variable_set("@#{k}", v)
  end
end

#validate_none?Boolean



94
95
96
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 94

def validate_none?
  @validate_none
end

#write_data_validation(writer) ⇒ Object

Write the <dataValidation> element.



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/write_xlsx/worksheet/data_validation.rb', line 101

def write_data_validation(writer) # :nodoc:
  @writer = writer
  if @validate == 'none'
    @writer.empty_tag('dataValidation', attributes)
  else
    @writer.tag_elements('dataValidation', attributes) do
      # Write the formula1 element.
      write_formula_1(@value)
      # Write the formula2 element.
      write_formula_2(@maximum) if @maximum
    end
  end
end