2

我正在尝试以某种格式构建 excel xlsx 工作簿的标题。有些字符串太大,需要包裹起来。

但是当我使用strwrap添加\n字符时,setHeaderFooter 告诉我我有三个以上的参数

例如:

require (openxlsx)
require (tidyverse)

wb0 <- createWorkbook()
addWorksheet(wb0, "Sheet 1")

label <- "&B EJECUTIVOS DE CUENTA: &B"
value <- "FANARA ARIEL GUSTAVO:476, BARETTO ANA SILVINA:34, NO APLICA:32, SANTOS MAXIMILIANO ARIEL:21, AVILA ROBERTO:19, REGGI PABLO:9, Otros:51"


setHeaderFooter (
  wb0, sheet = 1
  , header = c ( 
    # "left side / izquierda"
    strwrap (
      x = paste0 (
          label, " "
        , substr (value, start = 1, stop = 360)
      )
      , width = 45
      , prefix = "\n", initial = ""
    )
    , "center header / centro"
    , "rigth side / derecha"
  ) 
  , footer = c (
    "&[Date]"
    ,  NA
    , "&[File]"
  )
)    
4

1 回答 1

2

strwrap正在创建一个长度为 4 的向量,因此标题中的元素总数为 6,而不是所需的 3 setHeaderFooter

strwrap(x = paste0 (label, " ", substr (value, start = 1, stop = 360)), 
        width = 45, prefix = "\n", initial = "")

[1] "&B EJECUTIVOS DE CUENTA: &B FANARA ARIEL"    "\nGUSTAVO:476, BARETTO ANA SILVINA:34, NO"   "\nAPLICA:32, SANTOS MAXIMILIANO ARIEL:21,"  
[4] "\nAVILA ROBERTO:19, REGGI PABLO:9, Otros:51"

因此,让我们将其包装成一个字符串向量paste

paste(strwrap(x = paste0 (label, " ", substr (value, start = 1, stop = 360)), 
              width = 45, prefix = "\n", initial = ""), 
      collapse="")

[1] "&B EJECUTIVOS DE CUENTA: &B FANARA ARIEL\nGUSTAVO:476, BARETTO ANA SILVINA:34, NO\nAPLICA:32, SANTOS MAXIMILIANO ARIEL:21,\nAVILA ROBERTO:19, REGGI PABLO:9, Otros:51"

现在您将获得一个包含函数期望的 3 个元素的标题。下面是 Excel 文件中的标题:

在此处输入图像描述

于 2018-08-30T16:42:30.163 回答