I have added a worksheet to an excel spreadsheet using the following code using the xlsx package in R
library(xlsx)
wb <- CreateWorkbook()
newsheet <- createSheet(wb, sheetName = 'Sheet1')
Is there a way to automatically change the color of the tab for "Sheet1"?
There is no reference to tabs in xlsx
My first instinct was to look look at XLConnect but there is no reference to tabs there either.
Related
I have an xlsx workbook on my desktop with existing sheets. I have a dataframe in R that I would like to add to this workbook as a new sheet.
Using library(openxlsx) I want to do:
wb <- loadWorkbook("workbook.xlsx", isUnzipped=TRUE)
addWorksheet(wb, "New_Sheet")
writeData(wb, "New_Sheet", df)
saveWorkbook(wb, "workbook.xlsx", overwrite=TRUE)
however the program fails at the first line:
wb <- loadWorkbook("workbook.xlsx", isUnzipped=TRUE)
I get the error message:
Error in match(sheetrID, file_rIds): object sheetrId not found
I also created a dummy file (excel workbook with just two existing sheets and some dummy strings in each sheet) and I get the same error message.
I also tried uninstalling openxlsx and re-installing. No change.
As mentioned in the comments by #MrFlick (thank you!), Excel files are actually zipped files that contain XML files. So the workbook must be unzipped before it can be opened in R.
As the title stated, I would like to delete the second worksheet in excel using r with package xlsx. However, after running the code, nothing happened. Following is a sample code I used to delete the sheet:
path = "C://ECOS//Code//Test//data.xlsx"
sheets = getSheets(loadWorkbook(path))
removeSheet(loadWorkbook(path), sheetName = names(sheets[2]))
Really appreciate helps. Thank you!
You need to save the workbook in the same file (or different if you prefer).
wb = loadWorkbook(path)
removeSheet(wb, sheetName = names(sheets[2]))
saveWorkbook(wb, path)
I get the corruption error when I try to open the Excel workbook created in R.
I tried with both .xlsx and .xls extensions but neither worked!
The code that I used for doing all this is:
wb <- loadWorkbook("RCreated.xls", create = TRUE);
saveWorkbook(wb)
createSheet(wb, name = "First")
HELP!
Create the sheet BEFORE saving the workbook.
I can open an excel workbook from within R using:
shell.exec("abc.xlsx")
but how can I open the workbook at a specific worksheet using shell exec?
Thank you for your help
This does not use shell.exec but does open the specified sheet:
# start Excel
library(RDCOMClient)
xl <- COMCreate("Excel.Application")
xl[["Visible"]] <- TRUE
# activate second sheet of indicated xlsx file
file <- normalizePath("abc.xlsx")
sheetNo <- 2
xl[["Workbooks"]]$Open(file)$Sheets(sheetNo)$Activate()
To specify sheet name instead of number replace last two lines with:
sheetName <- "Sheet2"
xl[["Workbooks"]]$Open(file)$Sheets(sheetName)$Activate()
I am trying to change the page setting of a xlsx file so that it will be printed as landscape, but not portrait, I tried the following:
library(xlsx)
wb <- createWorkbook()
sheet <- createSheet(wb, "Sheet1")
ps <- printSetup(sheet, landscape=TRUE, copies=3)
This is OK if I create a new Excel workbook, but I can't use it when I am using the loadWorkbook function to load an xls file. I wonder why.
Update: I am working on a xls file but not xlsx file and found the answer below cannot solve my problem, any further suggestions? Thanks.
I can get it to work by using getSheets to select the sheet to set the print area for:
wb <- loadWorkbook("test.xlsx")
printSetup(getSheets(wb)[[1]], landscape=TRUE, copies=3)
saveWorkbook(wb,"test.xlsx")