R/make_excel_file.R

#'
#' Making an excel file with the formatting we want
#'
#' @description this function accepts parameters to
#' build a custom report for users. This is typically used within
#' an application for users to easily manipulate data the way
#' they wish
#'
#' @param df The dataframe to be made into an excel file
#' @param username The session user's name
#' @param rpt_title The title of the report
#' @param rpt_subtitle The subtitle of the report
#' @param sheet_name The name of the sheet in the xlsx export
#' @param colstyles The styles for the columns in the xlsx export
#' @param colnames The name of the columns in the xlsx report
#' @param file The name of the file to be exported to user
#'
#'
# make_excel_file = function(
#   df,
#   username,
#   rpt_title,
#   rpt_subtitle,
#   sheet_name,
#   colstyles,
#   colnames,
#   file){
#
#
#   xlsx_setBorder <- function(sheet,dfrow,dfcol,startrow,startcol){
#     tbl_block <- CellBlock(sheet,startRow = startrow,startColumn = startcol,create = F,noRows = dfrow + 1,noColumns = dfcol)
#     # set borders to THICK one by one
#     CB.setBorder(tbl_block,rowIndex = c(1:(dfrow+1)),colIndex = 1,
#                  border = Border(position = "LEFT",pen = "BORDER_THICK"))
#     CB.setBorder(tbl_block,rowIndex = c(1:(dfrow+1)),colIndex = dfcol,
#                  border = Border(position = "RIGHT",pen = "BORDER_THICK"))
#     CB.setBorder(tbl_block,rowIndex = 1,colIndex = c(1:dfcol),
#                  border = Border(position = "TOP",pen = "BORDER_THICK"))
#     CB.setBorder(tbl_block,rowIndex = dfrow+1,colIndex = c(1:dfcol),
#                  border = Border(position = "BOTTOM",pen = "BORDER_THICK"))
#   }
#
#   xlsx_addText <- function(sheet, row_ind, col_ind, text, style){
#     row  <- createRow(sheet,rowIndex=row_ind)
#     cell <- createCell(row, colIndex=col_ind)
#     setCellValue(cell[[1,1]], text)
#     setCellStyle(cell[[1,1]], style)
#   }
#
#   underscore_to_upper = function(nm){
#     out = gsub("_",replacement = " ",x = nm)
#     return(toupper(trimws(out)))
#   }
#
#   if(missing(username) || is.null(username)){
#     username = "Anonymous User"
#   }
#
#   # get the store name for titles
#   report_date = format(Sys.time(),'%d %b, %Y at %T %Z')
#   title_date = paste0("Report Accessed on: ",report_date)
#   username_txt = paste0("Accessed by: ",username)
#   #  date_range_txt = paste0("Orders from ",format(date_range[1],'%d %b, %Y'),' through ',format(date_range[2],'%d %b, %Y'))
#
#   # create the workbook
#   file.out = createWorkbook(type = "xlsx")
#   # create the worksheet
#   sheet_ = createSheet(file.out,sheet_name)
#
#   # register the styles
#   STY_title       <- CellStyle(file.out) + Font(file.out,heightInPoints = 16,isBold = T,name = "Calibri")
#   STY_notes       <- CellStyle(file.out) + Font(file.out,heightInPoints = 9,isBold = F,name = "Calibri")
#   STY_daterange   <- CellStyle(file.out) + Font(file.out,heightInPoints = 12,isBold = T,name = "Calibri")
#   STY_tabletitle  <- CellStyle(file.out) + Font(file.out,heightInPoints = 12,isBold = T,name = "Calibri")
#   STY_tableheader <- CellStyle(file.out) + Font(file.out,heightInPoints = 11,isBold = T,name = "Calibri") + Fill(foregroundColor = "grey80",backgroundColor = "grey80")
#   STY_number      <- CellStyle(file.out) + DataFormat("#,##0") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri")
#   STY_decimal     <- CellStyle(file.out) + DataFormat("0.00") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri")
#   STY_pct         <- CellStyle(file.out) + DataFormat("0.0%") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri")
#   STY_dollar      <- CellStyle(file.out) + DataFormat("$#,##0") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri")
#   STY_text        <- CellStyle(file.out) + DataFormat("@") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri")
#   STY_date        <- CellStyle(file.out) + DataFormat("yyyy-mm-dd;@") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri")
#   STY_text_ttl    <- CellStyle(file.out) + DataFormat("@") + Font(file.out,heightInPoints = 11,isBold = F,name = "Calibri") + Fill(foregroundColor = "grey60",backgroundColor = "grey60")
#   STY_number_ttl  <- CellStyle(file.out) + DataFormat("#,##0") + Font(file.out,heightInPoints = 13,isBold = T,name = "Calibri") + Fill(foregroundColor = "grey60",backgroundColor = "grey60")
#   STY_pct_ttl     <- CellStyle(file.out) + DataFormat("0.0%") + Font(file.out,heightInPoints = 11,isBold = T,name = "Calibri") + Fill(foregroundColor = "grey60",backgroundColor = "grey60")
#
#   styles_match = list(
#     count = STY_number,
#     decimal = STY_decimal,
#     percent = STY_pct,
#     dollar = STY_dollar,
#     text = STY_text,
#     date = STY_date
#   )
#
#   # row incrementer for tracking rows
#   row_inc = 1
#
#   ## ADD TITLE
#   xlsx_addText(sheet = sheet_,
#                row_ind = row_inc,
#                col_ind = 1,
#                text = rpt_title,
#                style = STY_title)
#   row_inc <- row_inc + 1
#
#   ## DATE RANGE
#   xlsx_addText(sheet = sheet_,
#                row_ind = row_inc,
#                col_ind = 1,
#                text = rpt_subtitle,
#                style = STY_daterange)
#   row_inc <- row_inc + 2
#
#   ## ACCESS NOTES
#   xlsx_addText(sheet = sheet_,
#                row_ind = row_inc,
#                col_ind = 1,
#                text = title_date,
#                style = STY_notes)
#   row_inc <- row_inc + 1
#   xlsx_addText(sheet = sheet_,
#                row_ind = row_inc,
#                col_ind = 1,
#                text = username_txt,
#                style = STY_notes)
#   row_inc <- row_inc + 2
#
#   col_styles = unname(styles_match[match(colstyles,table = names(styles_match),nomatch = 5L)])
#   names(col_styles) <- seq(1,length(col_styles),1)
#
#   # add data
#   names(df) = colnames
#   addDataFrame(df,
#                sheet_,
#                startRow=row_inc,
#                startColumn=2,
#                row.names = F,
#                colStyle = col_styles,
#                colnamesStyle = STY_tableheader)
#
#   # set border
#   xlsx_setBorder(sheet = sheet_,
#                  dfrow = nrow(df),
#                  dfcol = ncol(df),
#                  startrow = row_inc,
#                  startcol = 2)
#
#   # set col widths
#   setColumnWidth(sheet_, colIndex=1, colWidth=3.00)
#   setColumnWidth(sheet_, colIndex=1+c(1:length(col_styles)), colWidth=20.00)
#   setColumnWidth(sheet_, colIndex=14, colWidth=25.00)
#
#   saveWorkbook(file.out,file = file)
#
#   invisible(TRUE)
#
# }
HarryRosen/hrimodules documentation built on Jan. 11, 2022, 12:36 a.m.