From 73ed8c27c03d26752a947bb59796b9bf7d329ca7 Mon Sep 17 00:00:00 2001 From: Joshua Pinter Date: Wed, 4 Mar 2026 09:42:40 -0500 Subject: [PATCH 1/2] Prevent adding more hyperlinks than the allowed limit per worksheet in Excel of 65,530. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 https://github.com/weshatheleopard/rubyXL/issues/475 --- lib/rubyXL/convenience_methods/cell.rb | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/lib/rubyXL/convenience_methods/cell.rb b/lib/rubyXL/convenience_methods/cell.rb index ca380ea1b..c62fd7a3d 100644 --- a/lib/rubyXL/convenience_methods/cell.rb +++ b/lib/rubyXL/convenience_methods/cell.rb @@ -1,5 +1,7 @@ module RubyXL module CellConvenienceMethods + 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. + def change_contents(data, formula_expression = nil) validate_worksheet @@ -263,6 +265,15 @@ def add_hyperlink(url, tooltip = nil) hyperlink = RubyXL::Hyperlink.new(:ref => self.r, :r_id => r_id) hyperlink.tooltip = tooltip if tooltip worksheet.hyperlinks ||= RubyXL::Hyperlinks.new + + if worksheet.hyperlinks.size >= EXCEL_HYPERLINK_LIMIT_PER_WORKSHEET # Do not create hyperlinks once the Excel limit of 65,530 is reached. Return false. + unless @excel_hyperlink_limit_per_worksheet_warned + Rails.logger.warn("Excel hyperlink limit (65,530) reached in worksheet '#{worksheet.sheet_name}' at row #{row}, column #{column}. Further hyperlinks skipped.") + @excel_hyperlink_limit_per_worksheet_warned = true + end + return false + end + worksheet.hyperlinks << hyperlink end From b87e0f89e7d4ec84dbecaaa8272eba898c1e7133 Mon Sep 17 00:00:00 2001 From: Joshua Pinter Date: Wed, 4 Mar 2026 09:44:01 -0500 Subject: [PATCH 2/2] If the contents of a cell is a URL and does not include a formula, use the `HYPERLINK` formula to make it clickable. This also gets around the 65,530 hyperlinks per Worksheet limit in Excel as these `HYPERLINK` formulas do not contribute to that count. Related to https://github.com/weshatheleopard/rubyXL/issues/475. --- lib/rubyXL/worksheet.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/rubyXL/worksheet.rb b/lib/rubyXL/worksheet.rb index 299cc2fa2..5e6f99f84 100644 --- a/lib/rubyXL/worksheet.rb +++ b/lib/rubyXL/worksheet.rb @@ -57,6 +57,9 @@ def add_cell(row_index = 0, column_index = 0, data = '', formula = nil, overwrit end c.raw_value = data c.datatype = RubyXL::DataType::RAW_STRING + if data.match?( URI::DEFAULT_PARSER.make_regexp ) # If content is a URL then use the HYPERLINK formula to make it clickable. + c.formula = RubyXL::Formula.new(:expression => %(HYPERLINK("#{ data }"))) + end when RubyXL::RichText then if data.to_s.length > TEXT_LENGTH_LIMIT_IN_CELL raise ArgumentError, "The maximum length of cell contents (text) is #{TEXT_LENGTH_LIMIT_IN_CELL} characters"