davidgohel / flextable

table farming
https://ardata-fr.github.io/flextable-book/
562 stars 81 forks source link

`headers_flextable_at_bkm` function does not use `bookmark` parameter #540

Closed akochetkova closed 1 year ago

akochetkova commented 1 year ago

Code for the headers_flextable_at_bkm function does not use bookmark parameter. Instead the bookmark actually being search for is hardcoded in the function source code as \"hd_summary_tbl\" in the following line:

node <- xml_find_first(footer$get(), "//w:bookmarkStart[@w:name=\"hd_summary_tbl\"]")

Therefore the function would only replace at "hd_summary_tbl" bookmark, if such is present in the header, regardless of an actual bookmark parameter supplied.

akochetkova commented 1 year ago
> sessionInfo()
R version 4.1.1 (2021-08-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19045)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] zip_2.2.0               Rcpp_1.0.7              fontBitstreamVera_0.1.1
 [4] compiler_4.1.1          later_1.3.0             gfonts_0.2.0           
 [7] tools_4.1.1             uuid_0.1-4              digest_0.6.27          
[10] evaluate_0.14           jsonlite_1.7.2          lifecycle_1.0.2        
[13] rlang_1.0.5             shiny_1.7.2             cli_3.4.0              
[16] rstudioapi_0.13         crul_1.3                curl_4.3.2             
[19] fontLiberation_0.1.0    xfun_0.25               fastmap_1.1.0          
[22] xml2_1.3.2              officer_0.6.2           knitr_1.34             
[25] gdtools_0.3.3           systemfonts_1.0.4       askpass_1.1            
[28] grid_4.1.1              glue_1.4.2              httpcode_0.3.0         
[31] data.table_1.14.0       R6_2.5.1                textshaping_0.3.6      
[34] rmarkdown_2.10          magrittr_2.0.1          fontquiver_0.2.1       
[37] promises_1.2.0.1        htmltools_0.5.2         ellipsis_0.3.2         
[40] mime_0.11               flextable_0.9.1         xtable_1.8-4           
[43] httpuv_1.6.6            ragg_1.2.2              openssl_2.0.3          
[46] crayon_1.4.1 
davidgohel commented 1 year ago

The functions footers_flextable_at_bkm and headers_flextable_at_bkm are going to be deprecated. That was the intention but we forgot them...

These functions did not function as intended and sometimes resulted in unexpected behavior. Instead, we recommend using the prop_section() and block_list() functions from the officer package as alternative solutions. These functions provide more reliable and flexible options for managing footers and headers.

Below an example:

library(officer)
library(flextable)

default_settings <- prop_section(
  type = "nextPage",
  header_default = block_list(
    fpar("coco")
  )
)
section_settings <- prop_section(
  type = "nextPage",
  header_default = block_list(
    qflextable(head(iris, n = 2))
  )
)

doc <- read_docx()
doc <- body_set_default_section(doc, default_settings)

doc <- body_add_par(doc, value = "hello 1", style = "heading 2")

doc <- body_add_flextable(x = doc, as_flextable(airquality))

doc <- body_end_block_section(x = doc, 
  value = block_section(property = section_settings)
)

doc <- body_add_par(doc, value = "hello 3", style = "heading 2")

doc <- body_add_flextable(x = doc, as_flextable(iris))

print(doc, target = tempfile(fileext = ".docx")) |> browseURL()
Capture d’écran 2023-06-06 à 23 39 12
github-actions[bot] commented 11 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.