I want to write an R script that copies the data range A5:X1000 from "WorksheetX" in "WorkbookX", and pastes values to the same range (A5:X1000) in "WorksheetY" in "WorkbookY". Both of the workbooks are in the same directory.
Is this possible?
I want to write an R script that copies the data range A5:X1000 from "WorksheetX" in "WorkbookX", and pastes values to the same range (A5:X1000) in "WorksheetY" in "WorkbookY". Both of the workbooks are in the same directory.
Is this possible?
The openxlsx
package is, in my opinion, the best for writing to Excel files. There are many more options for reading from Excel files (such as the readxl
package.)
I can't use this package on the machine I'm on at the moment, but this should work.
# NOT TESTED
library(openxlsx)
foo <- read.xlsx("WorkbookX.xlsx", sheet = "WorksheetX", rows = 5:1000, cols = 1:24)
write.xlsx("WorkbookY.xlsx", sheet = "WorksheetY", startRow = 5, startCol = 1)
You can write to multiple sheets with the xlsx package. You just need to use a different sheetName for each data frame and you need to add append=TRUE:
library(xlsx)
write.xlsx(dataframe1, file="filename.xlsx", sheetName="sheet1")
write.xlsx(dataframe2, file="filename.xlsx", sheetName="sheet2", append=TRUE)