修改可伸缩单元格一部分的格式

史考特

我想修改单元格一部分的格式,而其余文本保持不变,以包括在.docx报告中:
例如,Brill(Scophthalmus rhombus)位于22-32分区(波罗的海)

我的数据是使用HTML标签设置的,我一直在尝试使用该display()功能进行修改我的示例(有点明显)修改了整个单元格。我希望pattern可以使用来修改参数,{{moustaches}}以使嵌套格式成为可能,但是我没有任何运气。

library(flextable)
library(officer)
library(dplyr)

data <- structure(list(Description = c("Brill (<em>Scophthalmus rhombus</em>) in subdivisions 22-32 (Baltic Sea)", 
                                       "Cod (<em>Gadus morhua</em>) in subdivisions 22-24, western Baltic stock (western Baltic Sea)", 
                                       "Cod (<em>Gadus morhua</em>) in subdivisions 25-32, eastern Baltic stock (eastern Baltic Sea)", 
                                       "Dab (<em>Limanda limanda</em>) in subdivisions 22-32 (Baltic Sea)", 
                                       "Flounder (<em>Platichthys flesus</em>) in subdivisions 22 and 23 (Belt Seas and the Sound)", 
                                       "Flounder (<em>Platichthys flesus</em>) in subdivisions 24 and 25 (west of Bornholm and southwestern central Baltic)"),
                       SpeciesScientificName = c("Scophthalmus rhombus", "Gadus morhua", "Gadus morhua", "Limanda limanda", "Platichthys flesus", "Platichthys flesus")),
                  .Names = c("Description", "SpeciesScientificName"), 
                  row.names = c(NA, 6L), class = "data.frame")

data %>% 
  mutate(Description = gsub("<em>.*?</em>", "%s", Description)) %>%
  flextable() %>% 
  display(col_key = "Description", pattern = "{{sp_italics}}",
          formatters = list(sp_italics ~ sprintf(Description, SpeciesScientificName)),
          fprops = list(sp_italics = fp_text(italic = TRUE)))
大卫·高厄尔

显示功能正在定义显示值。它不允许仅格式化单元格中的文本a的一部分,它定义了段落内容及其格式。我将从一个整洁的data.frame开始

data <- structure(list(id = c("Brill", "Cod", "Cod", "Dab", "Flounder", 
"Flounder"), SpeciesScientificName = c("Scophthalmus rhombus", 
"Gadus morhua", "Gadus morhua", "Limanda limanda", "Platichthys flesus", 
"Platichthys flesus"), subdivision = c("22-32", "22-24, western Baltic stock", 
"25-32, eastern Baltic stock", "22-32", "22 and 23", "24 and 25"
), location = c("Baltic Sea", "western Baltic Sea", "eastern Baltic Sea", 
"Baltic Sea", "Belt Seas and the Sound", "west of Bornholm and southwestern central Baltic"
)), class = "data.frame", .Names = c("id", "SpeciesScientificName",  "subdivision", "location"), 
row.names = c(NA, -6L))

> data
        id SpeciesScientificName                 subdivision
1    Brill  Scophthalmus rhombus                       22-32
2      Cod          Gadus morhua 22-24, western Baltic stock
3      Cod          Gadus morhua 25-32, eastern Baltic stock
4      Dab       Limanda limanda                       22-32
5 Flounder    Platichthys flesus                   22 and 23
6 Flounder    Platichthys flesus                   24 and 25
                                          location
1                                       Baltic Sea
2                               western Baltic Sea
3                               eastern Baltic Sea
4                                       Baltic Sea
5                          Belt Seas and the Sound
6 west of Bornholm and southwestern central Baltic

您的示例在一个段落中连接了4条信息,该参数formatters将需要4个参数...

ft <- data %>% 
  flextable(col_keys = c("dummy_col", "SpeciesScientificName")) %>% 
  display(col_key = "dummy_col", pattern = "{{id_}} ({{sciname_}}) in {{subdivision_}} ({{location_}})",
          formatters = list(id_ ~ id, sciname_ ~ SpeciesScientificName, subdivision_ ~ subdivision, location_ ~ location ),
          fprops = list(sciname_ = fp_text(italic = TRUE), location_ = fp_text(color="red") )) %>% 
  set_header_labels(dummy_col="whatever") %>% 
  theme_booktabs() %>% autofit()

read_docx() %>% body_add_flextable(ft) %>% print("test.docx")

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

减去单元格的一部分

根据另一个单元格的内容将格式应用于单元格的一部分

将单元格的一部分与另一列进行比较

Excel:将单元格格式中显示的文本集识别为单元格文本的一部分

UITableViewCell仅突出显示单元格的一部分?

使用单元格中的值作为图纸参考的一部分

Excel网格线是单元格的一部分还是多余的空间?

选择单元格作为要执行的代码的一部分

将单元格的值用作宏的一部分

在 HTML 表格中隐藏单元格的一部分

在单元格的一部分中更改特定字符

将单元格的一部分更改为粗体

VB Excel中替换单元格循环中的单词的一部分

您如何提取单元格的第一部分?

更改Excel Forumla的一部分以包含来自另一个单元格的数据

Excel-根据另一个单元格的一部分计算特定值

从/xl/worksheets/sheet1.xml删除的记录合并单元格的一部分Coldfusion poi

官方API或检查单元格是否为动态数组一部分的方法

如何在Excel的VBA中仅匹配单元格值的一部分而不是完全匹配?

在 Libreoffice Calc Basic 中将单元格文本的一部分标记为粗体?

在XLS或CSV文件中用不同的颜色写入单元格的一部分

我如何使用python在Excel单元格中仅加粗字符串的一部分

在CSV中找到字符串的一部分,并用新条目替换整个单元格?

更改变量的字体颜色作为单元格中文本的一部分

VBA Excel 宏另存为带有日期的单元格的一部分

Python pptx-单元格中具有不同颜色的文本的一部分

在Excel VBA中使用单元格值(文本)作为公式的一部分

将集合视图中的单元格移到另一部分的最后一个单元格之外不起作用

如果单元格碰巧是非连续范围的一部分,则通过excel vba更改单元格的值