Class: Writexlsx::Package::Button

Inherits:
Object
  • Object
show all
Includes:
Utility
Defined in:
lib/write_xlsx/package/button.rb

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 Method Summary collapse

Methods included from Utility

#absolute_char, #check_dimensions, #check_dimensions_and_update_max_min_values, #check_parameter, #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(worksheet, row, col, params, default_row_pixels, button_number) ⇒ Button

Returns a new instance of Button.



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
64
# File 'lib/write_xlsx/package/button.rb', line 11

def initialize(worksheet, row, col, params, default_row_pixels, button_number)
  @worksheet = worksheet
  @default_row_pixels = default_row_pixels

  # Set the button caption.
  caption = params[:caption] || "Button #{button_number}"

  @font = { _caption: caption }

  # Set the macro name.
  @macro = if params[:macro]
             "[0]!#{params[:macro]}"
           else
             "[0]!Button#{button_number}_Click"
           end

  # Set the alt text for the button.
  @description = params[:description]

  # Ensure that a width and height have been set.
  default_height = @default_row_pixels
  width  = params[:width]  || DEFAULT_COL_PIXELS
  height = params[:height] || default_row_pixels

  # Scale the size of the button box if required.
  width  *= params[:x_scale] if params[:x_scale]
  height *= params[:y_scale] if params[:y_scale]

  # Round the dimensions to the nearest pixel.
  width  = (0.5 + width).to_i
  height = (0.5 + height).to_i

  # Set the x/y offsets.
  x_offset = params[:x_offset] || 0
  y_offset = params[:y_offset] || 0

  start_row = row
  start_col = col

  # Calculate the positions of button object.
  vertices = @worksheet.position_object_pixels(
    start_col,
    start_row,
    x_offset,
    y_offset,
    width,
    height
  )

  # Add the width and height for VML.
  vertices << [width, height]

  @vertices = vertices
end

Instance Method Details

#colorObject



82
83
84
# File 'lib/write_xlsx/package/button.rb', line 82

def color
  'buttonFace [67]'
end

#fill_attributesObject

attributes for <v:fill> element.



108
109
110
111
112
113
# File 'lib/write_xlsx/package/button.rb', line 108

def fill_attributes
  [
    ['color2',             'buttonFace [67]'],
    ['o:detectmouseclick', 't']
  ]
end

#style_additionObject



86
87
88
# File 'lib/write_xlsx/package/button.rb', line 86

def style_addition
  ['mso-wrap-style:tight']
end

#typeObject



78
79
80
# File 'lib/write_xlsx/package/button.rb', line 78

def type
  '#_x0000_t201'
end

#v_shape_attributes(id, z_index) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/write_xlsx/package/button.rb', line 66

def v_shape_attributes(id, z_index)
  attributes = v_shape_attributes_base(id)
  attributes << ['alt', @description] if @description

  attributes << ['style', (v_shape_style_base(z_index, @vertices) + style_addition).join]
  attributes << ['o:button',    't']
  attributes << ['fillcolor',   color]
  attributes << ['strokecolor', 'windowText [64]']
  attributes << ['o:insetmode', 'auto']
  attributes
end

#write_client_dataObject

Write the <x:ClientData> element.



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/write_xlsx/package/button.rb', line 144

def write_client_data
  attributes = [%w[ObjectType Button]]

  @writer.tag_elements('x:ClientData', attributes) do
    # Write the x:Anchor element.
    write_anchor
    # Write the x:PrintObject element.
    write_print_object
    # Write the x:AutoFill element.
    write_auto_fill
    # Write the x:FmlaMacro element.
    write_fmla_macro
    # Write the x:TextHAlign element.
    write_text_halign
    # Write the x:TextVAlign element.
    write_text_valign
  end
end

#write_fmla_macroObject

Write the <x:FmlaMacro> element.



173
174
175
# File 'lib/write_xlsx/package/button.rb', line 173

def write_fmla_macro
  @writer.data_element('x:FmlaMacro', @macro)
end

#write_print_objectObject

Write the <x:PrintObject> element.



166
167
168
# File 'lib/write_xlsx/package/button.rb', line 166

def write_print_object
  @writer.data_element('x:PrintObject', 'False')
end

#write_rotation_lockObject

Write the <o:lock> element.



118
119
120
121
122
123
124
# File 'lib/write_xlsx/package/button.rb', line 118

def write_rotation_lock
  attributes = [
    ['v:ext',    'edit'],
    %w[rotation t]
  ]
  @writer.empty_tag('o:lock', attributes)
end

#write_shape(writer, id, z_index) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/write_xlsx/package/button.rb', line 90

def write_shape(writer, id, z_index)
  @writer = writer

  attributes = v_shape_attributes(id, z_index)

  @writer.tag_elements('v:shape', attributes) do
    # Write the v:fill element.
    write_fill
    # Write the o:lock element.
    write_rotation_lock
    # Write the v:textbox element.
    write_textbox
    # Write the x:ClientData element.
    write_client_data
  end
end

#write_text_halignObject

Write the <x:TextHAlign> element.



180
181
182
# File 'lib/write_xlsx/package/button.rb', line 180

def write_text_halign
  @writer.data_element('x:TextHAlign', 'Center')
end

#write_text_valignObject

Write the <x:TextVAlign> element.



187
188
189
# File 'lib/write_xlsx/package/button.rb', line 187

def write_text_valign
  @writer.data_element('x:TextVAlign', 'Center')
end

#write_textboxObject

Write the <v:textbox> element.



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/write_xlsx/package/button.rb', line 129

def write_textbox
  attributes = [
    ['style', 'mso-direction-alt:auto'],
    ['o:singleclick', 'f']
  ]

  @writer.tag_elements('v:textbox', attributes) do
    # Write the div element.
    write_div('center', @font)
  end
end