rexcelopenxlsxxlconnectr-xlsx

Combining multiple worksheets from multiple excel workbooks into a single workbook via. R


Lets say I have two excel workbooks (e.g. A.xlsx and B.xlsx) that each contain multiple worksheets (e.g. "a1" and "a2" in A.xlsx; "b1", "b2", and "b3" in B.xlsx). Note that these worksheets all have their own conditional formatting, filtering, zoom, and other viewing options.

Given this setup, is it possible to generate a combined workbook via. R (e.g. C.xlsx) featuring the 5 worksheets from A.xlsx and B.xlsx (e.g. "a1", "a2", "b1", "b2", and "b3" in C.xlsx) that furthermore retains all of the original conditional formatting, filtering, zoom, and other viewing options? Thank you for your help!


Solution

  • We could read both the datasets together in map2. Get the sheet names from 'A.xlsx' and 'B.xlsx' data. Create a named vector of filepath with sheet names, loop over the vector with imap, read the sheets with read_excel, bind those together after creating a column for identification of sheetname, and combine the list output from imap to a single list with imap

    library(purrr)
    library(dplyr)
    library(readxl)
    library(openxlsx)
    
    # // replace the path/to/your - actual path
    fileA <- 'path/to/your/A.xlsx'
    fileB <- 'path/to/your/B.xlsx'
    nmA <- excel_sheets(path = fileA)
    nmB <- excel_sheets(path = fileB)
    nm1A <- setNames(rep(fileA, length(nmA)), nmA)
    nm1B <- setNames(rep(fileB, length(nmB)), nmB)
    lstC <- imap(c(nm1A, nm1B), ~ 
                  read_excel(.x, sheet = .y) %>%
                      mutate(sheetname = .y))
    names(lstC) <- c(nmA, nmB)
    write.xlsx(lstC, '/path/to/your/C.xlsx')