提问人:David Weisser 提问时间:10/28/2023 最后编辑:AxemanDavid Weisser 更新时间:11/18/2023 访问量:33
为什么在数据中使用 & 或 class = 公式字段会干扰OPENXLSX2中的wb_add_data
Why does using an & in data or class = formula fields interfere with wb_add_data in OPENXLSX2
问:
我正在尝试将一个小公式表写入 excel 工作表。它工作得很好,除非我尝试使用“&”字符。这是我的代码:
首先,我添加一个名为“摘要”的工作表,构建一个数据帧,我将用公式填充该数据帧并将其添加到工作簿中。
if(exists("df_tmp")) {rm(df_tmp)}
a <- c("Above and Beyond", "Fully Met", "Partially Met", "Did Not Meet") # try using "&" instead of "and"
b <- c(.15, .70, .10, .05);
c <- c("","","","")
d <- c("","","","")
e <- c("","","","")
f <- c("","","","")
df_tmp <- data.frame(a,b,c,d,e,f)
## Add Formula(s): Summary Table ----
wb <- wb_workbook()
wb$add_worksheet("Summary")
df_tmp$c = paste0('F',seq(4,7),'/SUM($F$4:$F$7)')
df_tmp$d = paste0('ROUNDDOWN((COUNTA(Population!A:A)-1)*C',seq(4,7),',0)')
df_tmp[1,5] = paste0('COUNTIF(Population!G:G,"Above and Beyond")') # try using "&" instead of "and"
df_tmp[2,5] = paste0('COUNTIF(Population!G:G,"Fully Met")')
df_tmp[3,5] = paste0('COUNTIF(Population!G:G,"Partially Met")')
df_tmp[4,5] = paste0('COUNTIF(Population!G:G,"Did Not Meet")')
df_tmp$f = paste0('E',seq(4,7),'-F',seq(4,7))
class(df_tmp$c) <- c(class(df_tmp$c), "formula")
class(df_tmp$d) <- c(class(df_tmp$d), "formula")
class(df_tmp$e) <- c(class(df_tmp$e), "formula")
class(df_tmp$f) <- c(class(df_tmp$f), "formula")
colnames(df_tmp) <- c("Rating","Target Percentage","Actual Percentage", "Target Count", "Actual Count", "Δ to Meet Distribution Targets")
wb <- wb %>% wb_add_data(sheet = "Summary", df_tmp, dims = wb_dims(3, 2))
if (interactive()) wb$open()
这段代码工作得很好。但是,如果我在上面指示的字符串中将“and”替换为“&”,则工作簿会出错。我尝试用“\”转义它,但无论我尝试什么,在尝试打开工作簿时都会出现此错误:
有什么想法吗?& 符号对我来说是一个不幸的要求,因为这些工作簿处理我无法控制的输入数据。希望我跳过一些明显的东西。谢谢你的智慧!
答: 暂无答案
评论
df_tmp[1,5] = paste0('COUNTIF(Population!G:G,"Above & Beyond")')
&
a & b
a & b
a & b
a &amp; b