删除一列,保持单元格的格式。

huangapple go评论111阅读模式
英文:

Delete a column keeping the format of the cells

问题

I want to remove column 2 from this excel while maintaining the color format.

library(openxlsx)

wkbook <- loadWorkbook(file = "data.xlsx")
deleteData(wkbook, sheet = 1, cols = 2, rows = 1:5, gridExpand = T)
saveWorkbook(wkbook, "data2.xlsx",overwrite = TRUE)

Data is removed, but column remains.
I'm looking to completely remove the column. But I do not know how to do it.

英文:

I want to remove column 2 from this excel while maintaining the color format.

删除一列,保持单元格的格式。

library(openxlsx)

wkbook &lt;- loadWorkbook(file = &quot;data.xlsx&quot;)
deleteData(wkbook, sheet = 1, cols = 2, rows = 1:5, gridExpand = T)
saveWorkbook(wkbook, &quot;data2.xlsx&quot;,overwrite = TRUE)

删除一列,保持单元格的格式。

Data is removed, but column remains.
I'm looking to completely remove the column. But I do not know how to do it.

删除一列,保持单元格的格式。

答案1

得分: 2

我不确定如何在openxlsx中执行类似此操作。使用openxlsx2是可能的,我将展示如何在下面执行此操作。然而,我不会在openxlsx2中添加此功能,因为它有破坏工作簿的倾向。

如果您在Excel等电子表格软件中执行此操作,软件会负责更新到B列的引用。在openxlsx2中,我们不这样做。我们不跟踪公式、图表或数据透视表,不管是在单个工作簿中还是在各种工作簿之间。因此,行为将完全不同,并且只在极少数情况下有效,就像上面的人工示例,其中工作表仅包含(样式化的)单元格数据。

话虽如此,这是您要求的内容:

## 创建文件
dat <- data.frame(
  AA = 1:4,
  BBB = 2:5,
  VVV = 6:9
)

library(openxlsx2)

wb <- wb_workbook()$add_worksheet()$add_data(x = dat)

wb$add_fill(dims = "A1:C5", color = wb_color("yellow"))
wb$add_fill(dims = "A2:A3", color = wb_color("red"))
wb$add_fill(dims = "A5", color = wb_color("lightgray"))
wb$add_fill(dims = "C4", color = wb_color("lightgray"))
wb$add_fill(dims = "B2;B4", color = wb_color("lightblue"))

###

wb_to_df(wb)
#>   AA BBB VVV
#> 2  1   2   6
#> 3  2   3   7
#> 4  3   4   8
#> 5  4   5   9

# 获取工作表的内部数据框
cc <- wb$worksheets[[1]]$sheet_data$cc

# 删除列B
cc <- cc[cc$c_r %in% c("A", "C"), ]
# 将列C变为新的列B
cc$r <- gsub("C", "B", cc$r)
cc$c_r <- gsub("C", "B", cc$c_r)

# 将内部数据框重新推送回工作表
wb$worksheets[[1]]$sheet_data$cc <- cc

wb_to_df(wb)
#>   AA VVV
#> 2  1   6
#> 3  2   7
#> 4  3   8
#> 5  4   9

# wb$save("example_file.xlsx")
# wb$open()

希望这对您有所帮助。

英文:

I am not sure how to do something like this in openxlsx. It is possible with openxlsx2 and I show how to do it below. However, I will not add a function for this in openxlsx2, because it has a tendency to break workbooks to the left and right.

If you do something like this in a spreadsheet software like Excel, the software will take care of updating the references to column B. In openxlsx2 we do nothing of the sort. We do not track formulas, charts, or pivot tables, not in a single workbook and not across various workbooks. Therefore the behavior would be completely different and would only work in rare cases. Like the artificial example above with worksheets containing only (styled) data in the cells.

That said, here is what you asked for:

## create file
dat &lt;- data.frame(
  AA = 1:4,
  BBB = 2:5,
  VVV = 6:9
)

library(openxlsx2)

wb &lt;- wb_workbook()$add_worksheet()$add_data(x = dat)

wb$add_fill(dims = &quot;A1:C5&quot;, color = wb_color(&quot;yellow&quot;))
wb$add_fill(dims = &quot;A2:A3&quot;, color = wb_color(&quot;red&quot;))
wb$add_fill(dims = &quot;A5&quot;, color = wb_color(&quot;lightgray&quot;))
wb$add_fill(dims = &quot;C4&quot;, color = wb_color(&quot;lightgray&quot;))
wb$add_fill(dims = &quot;B2;B4&quot;, color = wb_color(&quot;lightblue&quot;))

###

wb_to_df(wb)
#&gt;   AA BBB VVV
#&gt; 2  1   2   6
#&gt; 3  2   3   7
#&gt; 4  3   4   8
#&gt; 5  4   5   9

# get the internal data frame for the worksheet
cc &lt;- wb$worksheets[[1]]$sheet_data$cc

# remove column B
cc &lt;- cc[cc$c_r %in% c(&quot;A&quot;, &quot;C&quot;), ]
# make column C the new column B
cc$r &lt;- gsub(&quot;C&quot;, &quot;B&quot;, cc$r)
cc$c_r &lt;- gsub(&quot;C&quot;, &quot;B&quot;, cc$c_r)

# push the internal data frame back into the worksheet
wb$worksheets[[1]]$sheet_data$cc &lt;- cc

wb_to_df(wb)
#&gt;   AA VVV
#&gt; 2  1   6
#&gt; 3  2   7
#&gt; 4  3   8
#&gt; 5  4   9

# wb$save(&quot;example_file.xlsx&quot;)
# wb$open()

huangapple
  • 本文由 发表于 2023年3月7日 20:37:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75662068.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定