File: StyleClass.R

package info (click to toggle)
r-cran-openxlsx 4.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,328 kB
  • sloc: cpp: 1,867; makefile: 2
file content (293 lines) | stat: -rw-r--r-- 7,664 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293



#' @include class_definitions.R


Style$methods(initialize = function() {
  fontName <<- NULL
  fontColour <<- NULL
  fontSize <<- NULL
  fontFamily <<- NULL
  fontScheme <<- NULL
  fontDecoration <<- NULL

  borderTop <<- NULL
  borderLeft <<- NULL
  borderRight <<- NULL
  borderBottom <<- NULL
  borderTopColour <<- NULL
  borderLeftColour <<- NULL
  borderRightColour <<- NULL
  borderBottomColour <<- NULL
  borderDiagonal <<- NULL
  borderDiagonalColour <<- NULL
  borderDiagonalUp <<- FALSE
  borderDiagonalDown <<- FALSE

  halign <<- NULL
  valign <<- NULL
  indent <<- NULL
  textRotation <<- NULL
  numFmt <<- NULL
  fill <<- NULL
  wrapText <<- NULL
  hidden <<- NULL
  locked <<- NULL
  xfId <<- NULL
})



mergeStyle <- function(oldStyle, newStyle) {

  ## This function is used to merge an existing cell style with a new style to create a stacked style.
  oldStyle <- oldStyle$copy()

  if (!is.null(newStyle$fontName)) {
    oldStyle$fontName <- newStyle$fontName
  }

  if (!is.null(newStyle$fontColour)) {
    oldStyle$fontColour <- newStyle$fontColour
  }

  if (!is.null(newStyle$fontSize)) {
    oldStyle$fontSize <- newStyle$fontSize
  }

  if (!is.null(newStyle$fontFamily)) {
    oldStyle$fontFamily <- newStyle$fontFamily
  }

  if (!is.null(newStyle$fontScheme)) {
    oldStyle$fontScheme <- newStyle$fontScheme
  }

  if (length(newStyle$fontDecoration) > 0) {
    if (length(oldStyle$fontDecoration) == 0) {
      oldStyle$fontDecoration <- newStyle$fontDecoration
    } else {
      oldStyle$fontDecoration <- c(oldStyle$fontDecoration, newStyle$fontDecoration)
    }
  }


  ## borders
  if (!is.null(newStyle$borderTop)) {
    oldStyle$borderTop <- newStyle$borderTop
  }

  if (!is.null(newStyle$borderLeft)) {
    oldStyle$borderLeft <- newStyle$borderLeft
  }

  if (!is.null(newStyle$borderRight)) {
    oldStyle$borderRight <- newStyle$borderRight
  }

  if (!is.null(newStyle$borderBottom)) {
    oldStyle$borderBottom <- newStyle$borderBottom
  }

  if (!is.null(newStyle$borderDiagonal)) {
    oldStyle$borderDiagonal <- newStyle$borderDiagonal
  }

  oldStyle$borderDiagonalUp <- newStyle$borderDiagonalUp
  oldStyle$borderDiagonalDown <- newStyle$borderDiagonalDown


  if (!is.null(newStyle$borderTopColour)) {
    oldStyle$borderTopColour <- newStyle$borderTopColour
  }

  if (!is.null(newStyle$borderLeftColour)) {
    oldStyle$borderLeftColour <- newStyle$borderLeftColour
  }

  if (!is.null(newStyle$borderRightColour)) {
    oldStyle$borderRightColour <- newStyle$borderRightColour
  }

  if (!is.null(newStyle$borderBottomColour)) {
    oldStyle$borderBottomColour <- newStyle$borderBottomColour
  }



  ## other
  if (!is.null(newStyle$halign)) {
    oldStyle$halign <- newStyle$halign
  }

  if (!is.null(newStyle$valign)) {
    oldStyle$valign <- newStyle$valign
  }

  if (!is.null(newStyle$indent)) {
    oldStyle$indent <- newStyle$indent
  }

  if (!is.null(newStyle$textRotation)) {
    oldStyle$textRotation <- newStyle$textRotation
  }

  if (!is.null(newStyle$numFmt)) {
    oldStyle$numFmt <- newStyle$numFmt
  }

  if (!is.null(newStyle$fill)) {
    oldStyle$fill <- newStyle$fill
  }

  if (!is.null(newStyle$wrapText)) {
    oldStyle$wrapText <- newStyle$wrapText
  }

  if (!is.null(newStyle$locked)) {
    oldStyle$locked <- newStyle$locked
  }

  if (!is.null(newStyle$hidden)) {
    oldStyle$hidden <- newStyle$hidden
  }

  if (!is.null(newStyle$xfId)) {
    oldStyle$xfId <- newStyle$xfId
  }

  return(oldStyle)
}





Style$methods(show = function(print = TRUE) {
  numFmtMapping <- list(
    list("numFmtId" = 0),
    list("numFmtId" = 2),
    list("numFmtId" = 164),
    list("numFmtId" = 44),
    list("numFmtId" = 14),
    list("numFmtId" = 167),
    list("numFmtId" = 10),
    list("numFmtId" = 11),
    list("numFmtId" = 49)
  )

  validNumFmt <- c("GENERAL", "NUMBER", "CURRENCY", "ACCOUNTING", "DATE", "TIME", "PERCENTAGE", "SCIENTIFIC", "TEXT")

  if (!is.null(numFmt)) {
    if (as.integer(numFmt$numFmtId) %in% unlist(numFmtMapping)) {
      numFmtStr <- validNumFmt[unlist(numFmtMapping) == as.integer(numFmt$numFmtId)]
    } else {
      numFmtStr <- sprintf('"%s"', numFmt$formatCode)
    }
  } else {
    numFmtStr <- "GENERAL"
  }

  borders <- c(sprintf("Top: %s", borderTop), sprintf("Bottom: %s", borderBottom), sprintf("Left: %s", borderLeft), sprintf("Right: %s", borderRight))
  borderColours <- gsub("^FF", "#", c(borderTopColour, borderBottomColour, borderLeftColour, borderRightColour))

  fgFill <- fill$fillFg
  bgFill <- fill$fillBg

  styleShow <- "A custom cell style. \n\n"

  styleShow <- append(styleShow, sprintf("Cell formatting: %s \n", numFmtStr)) ## numFmt
  styleShow <- append(styleShow, sprintf("Font name: %s \n", fontName[[1]])) ## Font name
  styleShow <- append(styleShow, sprintf("Font size: %s \n", fontSize[[1]])) ## Font size
  styleShow <- append(styleShow, sprintf("Font colour: %s \n", gsub("^FF", "#", fontColour[[1]]))) ## Font colour

  ## Font decoration
  if (length(fontDecoration) > 0) {
    styleShow <- append(styleShow, sprintf("Font decoration: %s \n", paste(fontDecoration, collapse = ", ")))
  }

  if (length(borders) > 0) {
    styleShow <- append(styleShow, sprintf("Cell borders: %s \n", paste(borders, collapse = ", "))) ## Cell borders
    styleShow <- append(styleShow, sprintf("Cell border colours: %s \n", paste(borderColours, collapse = ", "))) ## Cell borders
  }

  if (!is.null(halign)) {
    styleShow <- append(styleShow, sprintf("Cell horz. align: %s \n", halign))
  } ## Cell horizontal alignment

  if (!is.null(valign)) {
    styleShow <- append(styleShow, sprintf("Cell vert. align: %s \n", valign))
  } ## Cell vertical alignment

  if (!is.null(indent)) {
    styleShow <- append(styleShow, sprintf("Cell indent: %s \n", indent))
  } ## Cell indent

  if (!is.null(textRotation)) {
    styleShow <- append(styleShow, sprintf("Cell text rotation: %s \n", textRotation))
  } ## Cell text rotation

  ## Cell fill colour
  if (length(fgFill) > 0) {
    styleShow <- append(styleShow, sprintf("Cell fill foreground: %s \n", paste(paste0(names(fgFill), ": ", sub("^FF", "#", fgFill)), collapse = ", ")))
  }

  if (length(bgFill) > 0) {
    styleShow <- append(styleShow, sprintf("Cell fill background: %s \n", paste(paste0(names(bgFill), ": ", sub("^FF", "#", bgFill)), collapse = ", ")))
  }

  if (!is.null(locked)) {
    styleShow <- append(styleShow, sprintf("Cell protection: %s \n", locked))
  } ## Cell protection
  if (!is.null(hidden)) {
    styleShow <- append(styleShow, sprintf("Cell formula hidden: %s \n", hidden))
  } ## Cell formula hidden

  styleShow <- append(styleShow, sprintf("wraptext: %s", wrapText)) ## wrap text

  styleShow <- c(styleShow, "\n\n")

  if (print) {
    cat(styleShow)
  }

  return(invisible(styleShow))
})




Style$methods(as.list = function() {
  l <- list(
    "fontName" = fontName,
    "fontColour" = fontColour,
    "fontSize" = fontSize,
    "fontFamily" = fontFamily,
    "fontScheme" = fontScheme,
    "fontDecoration" = fontDecoration,

    "borderTop" = borderTop,
    "borderLeft" = borderLeft,
    "borderRight" = borderRight,
    "borderBottom" = borderBottom,
    "borderTopColour" = borderTopColour,
    "borderLeftColour" = borderLeftColour,
    "borderRightColour" = borderRightColour,
    "borderBottomColour" = borderBottomColour,

    "halign" = halign,
    "valign" = valign,
    "indent" = indent,
    "textRotation" = textRotation,
    "numFmt" = numFmt,
    "fillFg" = fill$fillFg,
    "fillBg" = fill$fillBg,
    "wrapText" = wrapText,
    "locked" = locked,
    "hidden" = hidden,
    "xfId" = xfId
  )

  l[sapply(l, length) > 0]
})