Skip to content

Commit

Permalink
start on adding options back in (#20)
Browse files Browse the repository at this point in the history
I'm sure I've missed quite a bit
some updates to how functions handle/check defaults
  • Loading branch information
jmbarbone committed Jan 29, 2022
1 parent f5d31cc commit 993d8e7
Show file tree
Hide file tree
Showing 21 changed files with 381 additions and 164 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export(merge_fills)
export(merge_fonts)
export(merge_numFmts)
export(modifyBaseFont)
export(op.openxlsx)
export(openXL)
export(pageBreak)
export(pageSetup)
Expand Down
2 changes: 1 addition & 1 deletion R/class-chart-sheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ wbChartSheet <- R6::R6Class(
#' @return The `wbChartSheet` object
initialize = function(
tabSelected = FALSE,
tabColour = character(),
tabColour = getOption("openxlsx.tabColour"),
zoom = 100
) {
if (length(tabColour)) {
Expand Down
15 changes: 13 additions & 2 deletions R/class-comment.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ wbComment <- R6::R6Class(
#' @param width Width of the comment in ... units
#' @param height Height of comment in ... units
#' @return a `wbComment` object
initialize = function(text, author, style, visible = TRUE, width = 2, height = 4) {
initialize = function(
text,
author = get_creator(),
style,
visible = TRUE,
width = 2,
height = 4
) {
# TODO this needs the validations that the comment wrappers have
self$text <- text
self$author <- author
Expand Down Expand Up @@ -270,6 +277,10 @@ removeComment <- function(wb, sheet, cols, rows, gridExpand = TRUE) {
wb$comments[[sheet]] <- wb$comments[[sheet]][toKeep]
}

wb_comment <- function(text = character(), author = character(), style = wb_style()) {
wb_comment <- function(
text = character(),
author = get_creator(),
style = wb_style()
) {
wbComment$new(text = text, author = author, style = style)
}
18 changes: 8 additions & 10 deletions R/class-style.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ wbStyle <- R6::R6Class(
fontName = NULL,
fontSize = NULL,
fontColour = "none",
numFmt = "GENERAL",
numFmt = getOption("openxlsx.numFmt"),
# numberFormat = c("GENERAL", "NUMBER", "CURRENCY", "ACCOUNTING", "DATE", "LONGDATE", "TIME", "PERCENTAGE", "SCIENTIFIC", "TEXT", "3", "4", "COMMA"),
# customFormat = NULL, # this could be used to overwrite number format, and allow for easier match.arg()
# if any border == none, sets to NULL
# TODO wb_border_spec() would be better
border = c("none", "top", "bottom", "left", "right", "all"),
borderColour = getOption("openxlsx.borderColour", "black"),
border = getOption("openxlsx.border"),
borderColour = getOption("openxlsx.borderColour"),
# should borderStyle default to "none"?
borderStyle = getOption("openxlsx.borderStyle", "thin"),
borderStyle = getOption("openxlsx.borderStyle"),
bgFill = "none",
fgFill = "none",
halign = c("left", "right", "center"),
Expand Down Expand Up @@ -160,11 +160,7 @@ wbStyle <- R6::R6Class(
self$textRotation <- validate_text_rotation(textRotation)

# borders ----
private$set_borders(
match.arg(border, several.ok = TRUE),
colour = borderColour,
style = borderStyle
)
private$set_borders(border, colour = borderColour, style = borderStyle)

# others ----
self$indent <- isTRUE(indent)
Expand All @@ -183,7 +179,7 @@ wbStyle <- R6::R6Class(
self$borderDiagonalDown <- FALSE
self$xfId <- NULL

invisible(self)
self
},

# TODO as.list() to to_list() ?
Expand Down Expand Up @@ -333,6 +329,8 @@ wbStyle <- R6::R6Class(

private = list(
set_borders = function(border, colour, style) {
border <- match.arg(border, c("none", "top", "bottom", "left", "right", "all"), several.ok = TRUE)

if (any(border == "none")) {
# default will override
self$borderLeft <- NULL
Expand Down
35 changes: 19 additions & 16 deletions R/class-workbook-wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -294,22 +294,25 @@ sheets <- function(wb) {
#' \dontrun{
#' saveWorkbook(wb, "addWorksheetExample.xlsx", overwrite = TRUE)
#' }
addWorksheet <- function(wb, sheetName,
gridLines = TRUE,
tabColour = NULL,
zoom = 100,
header = NULL,
footer = NULL,
evenHeader = NULL,
evenFooter = NULL,
firstHeader = NULL,
firstFooter = NULL,
visible = TRUE,
hasDrawing = FALSE,
paperSize = getOption("openxlsx.paperSize", default = 9),
orientation = getOption("openxlsx.orientation", default = "portrait"),
vdpi = getOption("openxlsx.vdpi", default = getOption("openxlsx.dpi", default = 300)),
hdpi = getOption("openxlsx.hdpi", default = getOption("openxlsx.dpi", default = 300))) {
addWorksheet <- function(
wb,
sheetName,
gridLines = getOption("openxlsx.gridLines"),
tabColour = getOption("openxlsx.tabColour"),
zoom = 100,
header = getOption("openxlsx.header"),
footer = getOption("openxlsx.footer"),
evenHeader = getOption("openxlsx.evenHeader"),
evenFooter = getOption("openxlsx.evenFooter"),
firstHeader = getOption("openxlsx.firstHeader"),
firstFooter = getOption("openxlsx.firstFooter"),
visible = TRUE,
hasDrawing = FALSE,
paperSize = getOption("openxlsx.paperSize"),
orientation = getOption("openxlsx.orientation"),
vdpi = getOption("openxlsx.vdpi", getOption("openxlsx.dpi")),
hdpi = getOption("openxlsx.hdpi", getOption("openxlsx.dpi"))
) {
od <- getOption("OutDec")
options("OutDec" = ".")
on.exit(expr = options("OutDec" = od), add = TRUE)
Expand Down
53 changes: 34 additions & 19 deletions R/class-workbook.R
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,16 @@ wbWorkbook <- R6::R6Class(

#' @description
#' Creates a new `wbWorkbook` object
#' @param creator creator
#' @param creator Workbook creator. Default uses internal `get_creator()`
#' which searches for an option `openxlsx.creator` or environmental
#' variables `USER` and `USERNAME`, in this order. If none are found,
#' `""` is used instead.
#' @param title title
#' @param subject subject
#' @param category category
#' @return a `wbWorkbook` object
initialize = function(
creator = "",
creator = get_creator(),
title = NULL,
subject = NULL,
category = NULL
Expand Down Expand Up @@ -270,21 +273,22 @@ wbWorkbook <- R6::R6Class(
#' @return The `wbWorkbook` object, invisibly
addWorksheet = function(
sheetName,
showGridLines = TRUE,
tabColour = NULL,
zoom = 100,
oddHeader = NULL,
oddFooter = NULL,
evenHeader = NULL,
evenFooter = NULL,
firstHeader = NULL,
firstFooter = NULL,
visible = TRUE,
hasDrawing = FALSE,
paperSize = 9,
orientation = "portrait",
hdpi = 300,
vdpi = 300
# TODO rename to gridLines?
showGridLines = getOption("openxlsx.gridLines"),
tabColour = getOption("openxlsx.tabColour"),
zoom = 100,
oddHeader = getOption("openxlsx.oddHeader", getOption("openxlsx.header")),
oddFooter = getOption("openxlsx.oddFooter", getOption("openxlsx.footer")),
evenHeader = getOption("openxlsx.evenHeader", getOption("openxlsx.header")),
evenFooter = getOption("openxlsx.evenFooter", getOption("openxlsx.footer")),
firstHeader = getOption("openxlsx.firstHeader", getOption("openxlsx.header")),
firstFooter = getOption("openxlsx.firstFooter", getOption("openxlsx.footer")),
visible = TRUE,
hasDrawing = FALSE,
paperSize = getOption("openxlsx.paperSize"),
orientation = getOption("openxlsx.orientation"),
vdpi = getOption("openxlsx.vdpi", getOption("openxlsx.dpi")),
hdpi = getOption("openxlsx.hdpi", getOption("openxlsx.dpi"))
) {
if (!missing(sheetName)) {
if (grepl(pattern = ":", x = sheetName)) {
Expand Down Expand Up @@ -665,7 +669,11 @@ wbWorkbook <- R6::R6Class(
#' @param tabColour tabColour
#' @param zoom zoom
#' @return The `wbWorkbook` object, invisibly
addChartSheet = function(sheetName, tabColour = NULL, zoom = 100) {
addChartSheet = function(
sheetName,
tabColour = getOption("openxlsx.tabColour"),
zoom = 100
) {
# TODO private$new_sheet_index()?
newSheetIndex <- length(self$worksheets) + 1L

Expand Down Expand Up @@ -1323,7 +1331,7 @@ wbWorkbook <- R6::R6Class(
zipfile = tmpFile,
files = list.files(tmpDir, full.names = FALSE),
recurse = TRUE,
compression_level = getOption("openxlsx.compresssionevel", 6),
compression_level = getOption("openxlsx.compressionLevel"),
include_directories = FALSE,
# change the working directory for this
root = tmpDir,
Expand Down Expand Up @@ -5087,3 +5095,10 @@ wbWorkbook <- R6::R6Class(
}
)
)


# helpers -----------------------------------------------------------------

get_creator <- function() {
getOption("openxlsx.creator", Sys.getenv("USER", Sys.getenv("USERNAME")))
}
24 changes: 12 additions & 12 deletions R/class-worksheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,20 @@ wbWorksheet <- R6::R6Class(
#' @param vdpi vdpi
#' @return a `wbWorksheet` object
initialize = function(
showGridLines = TRUE,
showGridLines = getOption("openxlsx.showGridLines"),
tabSelected = FALSE,
tabColour = NULL,
tabColour = getOption("openxlsx.tabColour"),
zoom = 100,
oddHeader = NULL,
oddFooter = NULL,
evenHeader = NULL,
evenFooter = NULL,
firstHeader = NULL,
firstFooter = NULL,
paperSize = 9,
orientation = "portrait",
hdpi = 300,
vdpi = 300
oddHeader = getOption("openxlsx.oddHeader", getOption("openxlsx.header")),
oddFooter = getOption("openxlsx.oddFooter", getOption("openxlsx.footer")),
evenHeader = getOption("openxlsx.evenHeader", getOption("openxlsx.header")),
evenFooter = getOption("openxlsx.evenFooter", getOption("openxlsx.footer")),
firstHeader = getOption("openxlsx.firstHeader", getOption("openxlsx.header")),
firstFooter = getOption("openxlsx.firstFooter", getOption("openxlsx.footer")),
paperSize = getOption("openxlsx.paperSize"),
orientation = getOption("openxlsx.orientation"),
hdpi = getOption("openxlsx.hdpi", getOption("opnexlsx.dpi")),
vdpi = getOption("openxlsx.vdpi", getOption("opnexlsx.dpi"))
) {
if (!is.null(tabColour)) {
tabColour <- sprintf('<sheetPr><tabColor rgb="%s"/></sheetPr>', tabColour)
Expand Down
3 changes: 0 additions & 3 deletions R/onUnload.R

This file was deleted.

92 changes: 92 additions & 0 deletions R/options.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#' `openxlsx` options
#'
#' A list of standard options from `openxlsx`
#'
#' @description A named `list` of options that are set during `.onAttach()` and
#' are available for global controls of other functions.
#'
#' \details{
#' \item{`bandedCols`}{Table style colun bands}
#' \item{`bandedRows`}{Table style row bands}
#' \item{`firstColumn`}{Table style, first column}
#' \item{`lastColumn`}{Table style, last column}
#' \item{`withFilter`}{Include filters on tables}
#' \item{`gridLines`}{Show grid lines}
#' \item{`tableStyle`}{Table style}
#'
#' \item{`borders`}{Cell borders (e.g., `"none"`, `"sorrounding"`, `"all"`)}
#' \item{`borderColour`}{Style border colour}
#' \item{`bordersStyle`}{Cell border style}
#'
#' \item{`numFormat`}{Number format}
#' \item{`dateFormat`}{Date format style}
#' \item{`datetimeFormat`}{Date time format style}
#'
#' \item{`header`}{Document headers; prioritized last}
#' \item{`footer`}{Document footers; prioritized last}
#' \item{`firstHeader`}{Document header; first page only; prioritized first}
#' \item{`firstFooter`}{Document footer; first page only; prioritized first}
#' \item{`evenFooter`}{Document footers; even pages}
#' \item{`evenHeader`}{Document headers; even pages}
#' \item{`oddFooter`}{Document footers; odd pages}
#' \item{`oddHeader`}{Document headers; odd pages}
#'
#' \item{`dpi`}{Dots per inch (see `hdpi` and `vdpi` for other controls)}
#' \item{`hdpi`}{Horizontal dots per inch; prioritized over `dpi`}
#' \item{`vdpi`}{Vertical dots per inch; prioritized over `dpi`}
#'
#' \item{`creator`}{Sets workbook creator (used in internal `get_creator()`; see [wbWorkbook] for more details)}
#' \item{`compressionLevel`}{Passed to [zip::zip()]}
#' \item{`na`}{String to convert to `NA`}
#' \item{`keepNA`}{Convert `NA` to `"#N/A"` (or string in `na`)}
#'
#' \item{`orientation`}{paper orientation}
#' \item{`paperSize`}{paper size, in inches}
#' \item{`tabColour`}{tab colour}
#' }
#'
#' @export
op.openxlsx <- list(
openxlsx.bandedCols = FALSE,
openxlsx.bandedRows = TRUE,
openxlsx.firstColumn = FALSE,
openxlsx.lastColumn = FALSE,
openxlsx.withFilter = NULL,
openxlsx.gridLines = NA,
openxlsx.tableStyle = "TableStyleLight9",

openxlsx.borders = "none",
openxlsx.borderColour = "black",
openxlsx.borderStyle = "thin",

openxlsx.numFmt = "GENERAL",
openxlsx.dateFormat = "mm/dd/yyyy",
openxlsx.datetimeFormat = "yyyy-mm-dd hh:mm:ss",

openxlsx.header = NULL, # consider list(left = "", center = "", right = "")
openxlsx.footer = NULL, # consider list(left = "", center = "", right = "")
# allows NA to turn off?
openxlsx.firstFooter = NULL,
openxlsx.firstHeader = NULL,
openxlsx.evenFooter = NULL, # consider list(left = "", center = "", right = "")
openxlsx.evenHeader = NULL, # consider list(left = "", center = "", right = "")
openxlsx.oddFooter = NULL,
openxlsx.oddHeader = NULL,

# openxlsx.headerStyle = NULL, # shouldn't this be a style object?

openxlsx.dpi = 300,
openxlsx.hdpi = 300,
openxlsx.vdpi = 300,

openxlsx.creator = NULL,
openxlsx.compressionLevel = 9,
openxlsx.na = NULL, # default to "#N/A" ?
openxlsx.keepNA = FALSE,

# openxlsx.maxWidth = 250,
# openxlsx.minWidth = 3,
openxlsx.orientation = "portrait",
openxlsx.paperSize = 9,
openxlsx.tabColour = NULL
)

0 comments on commit 993d8e7

Please sign in to comment.