lib/write_xlsx/worksheet.rb in write_xlsx-1.01.0 vs lib/write_xlsx/worksheet.rb in write_xlsx-1.02.0
- old
+ new
@@ -307,10 +307,11 @@
@colinfo = {}
@cell_data_table = {}
@excel_version = 2007
@palette = workbook.palette
@default_url_format = workbook.default_url_format
+ @max_url_length = workbook.max_url_length
@page_setup = PageSetup.new
@screen_gridlines = true
@show_zeros = true
@@ -354,10 +355,12 @@
@images = []
@tables = []
@sparklines = []
@shapes = []
@shape_hash = {}
+ @drawing_rels = {}
+ @drawing_rels_id = 0
@header_images = []
@footer_images = []
@outline_row_level = 0
@outline_col_level = 0
@@ -3045,20 +3048,25 @@
x_offset = params[:x_offset]
y_offset = params[:y_offset]
x_scale = params[:x_scale]
y_scale = params[:y_scale]
anchor = params[:object_position]
+ url = params[:url]
+ tip = params[:tip]
else
x_offset, y_offset, x_scale, y_scale, anchor = options
end
x_offset ||= 0
y_offset ||= 0
x_scale ||= 1
y_scale ||= 1
anchor ||= 2
- @images << [row, col, image, x_offset, y_offset, x_scale, y_scale, anchor]
+ @images << [
+ row, col, image, x_offset, y_offset,
+ x_scale, y_scale, url, tip, anchor
+ ]
end
#
# :call-seq:
# repeat_formula(row, column, formula [ , format ] )
@@ -5726,11 +5734,11 @@
# Set the chart name for the embedded object if it has been specified.
name = chart.name
# Create a Drawing object to use with worksheet unless one already exists.
- drawing = Drawing.new(drawing_type, dimensions, 0, 0, name, nil, anchor)
+ drawing = Drawing.new(drawing_type, dimensions, 0, 0, name, nil, anchor, drawing_rel_index, 0, nil)
if !drawings?
@drawings = Drawings.new
@drawings.add_drawing_object(drawing)
@drawings.embedded = 1
@@ -6075,10 +6083,25 @@
end
end
private
+ #
+ # Get the index used to address a drawing rel link.
+ #
+ def drawing_rel_index(target = nil)
+ if !target
+ # Undefined values for drawings like charts will always be unique.
+ @drawing_rels_id += 1
+ elsif ptrue?(@drawing_rels[target])
+ @drawing_rels[target]
+ else
+ @drawing_rels_id += 1
+ @drawing_rels[target] = @drawing_rels_id
+ end
+ end
+
def hyperlinks_count
@hyperlinks.keys.inject(0) { |s, n| s += @hyperlinks[n].keys.size }
end
def store_hyperlink(row, col, hyperlink)
@@ -6427,11 +6450,12 @@
def prepare_image(index, image_id, drawing_id, width, height, name, image_type, x_dpi = 96, y_dpi = 96) #:nodoc:
x_dpi ||= 96
y_dpi ||= 96
drawing_type = 2
- row, col, image, x_offset, y_offset, x_scale, y_scale, anchor = @images[index]
+ row, col, image, x_offset, y_offset,
+ x_scale, y_scale, url, tip, anchor = @images[index]
width *= x_scale
height *= y_scale
width *= 96.0 / x_dpi
@@ -6442,11 +6466,11 @@
# Convert from pixels to emus.
width = (0.5 + (width * 9_525)).to_i
height = (0.5 + (height * 9_525)).to_i
# Create a Drawing object to use with worksheet unless one already exists.
- drawing = Drawing.new(drawing_type, dimensions, width, height, name, nil, anchor)
+ drawing = Drawing.new(drawing_type, dimensions, width, height, name, nil, anchor, 0, 0, tip)
if !drawings?
drawings = Drawings.new
drawings.embedded = 1
@drawings = drawings
@@ -6455,10 +6479,39 @@
else
drawings = @drawings
end
drawings.add_drawing_object(drawing)
+ if url
+ rel_type = '/hyperlink'
+ target_mode = 'External'
+ if url =~ %r!^[fh]tt?ps?://! || url =~ /^mailto:/
+ target = escape_url(url)
+ end
+ if url =~ /^external:/
+ target = escape_url(url.sub(/^external:/, 'file:///'))
+ # Additional escape not required in worksheet hyperlinks
+ target = target.gsub(/#/, '%23')
+ end
+ if url =~ /^internal:/
+ target = url.sub(/^internal:/, '#')
+ target_mode = nil
+ end
+
+ if target.length > 255
+ raise <<"EOS"
+Ignoring URL #{target} where link or anchor > 255 characters since it exceeds Excel's limit for URLS. See LIMITATIONS section of the WriteXLSX documentation.
+EOS
+ end
+
+ if target
+ @drawing_links << [rel_type, target, target_mode]
+ end
+ drawing.url_rel_index = drawing_rel_index
+ end
+
+ drawing.rel_index = drawing_rel_index
@drawing_links << ['/image', "../media/image#{image_id}.#{image_type}"]
end
public :prepare_image
def prepare_header_image(image_id, width, height, name, image_type, position, x_dpi, y_dpi)
@@ -6573,10 +6626,10 @@
# Validate the he shape against various rules.
shape.validate(index)
shape.calc_position_emus(self)
drawing_type = 3
- drawing = Drawing.new(drawing_type, shape.dimensions, shape.width_emu, shape.height_emu, shape.name, shape, shape.anchor)
+ drawing = Drawing.new(drawing_type, shape.dimensions, shape.width_emu, shape.height_emu, shape.name, shape, shape.anchor, drawing_rel_index, 0, nil)
drawings.add_drawing_object(drawing)
end
public :prepare_shape
#