axlsx 1.2.0 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +10 -2
- data/examples/example.rb +40 -13
- data/examples/example.xlsx +0 -0
- data/examples/example_streamed.xlsx +0 -0
- data/examples/hyperlinks.rb +23 -0
- data/examples/hyperlinks.xlsx +0 -0
- data/examples/no-use_autowidth.xlsx +0 -0
- data/examples/shared_strings_example.xlsx +0 -0
- data/examples/skydrive/axlsx.xlsx +0 -0
- data/examples/skydrive/real_example.rb +2 -2
- data/lib/axlsx/doc_props/app.rb +3 -3
- data/lib/axlsx/drawing/view_3D.rb +1 -1
- data/lib/axlsx/stylesheet/styles.rb +6 -1
- data/lib/axlsx/util/constants.rb +17 -1
- data/lib/axlsx/util/simple_typed_list.rb +3 -1
- data/lib/axlsx/version.rb +1 -1
- data/lib/axlsx/workbook/workbook.rb +2 -0
- data/lib/axlsx/workbook/worksheet/auto_filter.rb +1 -0
- data/lib/axlsx/workbook/worksheet/cell.rb +1 -1
- data/lib/axlsx/workbook/worksheet/worksheet.rb +27 -4
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb +101 -0
- data/lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb +35 -0
- data/test/tc_package.rb +1 -0
- data/test/workbook/worksheet/tc_cell.rb +16 -0
- data/test/workbook/worksheet/tc_worksheet_hyperlink.rb +64 -0
- metadata +10 -3
data/README.md
CHANGED
|
@@ -17,7 +17,7 @@ Axlsx: Office Open XML Spreadsheet Generation
|
|
|
17
17
|
|
|
18
18
|
**License**: MIT License
|
|
19
19
|
|
|
20
|
-
**Latest Version**: 1.2.
|
|
20
|
+
**Latest Version**: 1.2.2
|
|
21
21
|
|
|
22
22
|
**Ruby Version**: 1.8.7, 1.9.2, 1.9.3
|
|
23
23
|
|
|
@@ -25,7 +25,7 @@ Axlsx: Office Open XML Spreadsheet Generation
|
|
|
25
25
|
|
|
26
26
|
**Rubinius Version**: rubinius 2.0.0dev * lower versions may run, this gem always tests against head.
|
|
27
27
|
|
|
28
|
-
**Release Date**: August
|
|
28
|
+
**Release Date**: August 27th 2012
|
|
29
29
|
|
|
30
30
|
If you are working in rails, or with active record see:
|
|
31
31
|
* http://github.com/randym/acts_as_xlsx
|
|
@@ -145,6 +145,13 @@ This gem has 100% test coverage using test/unit. To execute tests for this gem,
|
|
|
145
145
|
|
|
146
146
|
#Change log
|
|
147
147
|
---------
|
|
148
|
+
- **August.27.12**: 1.2.2
|
|
149
|
+
- minor patch for auto-filters
|
|
150
|
+
- minor documentation improvements.
|
|
151
|
+
- **August.12.12**: 1.2.1
|
|
152
|
+
- Add support for hyperlinks in worksheets
|
|
153
|
+
- Fix example that was using old style cell merging with concact.
|
|
154
|
+
- Fix bug that occurs when calculating the font_size for cells that use a user specified style which does not define sz
|
|
148
155
|
- **August.5.12**: 1.2.0
|
|
149
156
|
- rebuilt worksheet serialization to clean up the code base a bit.
|
|
150
157
|
- added data labels for charts
|
|
@@ -229,6 +236,7 @@ done without the help of the people below.
|
|
|
229
236
|
|
|
230
237
|
[straydogstudio](https://github.com/straydocstudio) - For making an AWESOME axlsx templating gem for rails.
|
|
231
238
|
|
|
239
|
+
[MitchellAJ](https://github.com/MitchellAJ) - For catching a bug in font_size calculations, finding some old code in an example and above all for reporting all of that brilliantly
|
|
232
240
|
#Copyright and License
|
|
233
241
|
----------
|
|
234
242
|
|
data/examples/example.rb
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env ruby -w -s
|
|
2
2
|
# -*- coding: utf-8 -*-
|
|
3
|
-
|
|
3
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
|
4
4
|
|
|
5
5
|
#```ruby
|
|
6
6
|
require 'axlsx'
|
|
@@ -55,22 +55,22 @@ wb.add_worksheet(:name => "Cell Level Style Overrides") do |sheet|
|
|
|
55
55
|
sheet["A1:D1"].each { |c| c.color = "FF0000" }
|
|
56
56
|
sheet['A1:D2'].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
|
|
57
57
|
end
|
|
58
|
-
|
|
58
|
+
##```
|
|
59
59
|
|
|
60
60
|
##Using Custom Border Styles
|
|
61
61
|
|
|
62
62
|
#```ruby
|
|
63
63
|
#Axlsx defines a thin border style, but you can easily create and use your own.
|
|
64
64
|
wb.styles do |s|
|
|
65
|
-
red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000" }
|
|
66
|
-
blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF"
|
|
65
|
+
red_border = s.add_style :border => { :style => :thick, :color =>"FFFF0000", :edges => [:left, :right] }
|
|
66
|
+
blue_border = s.add_style :border => { :style => :thick, :color =>"FF0000FF"}
|
|
67
67
|
|
|
68
68
|
wb.add_worksheet(:name => "Custom Borders") do |sheet|
|
|
69
69
|
sheet.add_row ["wrap", "me", "Up in Red"], :style => red_border
|
|
70
70
|
sheet.add_row [1, 2, 3], :style => blue_border
|
|
71
71
|
end
|
|
72
72
|
end
|
|
73
|
-
|
|
73
|
+
##```
|
|
74
74
|
|
|
75
75
|
|
|
76
76
|
##Styling Rows and Columns
|
|
@@ -101,7 +101,7 @@ wb.styles do |s|
|
|
|
101
101
|
sheet.rows[1].outlineLevel = 2
|
|
102
102
|
end
|
|
103
103
|
end
|
|
104
|
-
|
|
104
|
+
##```
|
|
105
105
|
|
|
106
106
|
|
|
107
107
|
##Specifying Column Widths
|
|
@@ -112,7 +112,7 @@ wb.add_worksheet(:name => "custom column widths") do |sheet|
|
|
|
112
112
|
sheet.add_row ['abcdefg', 'This is a very long text and should flow into the right cell', nil, 'xxx' ]
|
|
113
113
|
sheet.column_widths nil, 3, 5, nil
|
|
114
114
|
end
|
|
115
|
-
|
|
115
|
+
##```
|
|
116
116
|
|
|
117
117
|
##Merging Cells.
|
|
118
118
|
|
|
@@ -127,7 +127,7 @@ wb.add_worksheet(:name => 'Merging Cells') do |sheet|
|
|
|
127
127
|
sheet["A1:D1"].each { |c| c.color = "FF0000"}
|
|
128
128
|
sheet["A1:D4"].each { |c| c.style = Axlsx::STYLE_THIN_BORDER }
|
|
129
129
|
end
|
|
130
|
-
|
|
130
|
+
##```
|
|
131
131
|
|
|
132
132
|
##Add an Image with a hyperlink
|
|
133
133
|
|
|
@@ -169,7 +169,7 @@ wb.add_worksheet(:name => "日本語でのシート名") do |sheet|
|
|
|
169
169
|
sheet.add_row ["华语/華語"]
|
|
170
170
|
sheet.add_row ["한국어/조선말"]
|
|
171
171
|
end
|
|
172
|
-
|
|
172
|
+
##```
|
|
173
173
|
|
|
174
174
|
##Using formula
|
|
175
175
|
|
|
@@ -178,7 +178,7 @@ wb.add_worksheet(:name => "Using Formulas") do |sheet|
|
|
|
178
178
|
sheet.add_row ['col 1', 'col 2', 'col 3', 'col 4']
|
|
179
179
|
sheet.add_row [1, 2, 3, "=SUM(A2:C2)"]
|
|
180
180
|
end
|
|
181
|
-
|
|
181
|
+
##```
|
|
182
182
|
|
|
183
183
|
##Auto Filter
|
|
184
184
|
|
|
@@ -195,6 +195,7 @@ end
|
|
|
195
195
|
|
|
196
196
|
##Automatic cell types
|
|
197
197
|
|
|
198
|
+
|
|
198
199
|
#```ruby
|
|
199
200
|
wb.add_worksheet(:name => "Automatic cell types") do |sheet|
|
|
200
201
|
date_format = wb.styles.add_style :format_code => 'YYYY-MM-DD'
|
|
@@ -202,9 +203,33 @@ wb.add_worksheet(:name => "Automatic cell types") do |sheet|
|
|
|
202
203
|
sheet.add_row ["Date", "Time", "String", "Boolean", "Float", "Integer"]
|
|
203
204
|
sheet.add_row [Date.today, Time.now, "value", true, 0.1, 1], :style => [date_format, time_format]
|
|
204
205
|
end
|
|
205
|
-
#```
|
|
206
206
|
|
|
207
207
|
|
|
208
|
+
# Hyperlinks in worksheet
|
|
209
|
+
wb.add_worksheet(:name => 'hyperlinks') do |sheet|
|
|
210
|
+
# external references
|
|
211
|
+
sheet.add_row ['axlsx']
|
|
212
|
+
sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
|
|
213
|
+
# internal references
|
|
214
|
+
sheet.add_hyperlink :location => "'Next Sheet'!A1", :ref => 'A2', :target => :sheet
|
|
215
|
+
sheet.add_row ['next sheet']
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
wb.add_worksheet(:name => 'Next Sheet') do |sheet|
|
|
219
|
+
sheet.add_row ['hello!']
|
|
220
|
+
end
|
|
221
|
+
###```
|
|
222
|
+
|
|
223
|
+
##Number formatting and currency
|
|
224
|
+
wb.add_worksheet(:name => "Formats and Currency") do |sheet|
|
|
225
|
+
currency = wb.styles.add_style :num_fmt => 5
|
|
226
|
+
red_negative = wb.styles.add_style :num_fmt => 8
|
|
227
|
+
comma = wb.styles.add_style :num_fmt => 3
|
|
228
|
+
super_funk = wb.styles.add_style :format_code => '[Green]#'
|
|
229
|
+
sheet.add_row %w(Currency RedNegative Comma Custom)
|
|
230
|
+
sheet.add_row [1500, -122.34, 123456789, 594829], :style=> [currency, red_negative, comma, super_funk]
|
|
231
|
+
end
|
|
232
|
+
|
|
208
233
|
##Generating A Bar Chart
|
|
209
234
|
|
|
210
235
|
#```ruby
|
|
@@ -215,7 +240,7 @@ wb.add_worksheet(:name => "Bar Chart") do |sheet|
|
|
|
215
240
|
chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"], :title => sheet["A1"]
|
|
216
241
|
end
|
|
217
242
|
end
|
|
218
|
-
|
|
243
|
+
##```
|
|
219
244
|
|
|
220
245
|
##Hide Gridlines in chart
|
|
221
246
|
|
|
@@ -447,7 +472,9 @@ p.use_shared_strings = true
|
|
|
447
472
|
p.serialize("shared_strings_example.xlsx")
|
|
448
473
|
#```
|
|
449
474
|
|
|
450
|
-
|
|
475
|
+
#p.validate do |er|
|
|
476
|
+
#puts er.inspect
|
|
477
|
+
#end
|
|
451
478
|
##Disabling Autowidth
|
|
452
479
|
|
|
453
480
|
#```ruby
|
data/examples/example.xlsx
CHANGED
|
Binary file
|
|
Binary file
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
#!/usr/bin/env ruby -w -s
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../lib"
|
|
4
|
+
|
|
5
|
+
#```ruby
|
|
6
|
+
require 'axlsx'
|
|
7
|
+
|
|
8
|
+
p = Axlsx::Package.new
|
|
9
|
+
wb = p.workbook
|
|
10
|
+
wb.add_worksheet(:name => 'hyperlinks') do |sheet|
|
|
11
|
+
# external references
|
|
12
|
+
sheet.add_row ['axlsx']
|
|
13
|
+
sheet.add_hyperlink :location => 'https://github.com/randym/axlsx', :ref => sheet.rows.first.cells.first
|
|
14
|
+
# internal references
|
|
15
|
+
sheet.add_row ['next sheet']
|
|
16
|
+
sheet.add_hyperlink :location => "'Next Sheet'!A1", :target => :sheet, :ref => 'A2'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
wb.add_worksheet(:name => 'Next Sheet') do |sheet|
|
|
20
|
+
sheet.add_row ['hello!']
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
p.serialize 'hyperlinks.xlsx'
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}
|
|
1
|
+
$LOAD_PATH.unshift "#{File.dirname(__FILE__)}/../../lib"
|
|
2
2
|
|
|
3
3
|
require 'axlsx'
|
|
4
4
|
|
|
@@ -54,7 +54,7 @@ Axlsx::Package.new do |p|
|
|
|
54
54
|
chart.start_at 7, 16
|
|
55
55
|
chart.end_at 12, 31
|
|
56
56
|
end
|
|
57
|
-
|
|
57
|
+
%w(B4:C4 E4:F4 B11:C11 E11:F11 B2:F2).each { |range| sheet.merge_cells(range) }
|
|
58
58
|
sheet.column_widths 2, nil, nil, 2, nil, nil, 2
|
|
59
59
|
end
|
|
60
60
|
end
|
data/lib/axlsx/doc_props/app.rb
CHANGED
|
@@ -134,15 +134,15 @@ module Axlsx
|
|
|
134
134
|
|
|
135
135
|
# Sets the template property of your app.xml file
|
|
136
136
|
def template=(v) Axlsx::validate_string v; @template = v; end
|
|
137
|
-
alias :Template :template
|
|
137
|
+
alias :Template= :template=
|
|
138
138
|
|
|
139
139
|
# Sets the manager property of your app.xml file
|
|
140
140
|
def manager=(v) Axlsx::validate_string v; @manager = v; end
|
|
141
|
-
alias :Manager :manager
|
|
141
|
+
alias :Manager= :manager=
|
|
142
142
|
|
|
143
143
|
# Sets the company property of your app.xml file
|
|
144
144
|
def company=(v) Axlsx::validate_string v; @company = v; end
|
|
145
|
-
alias :Company :company
|
|
145
|
+
alias :Company= :company=
|
|
146
146
|
# Sets the pages property of your app.xml file
|
|
147
147
|
def pages=(v) Axlsx::validate_int v; @pages = v; end
|
|
148
148
|
|
|
@@ -135,6 +135,7 @@ module Axlsx
|
|
|
135
135
|
# @option options [Integer] num_fmt The number format to apply
|
|
136
136
|
# @option options [String] format_code The formatting to apply.
|
|
137
137
|
# @option options [Integer|Hash] border The border style to use.
|
|
138
|
+
# borders support style, color and edges options @see parse_border_options
|
|
138
139
|
# @option options [String] bg_color The background color to apply to the cell
|
|
139
140
|
# @option options [Boolean] hidden Indicates if the cell should be hidden
|
|
140
141
|
# @option options [Boolean] locked Indicates if the cell should be locked
|
|
@@ -298,7 +299,11 @@ module Axlsx
|
|
|
298
299
|
|
|
299
300
|
# parses Style#add_style options for borders.
|
|
300
301
|
# @note noop if :border is not specified in options
|
|
301
|
-
# @option options [Hash|Integer] A border style definition hash or the index of an existing border.
|
|
302
|
+
# @option options [Hash|Integer] A border style definition hash or the index of an existing border.
|
|
303
|
+
# Border style definition hashes must include :style and :color key-value entries and
|
|
304
|
+
# may include an :edges entry that references an array of symbols identifying which border edges
|
|
305
|
+
# you wish to apply the style or any other valid Border initializer options.
|
|
306
|
+
# If the :edges entity is not provided the style is applied to all edges of cells that reference this style.
|
|
302
307
|
# @example
|
|
303
308
|
# #apply a thick red border to the top and bottom
|
|
304
309
|
# { :border => { :style => :thick, :color => "FFFF0000", :edges => [:top, :bottom] }
|
data/lib/axlsx/util/constants.rb
CHANGED
|
@@ -31,6 +31,9 @@ module Axlsx
|
|
|
31
31
|
# xml schema namespace
|
|
32
32
|
CORE_NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
|
|
33
33
|
|
|
34
|
+
# Digital signature namespace
|
|
35
|
+
DIGITAL_SIGNATURE_NS = "http://schemas.openxmlformats.org/package/2006/digital-signature"
|
|
36
|
+
|
|
34
37
|
# spreadsheet drawing namespace
|
|
35
38
|
XML_NS_XDR = "http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing"
|
|
36
39
|
|
|
@@ -61,6 +64,9 @@ module Axlsx
|
|
|
61
64
|
# core rels namespace
|
|
62
65
|
CORE_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/metadata/core-properties"
|
|
63
66
|
|
|
67
|
+
# digital signature rels namespace
|
|
68
|
+
DIGITAL_SIGNATURE_R = "http://schemas.openxmlformats.org/package/2006/relationships/digital- signature/signature"
|
|
69
|
+
|
|
64
70
|
# styles rels namespace
|
|
65
71
|
STYLES_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles"
|
|
66
72
|
|
|
@@ -76,7 +82,7 @@ module Axlsx
|
|
|
76
82
|
# image rels namespace
|
|
77
83
|
IMAGE_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image"
|
|
78
84
|
|
|
79
|
-
#
|
|
85
|
+
# hyperlink rels namespace
|
|
80
86
|
HYPERLINK_R = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink"
|
|
81
87
|
|
|
82
88
|
# comment rels namespace
|
|
@@ -118,6 +124,15 @@ module Axlsx
|
|
|
118
124
|
# core content type
|
|
119
125
|
CORE_CT = "application/vnd.openxmlformats-package.core-properties+xml"
|
|
120
126
|
|
|
127
|
+
# digital signature xml content type
|
|
128
|
+
DIGITAL_SIGNATURE_XML_CT = "application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml"
|
|
129
|
+
|
|
130
|
+
# digital signature origin content type
|
|
131
|
+
DIGITAL_SIGNATURE_ORIGIN_CT = "application/vnd.openxmlformats-package.digital-signature-origin"
|
|
132
|
+
|
|
133
|
+
# digital signature certificate content type
|
|
134
|
+
DIGITAL_SIGNATURE_CERTIFICATE_CT = "application/vnd.openxmlformats-package.digital-signature-certificate"
|
|
135
|
+
|
|
121
136
|
# chart content type
|
|
122
137
|
CHART_CT = "application/vnd.openxmlformats-officedocument.drawingml.chart+xml"
|
|
123
138
|
|
|
@@ -136,6 +151,7 @@ module Axlsx
|
|
|
136
151
|
#drawing content type
|
|
137
152
|
DRAWING_CT = "application/vnd.openxmlformats-officedocument.drawing+xml"
|
|
138
153
|
|
|
154
|
+
|
|
139
155
|
# xml content type extensions
|
|
140
156
|
XML_EX = "xml"
|
|
141
157
|
|
data/lib/axlsx/version.rb
CHANGED
|
@@ -27,6 +27,8 @@ require 'axlsx/workbook/worksheet/dimension.rb'
|
|
|
27
27
|
require 'axlsx/workbook/worksheet/sheet_data.rb'
|
|
28
28
|
require 'axlsx/workbook/worksheet/worksheet_drawing.rb'
|
|
29
29
|
require 'axlsx/workbook/worksheet/worksheet_comments.rb'
|
|
30
|
+
require 'axlsx/workbook/worksheet/worksheet_hyperlink'
|
|
31
|
+
require 'axlsx/workbook/worksheet/worksheet_hyperlinks'
|
|
30
32
|
require 'axlsx/workbook/worksheet/worksheet.rb'
|
|
31
33
|
require 'axlsx/workbook/shared_strings_table.rb'
|
|
32
34
|
require 'axlsx/workbook/defined_name.rb'
|
|
@@ -343,7 +343,7 @@ module Axlsx
|
|
|
343
343
|
private
|
|
344
344
|
|
|
345
345
|
def font_size
|
|
346
|
-
sz || @styles.fonts[@styles.cellXfs[style].fontId].sz
|
|
346
|
+
sz || @styles.fonts[@styles.cellXfs[style].fontId].sz || @styles.fonts[0].sz
|
|
347
347
|
end
|
|
348
348
|
|
|
349
349
|
# Utility method for setting inline style attributes
|
|
@@ -70,7 +70,13 @@ module Axlsx
|
|
|
70
70
|
def tables
|
|
71
71
|
@tables ||= Tables.new self
|
|
72
72
|
end
|
|
73
|
-
|
|
73
|
+
|
|
74
|
+
# A typed collection of hyperlinks associated with this worksheet
|
|
75
|
+
# @return [WorksheetHyperlinks]
|
|
76
|
+
def hyperlinks
|
|
77
|
+
@hyperlinks ||= WorksheetHyperlinks.new self
|
|
78
|
+
end
|
|
79
|
+
|
|
74
80
|
# The a shortcut to the worksheet_comments list of comments
|
|
75
81
|
# @return [Array|SimpleTypedList]
|
|
76
82
|
def comments
|
|
@@ -391,6 +397,14 @@ module Axlsx
|
|
|
391
397
|
data_validations << dv
|
|
392
398
|
end
|
|
393
399
|
|
|
400
|
+
# Adds a new hyperlink to the worksheet
|
|
401
|
+
# @param [Hash] options for the hyperlink
|
|
402
|
+
# @see WorksheetHyperlink for a list of options
|
|
403
|
+
# @return [WorksheetHyperlink]
|
|
404
|
+
def add_hyperlink(options={})
|
|
405
|
+
hyperlinks.add(options)
|
|
406
|
+
end
|
|
407
|
+
|
|
394
408
|
# Adds a chart to this worksheets drawing. This is the recommended way to create charts for your worksheet. This method wraps the complexity of dealing with ooxml drawing, anchors, markers graphic frames chart objects and all the other dirty details.
|
|
395
409
|
# @param [Class] chart_type
|
|
396
410
|
# @option options [Array] start_at
|
|
@@ -490,12 +504,21 @@ module Axlsx
|
|
|
490
504
|
# @return [Relationships]
|
|
491
505
|
def relationships
|
|
492
506
|
r = Relationships.new
|
|
493
|
-
r + [tables.relationships,
|
|
494
|
-
worksheet_comments.relationships,
|
|
507
|
+
r + [tables.relationships,
|
|
508
|
+
worksheet_comments.relationships,
|
|
509
|
+
hyperlinks.relationships,
|
|
495
510
|
worksheet_drawing.relationship].flatten.compact || []
|
|
496
511
|
r
|
|
497
512
|
end
|
|
498
513
|
|
|
514
|
+
# identifies the index of an object withing the collections used in generating relationships for the worksheet
|
|
515
|
+
# @param [Any] object the object to search for
|
|
516
|
+
# @return [Integer] The index of the object
|
|
517
|
+
def relationships_index_of(object)
|
|
518
|
+
objects = [tables.to_a, worksheet_comments.comments.to_a, hyperlinks.to_a, worksheet_drawing.drawing].flatten.compact || []
|
|
519
|
+
objects.index(object)
|
|
520
|
+
end
|
|
521
|
+
|
|
499
522
|
# Returns the cell or cells defined using excel style A1:B3 references.
|
|
500
523
|
# @param [String|Integer] cell_def the string defining the cell or range of cells, or the rownumber
|
|
501
524
|
# @return [Cell, Array]
|
|
@@ -548,7 +571,7 @@ module Axlsx
|
|
|
548
571
|
[sheet_pr, dimension, sheet_view, column_info,
|
|
549
572
|
sheet_data, @sheet_protection, protected_ranges,
|
|
550
573
|
auto_filter, merged_cells, conditional_formattings,
|
|
551
|
-
data_validations, print_options, page_margins,
|
|
574
|
+
data_validations, hyperlinks, print_options, page_margins,
|
|
552
575
|
page_setup, worksheet_drawing, worksheet_comments,
|
|
553
576
|
tables]
|
|
554
577
|
end
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
module Axlsx
|
|
2
|
+
|
|
3
|
+
# A worksheet hyperlink object. Note that this is not the same as a drawing hyperlink object.
|
|
4
|
+
class WorksheetHyperlink
|
|
5
|
+
|
|
6
|
+
# Creates a new hyperlink object.
|
|
7
|
+
# @note the preferred way to add hyperlinks to your worksheet is the Worksheet#add_hyperlink method
|
|
8
|
+
# @param [Worksheet] worksheet the Worksheet that owns this hyperlink
|
|
9
|
+
# @param [Hash] options options to use when creating this hyperlink
|
|
10
|
+
# @option [String] display Display string, if different from string in string table. This is a property on the hyperlink object, but does not need to appear in the spreadsheet application UI.
|
|
11
|
+
# @option [String] location Location within target. If target is a workbook (or this workbook) this shall refer to a sheet and cell or a defined name. Can also be an HTML anchor if target is HTML file.
|
|
12
|
+
# @option [String] tooltip The tip to display when the user positions the mouse cursor over this hyperlink
|
|
13
|
+
# @option [Symbol] target This is :external by default. If you set it to anything else, the location is interpreted to be the current workbook.
|
|
14
|
+
# @option [String|Cell] ref The location of this hyperlink in the worksheet
|
|
15
|
+
def initialize(worksheet, options={})
|
|
16
|
+
DataTypeValidator.validate "Hyperlink.worksheet", [Worksheet], worksheet
|
|
17
|
+
@worksheet = worksheet
|
|
18
|
+
@target = :external
|
|
19
|
+
options.each do |o|
|
|
20
|
+
self.send("#{o[0]}=", o[1]) if self.respond_to? "#{o[0]}="
|
|
21
|
+
end
|
|
22
|
+
yield self if block_given?
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# String attributes for this object
|
|
26
|
+
STRING_ATTRIBUTES = %w(display location tooltip)
|
|
27
|
+
|
|
28
|
+
#Cell location of hyperlink on worksheet.
|
|
29
|
+
# @return [String]
|
|
30
|
+
attr_reader :ref
|
|
31
|
+
|
|
32
|
+
# Sets the target for this hyperlink. Anything other than :external instructs the library to treat the location as an in-workbook reference.
|
|
33
|
+
# @param [Symbol] target
|
|
34
|
+
def target=(target)
|
|
35
|
+
@target = target
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Sets the cell location of this hyperlink in the worksheet
|
|
39
|
+
# @param [String|Cell] cell_reference The string reference or cell that defines where this hyperlink shows in the worksheet.
|
|
40
|
+
def ref=(cell_reference)
|
|
41
|
+
cell_reference = cell_reference.r if cell_reference.is_a?(Cell)
|
|
42
|
+
|
|
43
|
+
Axlsx::validate_string cell_reference
|
|
44
|
+
@ref = cell_reference
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Dynamically create string attribute accessors
|
|
48
|
+
STRING_ATTRIBUTES.each do |attr|
|
|
49
|
+
class_eval %{
|
|
50
|
+
# The #{attr} attribute reader
|
|
51
|
+
# @return [String]
|
|
52
|
+
attr_reader :#{attr}
|
|
53
|
+
|
|
54
|
+
# The #{attr} writer
|
|
55
|
+
# @param [String] value The value to assign to #{attr}
|
|
56
|
+
# @return [String]
|
|
57
|
+
def #{attr}=(value)
|
|
58
|
+
Axlsx::validate_string(value)
|
|
59
|
+
@#{attr}= value
|
|
60
|
+
end
|
|
61
|
+
}
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# The relationship required by this hyperlink when the taget is :external
|
|
65
|
+
# @return [Relationship]
|
|
66
|
+
def relationship
|
|
67
|
+
return unless @target == :external
|
|
68
|
+
Relationship.new HYPERLINK_R, location, :target_mode => :External
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# The id of the relationship for this object
|
|
72
|
+
# @return [String]
|
|
73
|
+
def id
|
|
74
|
+
return unless @target == :external
|
|
75
|
+
"rId#{(@worksheet.relationships_index_of(self)+1)}"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# Seralize the object
|
|
79
|
+
# @param [String] str
|
|
80
|
+
# @return [String]
|
|
81
|
+
def to_xml_string(str='')
|
|
82
|
+
str << '<hyperlink '
|
|
83
|
+
serialization_values.map { |key, value| str << key.to_s << '="' << value.to_s << '" ' }
|
|
84
|
+
str << '/>'
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# The values to be used in serialization based on the target.
|
|
88
|
+
# location should only be specified for non-external targets.
|
|
89
|
+
# r:id should only be specified for external targets.
|
|
90
|
+
# @return [Hash]
|
|
91
|
+
def serialization_values
|
|
92
|
+
h = instance_values.reject { |key, value| !%w(display ref tooltip).include?(key) }
|
|
93
|
+
if @target == :external
|
|
94
|
+
h['r:id'] = id
|
|
95
|
+
else
|
|
96
|
+
h['location'] = location
|
|
97
|
+
end
|
|
98
|
+
h
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
module Axlsx
|
|
2
|
+
#A collection of hyperlink objects for a worksheet
|
|
3
|
+
class WorksheetHyperlinks < SimpleTypedList
|
|
4
|
+
|
|
5
|
+
# Creates a new Hyperlinks collection
|
|
6
|
+
# @param [Worksheet] worksheet the worksheet that owns these hyperlinks
|
|
7
|
+
def initialize(worksheet)
|
|
8
|
+
DataTypeValidator.validate "Hyperlinks.worksheet", [Worksheet], worksheet
|
|
9
|
+
@worksheet = worksheet
|
|
10
|
+
super WorksheetHyperlink
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# Creates and adds a new hyperlink based on the options provided
|
|
14
|
+
# @see WorksheetHyperlink#initialize
|
|
15
|
+
# @return [WorksheetHyperlink]
|
|
16
|
+
def add(options)
|
|
17
|
+
@list << WorksheetHyperlink.new(@worksheet, options)
|
|
18
|
+
@list.last
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def relationships
|
|
22
|
+
return [] if empty?
|
|
23
|
+
map { |hyperlink| hyperlink.relationship }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# seralize the collection of hyperlinks
|
|
27
|
+
# @return [String]
|
|
28
|
+
def to_xml_string(str='')
|
|
29
|
+
return if empty?
|
|
30
|
+
str << '<hyperlinks>'
|
|
31
|
+
@list.each { |hyperlink| hyperlink.to_xml_string(str) }
|
|
32
|
+
str << '</hyperlinks>'
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
data/test/tc_package.rb
CHANGED
|
@@ -7,6 +7,7 @@ class TestPackage < Test::Unit::TestCase
|
|
|
7
7
|
ws = @package.workbook.add_worksheet
|
|
8
8
|
ws.add_row ['Can', 'we', 'build it?']
|
|
9
9
|
ws.add_row ['Yes!', 'We', 'can!']
|
|
10
|
+
ws.add_hyperlink :ref => ws.rows.first.cells.last, :location => 'https://github.com/randym'
|
|
10
11
|
ws.workbook.add_defined_name("#{ws.name}!A1:C2", :name => '_xlnm.Print_Titles', :hidden => true)
|
|
11
12
|
ws.protect_range('A1:C1')
|
|
12
13
|
ws.protect_range(ws.rows.last.cells)
|
|
@@ -268,6 +268,22 @@ class TestCell < Test::Unit::TestCase
|
|
|
268
268
|
|
|
269
269
|
end
|
|
270
270
|
|
|
271
|
+
def test_font_size_with_custom_style_and_no_sz
|
|
272
|
+
@c.style = @c.row.worksheet.workbook.styles.add_style :bg_color => 'FF00FF'
|
|
273
|
+
sz = @c.send(:font_size)
|
|
274
|
+
assert_equal(sz, @c.row.worksheet.workbook.styles.fonts.first.sz)
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
def test_font_size_with_custom_sz
|
|
278
|
+
@c.style = @c.row.worksheet.workbook.styles.add_style :sz => 52
|
|
279
|
+
sz = @c.send(:font_size)
|
|
280
|
+
assert_equal(sz, 52)
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
def test_cell_with_sz
|
|
284
|
+
@c.sz = 25
|
|
285
|
+
assert_equal(25, @c.send(:font_size))
|
|
286
|
+
end
|
|
271
287
|
def test_to_xml
|
|
272
288
|
# TODO This could use some much more stringent testing related to the xml content generated!
|
|
273
289
|
@ws.add_row [Time.now, Date.today, true, 1, 1.0, "text", "=sum(A1:A2)"]
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
require 'tc_helper.rb'
|
|
2
|
+
|
|
3
|
+
class TestWorksheetHyperlink < Test::Unit::TestCase
|
|
4
|
+
def setup
|
|
5
|
+
p = Axlsx::Package.new
|
|
6
|
+
wb = p.workbook
|
|
7
|
+
@ws = wb.add_worksheet
|
|
8
|
+
@options = { :location => 'https://github.com/randym/axlsx', :tooltip => 'axlsx', :ref => 'A1', :display => 'AXSLX', :target => :internal }
|
|
9
|
+
@a = @ws.add_hyperlink @options
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def test_initailize
|
|
13
|
+
assert_raise(ArgumentError) { Axlsx::WorksheetHyperlink.new }
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def test_location
|
|
17
|
+
assert_equal(@options[:location], @a.location)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def test_tooltip
|
|
21
|
+
assert_equal(@options[:tooltip], @a.tooltip)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def test_target
|
|
25
|
+
assert_equal(@options[:target], @a.instance_values['target'])
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def test_display
|
|
29
|
+
assert_equal(@options[:display], @a.display)
|
|
30
|
+
end
|
|
31
|
+
def test_ref
|
|
32
|
+
assert_equal(@options[:ref], @a.ref)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def test_id
|
|
36
|
+
@a.target = :external
|
|
37
|
+
|
|
38
|
+
assert_equal("rId1", @a.id)
|
|
39
|
+
@a.target = :internal
|
|
40
|
+
assert_equal(nil, @a.id)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def test_to_xml_string_with_non_external
|
|
45
|
+
doc = Nokogiri::XML(@ws.to_xml_string)
|
|
46
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@ref='#{@a.ref}']").size, 1)
|
|
47
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@tooltip='#{@a.tooltip}']").size, 1)
|
|
48
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@location='#{@a.location}']").size, 1)
|
|
49
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@display='#{@a.display}']").size, 1)
|
|
50
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@r:id='#{@a.id}']").size, 0)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def test_to_xml_stirng_with_external
|
|
54
|
+
@a.target = :external
|
|
55
|
+
doc = Nokogiri::XML(@ws.to_xml_string)
|
|
56
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@ref='#{@a.ref}']").size, 1)
|
|
57
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@tooltip='#{@a.tooltip}']").size, 1)
|
|
58
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@display='#{@a.display}']").size, 1)
|
|
59
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@location='#{@a.location}']").size, 0)
|
|
60
|
+
assert_equal(doc.xpath("//xmlns:hyperlink[@r:id='#{@a.id}']").size, 1)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: axlsx
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.2.
|
|
4
|
+
version: 1.2.2
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
autorequire:
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2012-08-
|
|
12
|
+
date: 2012-08-27 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: nokogiri
|
|
@@ -226,6 +226,8 @@ files:
|
|
|
226
226
|
- lib/axlsx/workbook/worksheet/worksheet.rb
|
|
227
227
|
- lib/axlsx/workbook/worksheet/worksheet_comments.rb
|
|
228
228
|
- lib/axlsx/workbook/worksheet/worksheet_drawing.rb
|
|
229
|
+
- lib/axlsx/workbook/worksheet/worksheet_hyperlink.rb
|
|
230
|
+
- lib/axlsx/workbook/worksheet/worksheet_hyperlinks.rb
|
|
229
231
|
- lib/axlsx.rb
|
|
230
232
|
- lib/schema/dc.xsd
|
|
231
233
|
- lib/schema/dcmitype.xsd
|
|
@@ -275,6 +277,8 @@ files:
|
|
|
275
277
|
- examples/example_streamed.xlsx
|
|
276
278
|
- examples/extractive.pdf
|
|
277
279
|
- examples/extractive.rb
|
|
280
|
+
- examples/hyperlinks.rb
|
|
281
|
+
- examples/hyperlinks.xlsx
|
|
278
282
|
- examples/image1.gif
|
|
279
283
|
- examples/image1.jpeg
|
|
280
284
|
- examples/image1.jpg
|
|
@@ -285,6 +289,7 @@ files:
|
|
|
285
289
|
- examples/shared_strings_example.xlsx
|
|
286
290
|
- examples/sheet_protection.rb
|
|
287
291
|
- examples/sheet_view.rb
|
|
292
|
+
- examples/skydrive/axlsx.xlsx
|
|
288
293
|
- examples/skydrive/real_example.rb
|
|
289
294
|
- examples/two_cell_anchor_image.rb
|
|
290
295
|
- LICENSE
|
|
@@ -385,6 +390,7 @@ files:
|
|
|
385
390
|
- test/workbook/worksheet/tc_sheet_protection.rb
|
|
386
391
|
- test/workbook/worksheet/tc_sheet_view.rb
|
|
387
392
|
- test/workbook/worksheet/tc_worksheet.rb
|
|
393
|
+
- test/workbook/worksheet/tc_worksheet_hyperlink.rb
|
|
388
394
|
homepage: https://github.com/randym/axlsx
|
|
389
395
|
licenses: []
|
|
390
396
|
post_install_message:
|
|
@@ -405,7 +411,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
405
411
|
version: '0'
|
|
406
412
|
segments:
|
|
407
413
|
- 0
|
|
408
|
-
hash:
|
|
414
|
+
hash: 3191615889138307722
|
|
409
415
|
requirements: []
|
|
410
416
|
rubyforge_project:
|
|
411
417
|
rubygems_version: 1.8.24
|
|
@@ -506,4 +512,5 @@ test_files:
|
|
|
506
512
|
- test/workbook/worksheet/tc_sheet_protection.rb
|
|
507
513
|
- test/workbook/worksheet/tc_sheet_view.rb
|
|
508
514
|
- test/workbook/worksheet/tc_worksheet.rb
|
|
515
|
+
- test/workbook/worksheet/tc_worksheet_hyperlink.rb
|
|
509
516
|
has_rdoc: axlsx
|