Module: Writexlsx::Worksheet::DrawingMethods

Included in:
Writexlsx::Worksheet
Defined in:
lib/write_xlsx/worksheet/drawing_methods.rb

Instance Method Summary collapse

Instance Method Details

#add_sparkline(param) ⇒ Object

:call-seq:

add_sparkline(properties)

Add sparklines to the worksheet.



174
175
176
# File 'lib/write_xlsx/worksheet/drawing_methods.rb', line 174

def add_sparkline(param)
  @assets.add_sparkline(Sparkline.new(self, param, quote_sheetname(@name)))
end

#add_table(*args) ⇒ Object

:call-seq:

add_table(row1, col1, row2, col2, properties)

Add an Excel table to a worksheet.



161
162
163
164
165
166
# File 'lib/write_xlsx/worksheet/drawing_methods.rb', line 161

def add_table(*args)
  # Table count is a member of Workbook, global to all Worksheet.
  table = Package::Table.new(self, *args)
  @assets.add_table(table)
  table
end

#embed_image(row, col, filename, options = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/write_xlsx/worksheet/drawing_methods.rb', line 76

def embed_image(row, col, filename, options = nil)
  normalize_row, normalize_col, image, normalize_options = normalize_row_col_args(row, col, filename, options)

  raise WriteXLSXInsufficientArgumentError if [normalize_row, normalize_col, image].include?(nil)
  raise "Couldn't locate #{image}" unless File.exist?(image)

  # Check that row and col are valid and store max and min values
  check_dimensions(normalize_row, normalize_col)
  store_row_col_max_min_values(normalize_row, normalize_col)

  if options
    xf          = options[:cell_format]
    url         = options[:url]
    tip         = options[:tip]
    description = options[:description]
    decorative  = options[:decorative]
  else
    xf, url, tip, description, decorative = []
  end

  # Write the url without writing a string.
  if url
    xf ||= @default_url_format

    write_url(row, col, url, xf, nil, tip, true)
  end

  # Get the image properties, mainly for the type and checksum.
  image_property = ImageProperty.new(
    image, description: description, decorative: decorative
  )
  @workbook.store_image_types(image_property.type)

  # Check for duplicate images.
  image_index = @embedded_image_indexes[image_property.md5]

  unless ptrue?(image_index)
    @workbook.embedded_images << image_property

    image_index = @workbook.embedded_images.size
    @embedded_image_indexes[image_property.md5] = image_index
  end

  # Write the cell placeholder.
  store_data_to_table(EmbedImageCellData.new(image_index, xf), normalize_row, normalize_col)
  @has_embedded_images = true
end

#insert_chart(row, col, chart = nil, *options) ⇒ Object

DrawingMethods

Provides high-level Worksheet APIs for inserting drawing-related objects such as charts, images, shapes, tables, and sparklines.

Responsibilities:

  • Public insertion entry points used by Worksheet users

  • Argument normalization and option extraction

  • Shape insertion workflow and helper utilities

This module handles *what gets added to the sheet*. It does not manage relationships or XML output.

This method can be used to insert a Chart object into a worksheet. The Chart must be created by the add_chart() Workbook method and it must have the embedded option set.



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
# File 'lib/write_xlsx/worksheet/drawing_methods.rb', line 28

def insert_chart(row, col, chart = nil, *options)
  normalized_row, normalized_col, normalized_chart, normalized_options =
    normalize_row_col_args(row, col, chart, options)
  raise WriteXLSXInsufficientArgumentError if [normalized_row, normalized_col, normalized_chart].include?(nil)

  x_offset, y_offset, x_scale, y_scale, anchor, description, decorative =
    extract_chart_options(normalized_options)

  raise "Not a Chart object in insert_chart()" unless normalized_chart.is_a?(Chart) || normalized_chart.is_a?(Chartsheet)
  raise "Not a embedded style Chart object in insert_chart()" if normalized_chart.respond_to?(:embedded) && normalized_chart.embedded == 0

  if normalized_chart.already_inserted? || (normalized_chart.combined && normalized_chart.combined.already_inserted?)
    raise "Chart cannot be inserted in a worksheet more than once"
  else
    normalized_chart.already_inserted          = true
    normalized_chart.combined.already_inserted = true if normalized_chart.combined
  end

  # Use the values set with chart.set_size, if any.
  x_scale  = normalized_chart.x_scale  if normalized_chart.x_scale  != 1
  y_scale  = normalized_chart.y_scale  if normalized_chart.y_scale  != 1
  x_offset = normalized_chart.x_offset if ptrue?(normalized_chart.x_offset)
  y_offset = normalized_chart.y_offset if ptrue?(normalized_chart.y_offset)

  @assets.add_chart(
    InsertedChart.new(
      normalized_row,    normalized_col, normalized_chart,
      x_offset, y_offset, x_scale, y_scale, anchor, description, decorative
    )
  )
end

#insert_image(row, col, image = nil, *options) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/write_xlsx/worksheet/drawing_methods.rb', line 60

def insert_image(row, col, image = nil, *options)
  normalized_row, normalized_col, normalized_image, normalized_options =
    normalize_row_col_args(row, col, image, options)
  raise WriteXLSXInsufficientArgumentError if [normalized_row, normalized_col, normalized_image].include?(nil)

  x_offset, y_offset, x_scale, y_scale,
  anchor, url, tip, description, decorative = extract_image_options(normalized_options)

  @assets.add_image(
    Image.new(
      normalized_row, normalized_col, normalized_image, x_offset, y_offset,
      x_scale, y_scale, url, tip, anchor, description, decorative
    )
  )
end

#insert_shape(row_start, column_start, shape = nil, x_offset = nil, y_offset = nil, x_scale = nil, y_scale = nil, anchor = nil) ⇒ Object

:call-seq:

insert_shape(row, col, shape [ , x, y, x_scale, y_scale ])

Insert a shape into the worksheet.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/write_xlsx/worksheet/drawing_methods.rb', line 130

def insert_shape(
      row_start, column_start, shape = nil, x_offset = nil, y_offset = nil,
      x_scale = nil, y_scale = nil, anchor = nil
    )
  row, col, normalized_shape, normalized_options =
    normalize_shape_args(row_start, column_start, shape, x_offset, y_offset, x_scale, y_scale, anchor)
  raise "Insufficient arguments in insert_shape()" if [row, col, normalized_shape].include?(nil)

  set_shape_position(normalized_shape, row, col, normalized_options)
  assign_shape_id(normalized_shape)

  # Allow lookup of entry into shape array by shape ID.
  @shape_hash[normalized_shape.id] = normalized_shape.element = shapes.size

  inserted_shape = build_inserted_shape(normalized_shape)

  # For connectors change x/y coords based on location of connected shapes.
  auto_locate_shape_connectors(inserted_shape)

  # Insert a link to the shape on the list of shapes. Connection to
  # the parent shape is maintained.
  @assets.add_shape(inserted_shape)
  inserted_shape
end