Class: Writexlsx::Worksheet::Hyperlink

Inherits:
Object
  • Object
show all
Includes:
Utility::CellReference, Utility::Common, Utility::Url, Utility::XmlPrimitives
Defined in:
lib/write_xlsx/worksheet/hyperlink.rb

Overview

:nodoc:

Direct Known Subclasses

ExternalHyperlink, InternalHyperlink

Constant Summary collapse

MAXIMUM_URLS_SIZE =
2079

Constants included from Constants

Constants::COL_MAX, Constants::ROW_MAX, Constants::SHEETNAME_MAX, Constants::STR_MAX

Constants included from Utility::Common

Utility::Common::PERL_TRUE_VALUES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility::XmlPrimitives

#r_id_attributes, #write_color, #write_xml_declaration, #xml_str

Methods included from Utility::Url

#escape_url

Methods included from Utility::CellReference

#row_col_notation, #substitute_cellref, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell

Methods included from Utility::Common

#absolute_char, #check_parameter, #float_to_str, #ptrue?, #put_deprecate_message

Constructor Details

#initialize(url, str, tip, max_url_length) ⇒ Hyperlink

Returns a new instance of Hyperlink.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 31

def initialize(url, str, tip, max_url_length)
  # The displayed string defaults to the url string.
  str ||= url.dup

  # Strip the mailto header.
  normalized_str = str.sub(/^mailto:/, '')

  # Split url into the link and optional anchor/location.
  url, @url_str = url.split("#", 2)

  # Escape URL unless it looks already escaped.
  url = escape_url(url)

  # Excel limits the escaped URL and location/anchor to 255 characters.
  raise "Ignoring URL '#{url}' where link or anchor > #{max_url_length} characters since it exceeds Excel's limit for URLS. See LIMITATIONS section of the WriteXLSX documentation." if url.bytesize > max_url_length || (!@url_str.nil? && @url_str.bytesize > max_url_length)

  @url       = url
  @str       = normalized_str
  @tip       = tip
end

Instance Attribute Details

#str ⇒ Object (readonly)

Returns the value of attribute str.



17
18
19
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 17

def str
  @str
end

#tip ⇒ Object (readonly)

Returns the value of attribute tip.



17
18
19
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 17

def tip
  @tip
end

Class Method Details

.factory(url, str = nil, tip = nil, max_url_length = MAXIMUM_URLS_SIZE) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 21

def self.factory(url, str = nil, tip = nil, max_url_length = MAXIMUM_URLS_SIZE)
  if url =~ /^internal:(.+)/
    InternalHyperlink.new($~[1], str, tip, max_url_length)
  elsif url =~ /^external:(.+)/
    ExternalHyperlink.new($~[1], str, tip, max_url_length)
  else
    new(url, str, tip, max_url_length)
  end
end

Instance Method Details

#attributes(row, col, id) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 52

def attributes(row, col, id)
  ref = xl_rowcol_to_cell(row, col)

  attr = [['ref', ref]]
  attr << r_id_attributes(id)

  attr << ['location', @url_str] if @url_str
  attr << ['display',  @display] if @display
  attr << ['tooltip',  @tip]     if @tip
  attr
end

#display_on ⇒ Object



68
69
70
71
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 68

def display_on
  # @display = @url_str
  @display = @str
end


64
65
66
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 64

def external_hyper_link
  ['/hyperlink', @url, 'External']
end