Skip to content

Can not call to_html on paragraphs in table cells if they contain hyper links #160

@perryn

Description

@perryn

Describe the bug

If you have a table in a document with a hyperlink in one of the cells, than an attempt to get the html of paragraphs in that cell will throw an exception:

  NoMethodError:
       undefined method `[]' for nil

example

Assuming /path/to/your/docx/file.docx contains a table that has a hyperlink in the first cell

require 'docx'

doc = Docx::Document.new('/path/to/your/docx/file.docx')

## Something to reproduce the bug here
doc.tables[0].rows[0].cells[0].paragraphs.map(&:to_html).join

Expected behavior

Exception should not be thrown. HTML containing the link should be returned.

Cause

The paragraphs method of TableCell does not have access to document_properties for the document, and so they are defaulted to an empty hash. This causes an exception when trying to access document_properties[:hyperlinks] in the href method of TextRun

Workaround

I was able to work around this problem like this:

doc = Docx::Document.new('/path/to/your/docx/file.docx')

Docx::Elements::Containers::TableCell.class_eval do

      @@document = doc

      def paragraphs
        @node.xpath("w:p").map { |p_node| @@document.send(:parse_paragraph_from, p_node) }
      end
 end

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions