Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

165 options #174

Merged
merged 6 commits into from
Apr 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ Collate:
'openxlsxCoerce.R'
'readWorkbook.R'
'sheet_data_class.R'
'utils.R'
'workbook_column_widths.R'
'workbook_read_workbook.R'
'workbook_write_data.R'
Expand All @@ -77,3 +78,4 @@ Collate:
'writeData.R'
'writeDataTable.R'
'writexlsx.R'
'zzz.R'
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export(loadWorkbook)
export(makeHyperlinkString)
export(mergeCells)
export(modifyBaseFont)
export(op.openxlsx)
export(openXL)
export(openxlsx_getOp)
export(openxlsx_setOp)
export(pageBreak)
export(pageSetup)
export(protectWorkbook)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* Added ability to change positioning of summary columns and rows.
* These can be set with the `summaryCol` and `summaryRow` arguments in `pageSetup()`.
* activeSheet allows to set and get the active (displayed) sheet of a worbook.
* Adds new global options for workbook formatting ([#165](https://github.com/ycphs/openxlsx/issues/165); see `?op.openxlsx`)

# openxlsx 4.2.3

Expand Down
65 changes: 24 additions & 41 deletions R/WorkbookClass.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#' @import stringi

Workbook$methods(
initialize = function(creator = "",
initialize = function(creator = openxlsx_getOp("creator"),
title = NULL,
subject = NULL,
category = NULL) {
Expand Down Expand Up @@ -73,8 +73,6 @@ Workbook$methods(
vml <<- list()
vml_rels <<- list()



workbook <<- genBaseWorkbook()
workbook.xml.rels <<- genBaseWorkbook.xml.rels()

Expand All @@ -84,35 +82,31 @@ Workbook$methods(
}
)







Workbook$methods(
addWorksheet = function(sheetName,
showGridLines = TRUE,
tabColour = NULL,
zoom = 100,
oddHeader = NULL,
oddFooter = NULL,
evenHeader = NULL,
evenFooter = NULL,
firstHeader = NULL,
firstFooter = NULL,
visible = TRUE,
paperSize = 9,
orientation = "portrait",
hdpi = 300,
vdpi = 300) {
addWorksheet = function(
sheetName,
showGridLines = openxlsx_getOp("showGridLines"),
tabColour = openxlsx_getOp("tabColour"),
zoom = 100,
oddHeader = openxlsx_getOp("oddHeader"),
oddFooter = openxlsx_getOp("oddFooter"),
evenHeader = openxlsx_getOp("evenHeader"),
evenFooter = openxlsx_getOp("evenFooter"),
firstHeader = openxlsx_getOp("firstHeader"),
firstFooter = openxlsx_getOp("firstFooter"),
visible = TRUE,
paperSize = openxlsx_getOp("paperSize", 9),
orientation = openxlsx_getOp("orientation", "portrait"),
hdpi = openxlsx_getOp("hdpi", 300),
vdpi = openxlsx_getOp("vdpi", 300)
) {
if (!missing(sheetName)) {
if (grepl(pattern = ":", x = sheetName)) {
stop("colon not allowed in sheet names in Excel")
}
}
newSheetIndex <- length(worksheets) + 1L

if (newSheetIndex > 1) {
sheetId <-
max(as.integer(regmatches(
Expand All @@ -127,19 +121,15 @@ Workbook$methods(

## fix visible value
visible <- tolower(visible)

if (visible == "true") {
visible <- "visible"
}

if (visible == "false") {
} else if (visible == "false") {
visible <- "hidden"
}

if (visible == "veryhidden") {
} else if (visible == "veryhidden") {
visible <- "veryHidden"
}



## Add sheet to workbook.xml
workbook$sheets <<-
c(
Expand Down Expand Up @@ -226,7 +216,6 @@ Workbook$methods(
}
)


Workbook$methods(
cloneWorksheet = function(sheetName, clonedSheet) {
clonedSheet <- validateSheet(clonedSheet)
Expand Down Expand Up @@ -474,7 +463,6 @@ Workbook$methods(
}
)


Workbook$methods(
addChartSheet = function(sheetName,
tabColour = NULL,
Expand Down Expand Up @@ -534,8 +522,6 @@ Workbook$methods(
)
)



## add a drawing.xml for the worksheet
Content_Types <<-
c(
Expand Down Expand Up @@ -637,9 +623,6 @@ Workbook$methods(
})
}




## will always have drawings
xlworksheetsDir <- file.path(tmpDir, "xl", "worksheets")
dir.create(path = xlworksheetsDir, recursive = TRUE)
Expand Down Expand Up @@ -735,7 +718,6 @@ Workbook$methods(
}



if (length(embeddings) > 0) {
embeddingsDir <- file.path(tmpDir, "xl", "embeddings")
dir.create(path = embeddingsDir, recursive = TRUE)
Expand Down Expand Up @@ -3862,6 +3844,7 @@ Workbook$methods(
# cat(strs)


# openxlsxFontSizeLookupTable ---------------------------------------------

## Character width lookup table
openxlsxFontSizeLookupTable <-
Expand Down
94 changes: 59 additions & 35 deletions R/borderFunctions.R
Original file line number Diff line number Diff line change
@@ -1,50 +1,58 @@



genBaseColStyle <- function(cc) {
colStyle <- createStyle()
specialFormat <- TRUE

if ("date" %in% cc) {
colStyle <- createStyle("numFmt" = "date")
colStyle <- createStyle(numFmt = "date")
} else if (any(c("posixlt", "posixct", "posixt") %in% cc)) {
colStyle <- createStyle("numFmt" = "longdate")
colStyle <- createStyle(numFmt = "longdate")
} else if ("currency" %in% cc) {
colStyle$numFmt <- list("numFmtId" = "164", "formatCode" = "&quot;$&quot;#,##0.00")
colStyle$numFmt <- list(numFmtId = "164", "formatCode" = "&quot;$&quot;#,##0.00")
} else if ("accounting" %in% cc) {
colStyle$numFmt <- list("numFmtId" = "44")
colStyle$numFmt <- list(numFmtIs = "44")
} else if ("hyperlink" %in% cc) {
colStyle$fontColour <- list("theme" = "10")
colStyle$fontColour <- list(theme = "10")
} else if ("percentage" %in% cc) {
colStyle$numFmt <- list("numFmtId" = "10")
colStyle$numFmt <- list(numFmtIs = "10")
} else if ("scientific" %in% cc) {
colStyle$numFmt <- list("numFmtId" = "11")
} else if ("3" %in% cc | "comma" %in% cc) {
colStyle$numFmt <- list("numFmtId" = "3")
} else if ("numeric" %in% cc & !grepl("[^0\\.,#\\$\\* %]", getOption("openxlsx.numFmt", "GENERAL"))) {
colStyle$numFmt <- list("numFmtId" = 9999, "formatCode" = getOption("openxlsx.numFmt"))
colStyle$numFmt <- list(numFmtId = "11")
} else if (any(c("3", "comma") %in% cc)) {
colStyle$numFmt <- list(numFmtId = "3")
} else if ("numeric" %in% cc & !grepl("[^0\\.,#\\$\\* %]", openxlsx_getOp("numFmt"))) {
colStyle$numFmt <- list(numFmtId = 9999, formatCode = openxlsx_getOp("numFmt"))
} else {
colStyle$numFmt <- list(numFmtId = "0")
specialFormat <- FALSE
}

list(
"style" = colStyle,
"specialFormat" = specialFormat
style = colStyle,
specialFormat = specialFormat
)
}



Workbook$methods(surroundingBorders = function(colClasses, sheet, startRow, startCol, nRow, nCol, borderColour, borderStyle, borderType) {
Workbook$methods(surroundingBorders = function(
colClasses,
sheet,
startRow,
startCol,
nRow,
nCol,
borderColour,
borderStyle,
borderType
) {
sheet <- sheet_names[[validateSheet(sheet)]]
## steps
# get column class
# get corresponding base style

for (i in 1:nCol) {
tmp <- genBaseColStyle(colClasses[[i]])

colStyle <- tmp$style
specialFormat <- tmp$specialFormat

Expand Down Expand Up @@ -340,15 +348,17 @@ Workbook$methods(surroundingBorders = function(colClasses, sheet, startRow, star
invisible(0)
})









Workbook$methods(rowBorders = function(colClasses, sheet, startRow, startCol, nRow, nCol, borderColour, borderStyle, borderType) {
Workbook$methods(rowBorders = function(
colClasses,
sheet,
startRow,
startCol,
nRow,
nCol,
borderColour,
borderStyle,
borderType
) {
sheet <- sheet_names[[validateSheet(sheet)]]
## steps
# get column class
Expand Down Expand Up @@ -422,9 +432,17 @@ Workbook$methods(rowBorders = function(colClasses, sheet, startRow, startCol, nR
})




Workbook$methods(columnBorders = function(colClasses, sheet, startRow, startCol, nRow, nCol, borderColour, borderStyle, borderType) {
Workbook$methods(columnBorders = function(
colClasses,
sheet,
startRow,
startCol,
nRow,
nCol,
borderColour,
borderStyle,
borderType
) {
sheet <- sheet_names[[validateSheet(sheet)]]
## steps
# get column class
Expand Down Expand Up @@ -514,7 +532,6 @@ Workbook$methods(columnBorders = function(colClasses, sheet, startRow, startCol,
))
}


styleObjects <<- append(styleObjects, list(
list(
"style" = sBot,
Expand All @@ -531,10 +548,17 @@ Workbook$methods(columnBorders = function(colClasses, sheet, startRow, startCol,
})





Workbook$methods(allBorders = function(colClasses, sheet, startRow, startCol, nRow, nCol, borderColour, borderStyle, borderType) {
Workbook$methods(allBorders = function(
colClasses,
sheet,
startRow,
startCol,
nRow,
nCol,
borderColour,
borderStyle,
borderType
) {
sheet <- sheet_names[[validateSheet(sheet)]]
## steps
# get column class
Expand Down
13 changes: 8 additions & 5 deletions R/class_definitions.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

# Workbook ----------------------------------------------------------------

Workbook <- setRefClass("Workbook",
fields = c(
"sheet_names" = "character",
Expand Down Expand Up @@ -57,7 +60,7 @@ Workbook <- setRefClass("Workbook",
)
)


# Style -------------------------------------------------------------------

Style <- setRefClass("Style",
fields = c(
Expand Down Expand Up @@ -93,6 +96,8 @@ Style <- setRefClass("Style",
methods = list()
)

# Sheet_Data --------------------------------------------------------------

Sheet_Data <- setRefClass("Sheet_Data",
fields = c(
"rows" = "integer",
Expand All @@ -107,8 +112,7 @@ Sheet_Data <- setRefClass("Sheet_Data",
)




# Worksheet ---------------------------------------------------------------

WorkSheet <- setRefClass("WorkSheet",
fields = c(
Expand Down Expand Up @@ -144,8 +148,7 @@ WorkSheet <- setRefClass("WorkSheet",
)
)



# ChartSheet --------------------------------------------------------------

ChartSheet <- setRefClass("ChartSheet",
fields = c(
Expand Down
Loading