Send data to multiple excel sheets using arrays

Hi All,

I have to read data from the Source table (excel file) and do some calculations on the data and send the data to multiple sheets in an excel file.

I was able to do it using FOR loop, but, I was asked to include an Array where the operation will work only on the sheets specified in the array.

Can anyone please help me out with how to use array in this case?

Below is the code and sample source data, I was able to read the data and send the data related to each ID to multiple sheets using FOR loop,

Source Data in excel file

Id Ename Sal
101 John 2000
102 Max 3000
103 Ted 1000

Code
library(tidyverse)
library(readxl)
library(openxlsx)
readxl_example()

df <- read_excel("Employee.xlsx")
df <- df %>%
mutate(new_column = Sal*10)

output <- split(df, df$Id)

wb<- createWorkbook()

for (i in 1:length(output)){
addWorksheet(wb, sheetName = names(output[i]))
writeData (wb, sheet= names(output[i]), x = output[[i]])
}

saveWorkbook(wb,"test.xlsx", overwrite = TRUE)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.