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 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"