使用rmarkDown在可弹力表中的MS单词号码

发布于 2025-01-31 19:26:00 字数 292 浏览 5 评论 0原文

有没有办法将MS Word计算的字段插入可弹出的表格中?

军官软件包中,有一个函数run_word_field。我试图以以下方式使用它:

flextable(df) %>%
  compose(value = as_paragraph(
    as_chunk(run_word_field(field = "NumPages  \\* MERGEFORMAT"))
  ))

但是它会引发错误。

有解决方法吗?

Is there a way to insert MS Word computed field - total number of pages in the document - into a flextable table?

In the officer package there's a function run_word_field. I tried to use it the following way:

flextable(df) %>%
  compose(value = as_paragraph(
    as_chunk(run_word_field(field = "NumPages  \\* MERGEFORMAT"))
  ))

but it throws an error.

Is there a workaround?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷︶言冷语的世界 2025-02-07 19:26:00

您现在可以使用函数as_word_field

library(flextable)
library(officer)

# define some default values ----
set_flextable_defaults(font.size = 22, border.color = "gray")

ft_2 <- flextable(head(cars))
ft_2 <- add_footer_lines(ft_2, "temp text")
ft_2 <- compose(
  x = ft_2, part = "footer", i = 1, j = 1, 
  as_paragraph("p. ", 
               as_word_field(x = "Page"),
               " on ", as_word_field(x = "NumPages"))
)
ft_2 <- autofit(ft_2, part = c("header", "body"))

doc <- read_docx() 
doc <- body_add_flextable(doc, ft_2) 
doc <- body_add_break(doc) 
doc <- body_add_flextable(doc, ft_2) 
outfile <- print(doc, target = tempfile(fileext = ".docx")) 

You can now use function as_word_field:

library(flextable)
library(officer)

# define some default values ----
set_flextable_defaults(font.size = 22, border.color = "gray")

ft_2 <- flextable(head(cars))
ft_2 <- add_footer_lines(ft_2, "temp text")
ft_2 <- compose(
  x = ft_2, part = "footer", i = 1, j = 1, 
  as_paragraph("p. ", 
               as_word_field(x = "Page"),
               " on ", as_word_field(x = "NumPages"))
)
ft_2 <- autofit(ft_2, part = c("header", "body"))

doc <- read_docx() 
doc <- body_add_flextable(doc, ft_2) 
doc <- body_add_break(doc) 
doc <- body_add_flextable(doc, ft_2) 
outfile <- print(doc, target = tempfile(fileext = ".docx")) 

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文