Skip to content

Commit 73ed8c2

Browse files
committed
Prevent adding more hyperlinks than the allowed limit per worksheet in Excel of 65,530.
Exceeding this limit will result in an error when opening up the file, similar to this: > We found a problem with some content in ‘.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes. With this change we now prevent the creation of hyperlinks if the limit of 65,530 has been reached. We will also output a warning to the Rails log but only once. It will show the worksheet name as well as the row and column where it was exceeded. Fixes #475
1 parent ca2ad29 commit 73ed8c2

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

  • lib/rubyXL/convenience_methods

lib/rubyXL/convenience_methods/cell.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module RubyXL
22
module CellConvenienceMethods
3+
EXCEL_HYPERLINK_LIMIT_PER_WORKSHEET = 65_530 # Excel has a hard limit of 65,530 hyperlinks per Worksheet. HYPERLINK formulas are not included in this limit.
4+
35
def change_contents(data, formula_expression = nil)
46
validate_worksheet
57

@@ -263,6 +265,15 @@ def add_hyperlink(url, tooltip = nil)
263265
hyperlink = RubyXL::Hyperlink.new(:ref => self.r, :r_id => r_id)
264266
hyperlink.tooltip = tooltip if tooltip
265267
worksheet.hyperlinks ||= RubyXL::Hyperlinks.new
268+
269+
if worksheet.hyperlinks.size >= EXCEL_HYPERLINK_LIMIT_PER_WORKSHEET # Do not create hyperlinks once the Excel limit of 65,530 is reached. Return false.
270+
unless @excel_hyperlink_limit_per_worksheet_warned
271+
Rails.logger.warn("Excel hyperlink limit (65,530) reached in worksheet '#{worksheet.sheet_name}' at row #{row}, column #{column}. Further hyperlinks skipped.")
272+
@excel_hyperlink_limit_per_worksheet_warned = true
273+
end
274+
return false
275+
end
276+
266277
worksheet.hyperlinks << hyperlink
267278
end
268279

0 commit comments

Comments
 (0)