Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for all allowed line border styles to fp_border #542

Merged
merged 2 commits into from
Jan 20, 2024

Conversation

trekonom
Copy link
Contributor

@trekonom trekonom commented Jan 2, 2024

Hi David,

bonne année. This PR adds support to fp_border for all line border styles listed in ECMA 376 § 17.18.2 and allowed CSS border styles thereby closing #165 and #443. Personally, I'm not a fan of fancy borders. But okay. That's up to the user. :D

For Word output the new functionality should work "out-of-the-box" in flextable too as shown in the reprex below.

Here is a "minimal" reproducible example showing the new functionality in action:

library(rvest)
library(officer)
library(flextable)

url <- "http://www.datypic.com/sc/ooxml/t-w_ST_Border.html"

html <- read_html(url)

st_border <- (html |>
  html_elements("table.en") |>
  html_table())[[1]]

names(st_border) <- c("style", "description")

st_border$is_line <- grepl("(No|Line) Border$", st_border$description)

st_border_line <- st_border[st_border$is_line, ]

# Officer ----

doc <- read_docx()

for (i in seq_len(nrow(st_border_line))) {
  style <- st_border_line$style[i]
  description <- st_border_line$description[i]
  doc <- body_add_fpar(
    doc,
    value = fpar(
      paste(style, description, sep = " - "),
      fp_p = fp_par(
        border = fp_border(
          color = "red",
          style = style
        )
      )
    )
  )
}

fn <- tempfile(fileext = ".docx")

print(doc, fn)

doconv::to_miniature(
  fn,
  fileout = tempfile(fileext = ".png")
)

# Flextable ----

myft <- flextable(st_border_line[1:2])
myft <- border_remove(x = myft)
myft <- autofit(myft)
myft <- delete_part(myft)
myft <- fontsize(myft, size = 8)

for (i in seq_len(nrow(st_border_line))) {
  style <- st_border_line$style[i]
  myft <- surround(
    myft,
    i = i,
    border = fp_border(
      color = "red",
      style = style
    )
  )
}

fn <- tempfile(fileext = ".docx")

save_as_docx(myft, path = fn)

doconv::to_miniature(
  fn,
  fileout = tempfile(fileext = ".png")
)

Created on 2024-01-02 with reprex v2.0.2

Let me know what you think of it and if you have any suggestions for improvement.

Best, Stefan

@codecov-commenter
Copy link

codecov-commenter commented Jan 2, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (312daa5) 73.24% compared to head (1d1ad5d) 73.29%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #542      +/-   ##
==========================================
+ Coverage   73.24%   73.29%   +0.05%     
==========================================
  Files          40       40              
  Lines        5494     5501       +7     
==========================================
+ Hits         4024     4032       +8     
+ Misses       1470     1469       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@davidgohel
Copy link
Owner

Thanks again @trekonom, very good to me, as usual

I'm also not a big fan of borders other than the 3 that were implemented, but that's okay.

I'll have to implement the PPTX output a little later. This will make it possible to take advantage of 'flextable', for which the feature is only available in Word for the moment, but since it's controlled in the 'flextable' code, it won't cause any bugs.

Best,
David

@davidgohel davidgohel merged commit 930cc93 into davidgohel:master Jan 20, 2024
5 checks passed
@trekonom trekonom deleted the issue-443-165 branch May 18, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants