davidgohel / ReporteRs

Note that ReporteRs has been removed from CRAN the 16th of July 2018 and is not maintained anymore. please migrate to officer.
244 stars 44 forks source link

addParagraph.docx does not use stylename argument for value class set_of_paragraphs or pot #171

Closed brianstamper closed 7 years ago

brianstamper commented 7 years ago

According to the addParagraph documentation, one can make use of a stylename to apply a style from a docx template. However this only works if the value is a character vector - it does not work with set_of_paragraphs or pot objects.

If I could stick to character vector paragraphs this would not be a problem, but it seems pot objects are needed to add a footnote to a sentence.

davidgohel commented 7 years ago

OK, documentation should be then corrected. Why is it an issue? Can't you use parProperties?

brianstamper commented 7 years ago

I had been building all of the style details in my docx template, would just need to convert that work to parProperties, textProperties, etc. What I ended up with when I discovered this issue was many paragraphs with the correct font, indentation, etc., but in the paragraphs I had to build using set_of_paragraphs so as to contain a pot with a footnote those had the wrong font, indentation, etc.

I've already begun rebuilding my paragraphs using pots with textProperties defined and inserting those into paragraphs with parProperties defined. The stylename route is certainly more convenient. I also find that some things are missing from the parProperties options, like paragraph indent.

If there was some kind of accessor to a docx object that fetches the properties of a template style that might solve a lot, though admittedly I have no idea how difficult that would be to produce. Something like

# Get the parts of MyStyle that are text properties
my_text_props <- textProperties(doc = my_docx_obj, stylename = 'MyStyle')

# Get the parts of MyStyle that are paragraph properties
my_par_props < parProperties(doc = my_docx_obj, stylename = 'MyStyle')

This way we could 'import' a style, so to speak, and be able to do fun things like chprops on those styles as needed.

davidgohel commented 7 years ago

I like the idea but it requires too much work and certainly a lot of documentation to update (a nightmare for me as a bad english writer).

I am rewriting something simpler here: https://github.com/davidgohel/officer

When that work will be finished, you will be able to use any paragraph, character or table style from your word template.

davidgohel commented 7 years ago

for default font, use options("ReporteRs-default-font" = "Arial") and for indenting, use padding in parProperties

brianstamper commented 7 years ago

I understand, yes could be much work. That officer looks quite nice - time better spent on that. I agree that this issue can just be a documentation fix.

For now I'm okay with defining textProperties and parProperties at the top of my code and then using those throughout, sometimes with a chprops. The paragraph indent I'm looking for is the 'first line' type, not on the whole paragraph, which is what padding is giving me. This is okay, I'm just embedding a tab character at the beginning of the paragraphs instead.

Thank you very much for your work on this package, it is very useful! Looking forward to officer as well.

davidspellmeyer commented 7 years ago

I have a little bit more to add on this issue.

First, I have used the excellent ReporteRs package for about 2 years now and have come to rely on it. Great package. I generate large Word documents formatted precisely with my corporate styles sheet. The use of the Styles is highly desirable in such large files. The use of textProperties and parProperties is not sufficient.

Adding paragraph styles using a pot in addParagraph has worked until I updated all my R packages and ReporteRs was also updated.

In release 0.8.6, addParagraph was able to add a pot with a designated style (e.g. "Normal"). That no longer works in release 0.8.8. Since this is a feature that I rely on a lot, I've tried to track down a change between two releases.

Here is some simple code to add two paragraphs, one with only text and one with a pot.

p1 <- "Paragraph 1: Just some text."
doc <- addParagraph(doc,p1,stylename="A-BodyText")

p2 <- "And more text"
p2.pot <- pot("Paragraph 2: ", textProperties(font.weight = 'bold')) + p2
doc <- addParagraph(doc,p2.pot,stylename="A-BodyText")

stepping through the functions in Release 0.8.6

In release 0.8.6, this code accurately uses my corporate "A-BodyText" style for both paragraph 1 and 2. Both paragraphs are added properly with the .jcall with this conditional in 0.8.6:

if( missing( bookmark ) && !missing( stylename ) ){
   .jcall( doc$obj, "V", "addWithStyle" , parset, stylename) 
} ...

stepping through the functions in Release 0.8.8

In release 0.8.8, the first call to addParagraph works the same as in 0.8.6. The second addParagraph behaves differently.

When I open the resulting docx (from 0.8.8) in Word, it appears as if "Normal" has been applied, but it has not. In the resulting document.xml, that paragraph has no style at all assigned. That's a bit odd for Word.

The first call to addParagraph, the .jcall in this sequence is executed:

if (missing(bookmark)) {
      .jcall(doc$obj, "V", "addWithStyle", .jarray(value), 
        stylename)
} 

followed by return(doc). That looks very much like the call using 0.8.6.

The second paragraph (with a pot) executes this .jcall to add the paragraph:

if (missing(bookmark)) {
  .jcall(doc$obj, "V", "add", parset)
}

My debugging skills are not sufficient to figure out which set of logical checks is different between the versions.

Again, I thinks this is an excellent package! I hope this adds some context and I am happy to provide any additional information on this issue.