Class: Axlsx::WorksheetHyperlink

Inherits:
Object
  • Object
show all
Defined in:
lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb

Overview

A worksheet hyperlink object. Note that this is not the same as a drawing hyperlink object.

Constant Summary collapse

STRING_ATTRIBUTES =

String attributes for this object

%w(display location tooltip)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worksheet, options = {}) {|_self| ... } ⇒ WorksheetHyperlink

Note:

the preferred way to add hyperlinks to your worksheet is the Worksheet#add_hyperlink method

Creates a new hyperlink object.

Parameters:

  • worksheet (Worksheet)

    the Worksheet that owns this hyperlink

  • options (Hash) (defaults to: {})

    options to use when creating this hyperlink

  • [String] (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options

  • [String|Cell] (Hash)

    a customizable set of options

Yields:

  • (_self)

Yield Parameters:



15
16
17
18
19
20
21
22
23
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 15

def initialize(worksheet, options={})
  DataTypeValidator.validate "Hyperlink.worksheet", [Worksheet], worksheet
  @worksheet = worksheet
  @target = :external
  options.each do |o|
    self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
  end
  yield self if block_given?
end

Instance Attribute Details

#refString

Cell location of hyperlink on worksheet.

Returns:

  • (String)


30
31
32
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 30

def ref
  @ref
end

Instance Method Details

#idString

The id of the relationship for this object

Returns:

  • (String)


73
74
75
76
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 73

def id
  return unless @target == :external
  "rId#{(@worksheet.relationships_index_of(self)+1)}"
end

#relationshipRelationship

The relationship required by this hyperlink when the taget is :external

Returns:



66
67
68
69
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 66

def relationship
  return unless @target == :external
  Relationship.new HYPERLINK_R, location, :target_mode => :External
end

#serialization_valuesHash

The values to be used in serialization based on the target. location should only be specified for non-external targets. r:id should only be specified for external targets.

Returns:

  • (Hash)


91
92
93
94
95
96
97
98
99
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 91

def serialization_values
  h = instance_values.reject { |key, value| !%w(display ref tooltip).include?(key) }
  if @target == :external
    h['r:id'] = id
  else
    h['location'] = location
  end
  h
end

#target=(target) ⇒ Object

Sets the target for this hyperlink. Anything other than :external instructs the library to treat the location as an in-workbook reference.

Parameters:

  • target (Symbol)


34
35
36
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 34

def target=(target)
  @target = target
end

#to_xml_string(str = '') ⇒ String

Seralize the object

Parameters:

  • str (String) (defaults to: '')

Returns:

  • (String)


81
82
83
84
85
# File 'lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb', line 81

def to_xml_string(str='')
  str << '<hyperlink '
  serialization_values.map { |key, value| str << key.to_s << '="' << value.to_s << '" ' }
  str << '/>'
end