这是我的代表
library(openxlsx2)
wb <- openxlsx2::wb_workbook()
openxlsx2::wb_add_worksheet(wb, "All Courses")
allLines = c("This is the first test",
"This is the second test",
"This is the third test")
openxlsx2::wb_add_data(wb, sheet = "All Courses", allLines, start_col = 1, start_row = 1)
openxlsx2::wb_save(wb, file = "C:/Temp/My xlsx2 Test.xlsx", overwrite = TRUE)
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我可靠地收到以下错误消息:
openxlsx2::wb_add_data(wb, sheet = "All Courses", allLines, start_col = 1, start_row = 1)
Error in wb$.__enclos_env__$private$get_sheet_index(sheet) :
Sheet name(s) not found: all courses
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚这是一个错误还是我做错了什么。在此先感谢您的帮助
托马斯·飞利浦
问题是,使用wb_xxx函数系列时,您必须将结果分配回对象wb,即使用wb <- openxlsx2::wb_xxx(...):
library(openxlsx2)
allLines <- c(
"This is the first test",
"This is the second test",
"This is the third test"
)
wb <- openxlsx2::wb_workbook()
wb <- openxlsx2::wb_add_worksheet(wb, "All Courses")
wb <- openxlsx2::wb_add_data(wb,
sheet = "All Courses", allLines,
start_col = 1, start_row = 1
)
openxlsx2::wb_save(wb, file = "test.xlsx", overwrite = TRUE)
Run Code Online (Sandbox Code Playgroud)
我想避免您可以使用wb$xxx如下方式调用工作簿方法:
wb <- openxlsx2::wb_workbook()
wb$add_worksheet("All Courses")
wb$add_data(
sheet = "All Courses", allLines,
start_col = 1, start_row = 1
)
wb$save(file = "test1.xlsx", overwrite = TRUE)
Run Code Online (Sandbox Code Playgroud)