Class: Writexlsx::Worksheet::Hyperlink

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

Overview

:nodoc:

Constant Summary

Constants included from Utility

Utility::COL_MAX, 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, #convert_date_time, delete_files, #float_to_str, #pixels_to_points, #ptrue?, #put_deprecate_message, #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, #write_anchor, #write_auto_fill, #write_color, #write_comment_path, #write_div, #write_fill, #write_font, #write_stroke, #xl_cell_to_rowcol, #xl_col_to_name, #xl_range, #xl_range_formula, #xl_rowcol_to_cell, #xml_str

Constructor Details

#initialize(url, str = nil) ⇒ Hyperlink

Returns a new instance of Hyperlink.



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 11

def initialize(url, str = nil)
  link_type = 1

  # Remove the URI scheme from internal links.
  if url =~ /^internal:/
    url = url.sub(/^internal:/, '')
    link_type = 2
    # Remove the URI scheme from external links.
  elsif url =~ /^external:/
    url = url.sub(/^external:/, '')
    link_type = 3
  end

  # The displayed string defaults to the url string.
  str ||= url.dup

  # For external links change the directory separator from Unix to Dos.
  if link_type == 3
    url = url.gsub(%r|/|, '\\')
    str.gsub!(%r|/|, '\\')
  end

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

  # Copy string for use in hyperlink elements.
  url_str = str.dup

  # External links to URLs and to other Excel workbooks have slightly
  # different characteristics that we have to account for.
  if link_type == 1
    # Escape URL unless it looks already escaped.
    unless url =~ /%[0-9a-fA-F]{2}/
      # Escape the URL escape symbol.
      url = url.gsub(/%/, "%25")

      # Escape whitespae in URL.
      url = url.gsub(/[\s\x00]/, '%20')

      # Escape other special characters in URL.
      re = /(["<>\[\]`^{}])/
      while re =~ url
        match = $~[1]
        url = url.sub(re, sprintf("%%%x", match.ord))
      end
    end

    # Ordinary URL style external links don't have a "location" string.
    url_str = nil
  elsif link_type == 3
    # External Workbook links need to be modified into the right format.
    # The URL will look something like 'c:\temp\file.xlsx#Sheet!A1'.
    # We need the part to the left of the # as the URL and the part to
    # the right as the "location" string (if it exists).
    url, url_str = url.split(/#/)

    # Add the file:/// URI to the url if non-local.
    if url =~ %r![:]! ||        # Windows style "C:/" link.
        url =~ %r!^\\\\!        # Network share.
      url = "file:///#{url}"
    end

    # Convert a ./dir/file.xlsx link to dir/file.xlsx.
    url = url.sub(%r!^.\\!, '')

    # Treat as a default external link now that the data has been modified.
    link_type = 1
  end

  # Excel limits escaped URL to 255 characters.
  if url.bytesize > 255
    raise "URL '#{url}' > 255 characters, it exceeds Excel's limit for URLS."
  end
  @url       = url
  @link_type = link_type
  @str       = str
  @url_str   = url_str
end

Instance Attribute Details

#displayObject

Returns the value of attribute display.



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

def display
  @display
end

Returns the value of attribute link_type.



8
9
10
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 8

def link_type
  @link_type
end

#strObject (readonly)

Returns the value of attribute str.



8
9
10
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 8

def str
  @str
end

#tipObject

Returns the value of attribute tip.



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

def tip
  @tip
end

#urlObject (readonly)

Returns the value of attribute url.



8
9
10
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 8

def url
  @url
end

#url_strObject (readonly)

Returns the value of attribute url_str.



8
9
10
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 8

def url_str
  @url_str
end

Instance Method Details

#write_external_attributes(row, col, id) ⇒ Object



90
91
92
93
94
95
96
97
98
99
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 90

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

  attributes = ['ref', ref, 'r:id', "rId#{id}"]

  attributes << 'location' << url_str if url_str
  attributes << 'display'  << display if display
  attributes << 'tooltip'  << tip     if tip
  attributes
end

#write_internal_attributes(row, col) ⇒ Object



101
102
103
104
105
106
107
108
# File 'lib/write_xlsx/worksheet/hyperlink.rb', line 101

def write_internal_attributes(row, col)
  ref = xl_rowcol_to_cell(row, col)

  attributes = ['ref', ref, 'location', url]

  attributes << 'tooltip' << tip if tip
  attributes << 'display' << str
end