使用RDCOMClient,我们可以直接替换Excel单元格中的值,例如
range <- sheet$Range('A1')
range[['Value']] <- 1.2
在Word中,文本可以从范围对象的Text
属性中检索,例如
word.doc[['Tables']][[1]]$Cell(2, 2)[['Range']][['Text']]
# "12/28/2022ra"
但是,虽然范围对象的文档说Text
属性可以用来检索或设置范围的文本,这在RDCOMClient中不起作用:
word.doc[['Tables']][[1]]$Cell(2, 2)[['Range']][['Text']] <- "test"
# Error in word.doc[["Tables"]][[1]]$Cell(2, 2)[["Range"]][["Text"]] <- "test" :
target of assignment expands to non-language object
是否有更好的方法来编辑单元格或Word中的任何其他范围/选择的内容?
如果没有办法做到这一点,将选择和选择()$TypeText()或搜索替换范围更可取吗?
当您首先获得Range对象(从Word的角度)然后设置其Text属性时,它应该可以工作,例如:
library("RDCOMClient")
app <- COMCreate("Word.Application")
doc <- app$Documents()$Open('path_to_your_file.docx'))
## get the VBA object first:
the_range <- doc[['Tables']][[1]]$Cell(2, 2)[['Range']]
## assign its Text property:
the_range[['Text']] = "No cell is an islandn
Entire of itself;n
Every cell needs a piece of content,n
A part of the main."
doc$Save()