File: conditional_formatting.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 (708 lines) | stat: -rw-r--r-- 22,262 bytes parent folder | download
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708





#' @name conditionalFormatting
#' @aliases databar
#' @title Add conditional formatting to cells
#' @description Add conditional formatting to cells
#' @author Alexander Walker, Philipp Schauberger
#' @param wb A workbook object
#' @param sheet A name or index of a worksheet
#' @param cols Columns to apply conditional formatting to
#' @param rows Rows to apply conditional formatting to
#' @param rule The condition under which to apply the formatting. See examples.
#' @param style A style to apply to those cells that satisfy the rule. Default is createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
#' @param type Either 'expression', 'colourScale', 'databar', 'duplicates', 'beginsWith', 
#' 'endsWith', 'topN', 'bottomN', 'blanks', 'notBlanks', 'contains' or 'notContains' (case insensitive).
#' @param ... See below
#' @details See Examples.
#'
#' If type == "expression"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is an expression. Valid operators are "<", "<=", ">", ">=", "==", "!=".}
#' }
#'
#' If type == "colourScale"
#' \itemize{
#'   \item{style is a vector of colours with length 2 or 3}
#'   \item{rule can be NULL or a vector of colours of equal length to styles}
#' }
#'
#' If type == "databar"
#' \itemize{
#'   \item{style is a vector of colours with length 2 or 3}
#'   \item{rule is a numeric vector specifying the range of the databar colours. Must be equal length to style}
#'   \item{...
#'   \itemize{
#'     \item{**showvalue** If FALSE the cell value is hidden. Default TRUE.}
#'     \item{**gradient** If FALSE colour gradient is removed. Default TRUE.}
#'     \item{**border** If FALSE the border around the database is hidden. Default TRUE.}
#'      }
#'    }
#' }
#'
#' If type == "duplicates"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is ignored.}
#' }
#'
#' If type == "contains"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is the text to look for within cells}
#' }
#'
#' If type == "between"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is a numeric vector of length 2 specifying lower and upper bound (Inclusive)}
#' }
#' If type == "blanks"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is ignored.}
#' }
#'
#' If type == "notBlanks"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is ignored.}
#' }

#'
#' If type == "topN"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is ignored}
#'   \item{...
#'   \itemize{
#'     \item{**rank** numeric vector of length 1 indicating number of highest values.}
#'     \item{**percent** TRUE if you want top N percentage.}
#'      }
#'    }
#' }
#' 
#' If type == "bottomN"
#' \itemize{
#'   \item{style is a Style object. See [createStyle()]}
#'   \item{rule is ignored}
#'   \item{...
#'   \itemize{
#'     \item{**rank** numeric vector of length 1 indicating number of lowest values.}
#'     \item{**percent** TRUE if you want bottom N percentage.}
#'      }
#'    }
#' }
#' 
#' @seealso [createStyle()]
#' @export
#' @examples
#' wb <- createWorkbook()
#' addWorksheet(wb, "cellIs")
#' addWorksheet(wb, "Moving Row")
#' addWorksheet(wb, "Moving Col")
#' addWorksheet(wb, "Dependent on")
#' addWorksheet(wb, "Duplicates")
#' addWorksheet(wb, "containsText")
#' addWorksheet(wb, "notcontainsText")
#' addWorksheet(wb, "beginsWith")
#' addWorksheet(wb, "endsWith")
#' addWorksheet(wb, "colourScale", zoom = 30)
#' addWorksheet(wb, "databar")
#' addWorksheet(wb, "between")
#' addWorksheet(wb, "topN")
#' addWorksheet(wb, "bottomN")
#' addWorksheet(wb, "containsBlanks")
#' addWorksheet(wb, "notContainsBlanks")
#' addWorksheet(wb, "logical operators")
#'
#' negStyle <- createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
#' posStyle <- createStyle(fontColour = "#006100", bgFill = "#C6EFCE")
#'
#' ## rule applies to all each cell in range
#' writeData(wb, "cellIs", -5:5)
#' writeData(wb, "cellIs", LETTERS[1:11], startCol = 2)
#' conditionalFormatting(wb, "cellIs",
#'   cols = 1,
#'   rows = 1:11, rule = "!=0", style = negStyle
#' )
#' conditionalFormatting(wb, "cellIs",
#'   cols = 1,
#'   rows = 1:11, rule = "==0", style = posStyle
#' )
#'
#' ## highlight row dependent on first cell in row
#' writeData(wb, "Moving Row", -5:5)
#' writeData(wb, "Moving Row", LETTERS[1:11], startCol = 2)
#' conditionalFormatting(wb, "Moving Row",
#'   cols = 1:2,
#'   rows = 1:11, rule = "$A1<0", style = negStyle
#' )
#' conditionalFormatting(wb, "Moving Row",
#'   cols = 1:2,
#'   rows = 1:11, rule = "$A1>0", style = posStyle
#' )
#'
#' ## highlight column dependent on first cell in column
#' writeData(wb, "Moving Col", -5:5)
#' writeData(wb, "Moving Col", LETTERS[1:11], startCol = 2)
#' conditionalFormatting(wb, "Moving Col",
#'   cols = 1:2,
#'   rows = 1:11, rule = "A$1<0", style = negStyle
#' )
#' conditionalFormatting(wb, "Moving Col",
#'   cols = 1:2,
#'   rows = 1:11, rule = "A$1>0", style = posStyle
#' )
#'
#' ## highlight entire range cols X rows dependent only on cell A1
#' writeData(wb, "Dependent on", -5:5)
#' writeData(wb, "Dependent on", LETTERS[1:11], startCol = 2)
#' conditionalFormatting(wb, "Dependent on",
#'   cols = 1:2,
#'   rows = 1:11, rule = "$A$1<0", style = negStyle
#' )
#' conditionalFormatting(wb, "Dependent on",
#'   cols = 1:2,
#'   rows = 1:11, rule = "$A$1>0", style = posStyle
#' )
#'
#' ## highlight cells in column 1 based on value in column 2
#' writeData(wb, "Dependent on", data.frame(x = 1:10, y = runif(10)), startRow = 15)
#' conditionalFormatting(wb, "Dependent on",
#'   cols = 1,
#'   rows = 16:25, rule = "B16<0.5", style = negStyle
#' )
#' conditionalFormatting(wb, "Dependent on",
#'   cols = 1,
#'   rows = 16:25, rule = "B16>=0.5", style = posStyle
#' )
#'
#'
#' ## highlight duplicates using default style
#' writeData(wb, "Duplicates", sample(LETTERS[1:15], size = 10, replace = TRUE))
#' conditionalFormatting(wb, "Duplicates", cols = 1, rows = 1:10, type = "duplicates")
#'
#' ## cells containing text
#' fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
#' writeData(wb, "containsText", sapply(1:10, fn))
#' conditionalFormatting(wb, "containsText", cols = 1, rows = 1:10, type = "contains", rule = "A")
#' 
#' ## cells not containing text
#' fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
#' writeData(wb, "containsText", sapply(1:10, fn))
#' conditionalFormatting(wb, "notcontainsText", cols = 1, 
#'                      rows = 1:10, type = "notcontains", rule = "A")
#' 
#' 
#' ## cells begins with text
#' fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
#' writeData(wb, "beginsWith", sapply(1:100, fn))
#' conditionalFormatting(wb, "beginsWith", cols = 1, rows = 1:100, type = "beginsWith", rule = "A")
#' 
#' 
#' ## cells ends with text
#' fn <- function(x) paste(sample(LETTERS, 10), collapse = "-")
#' writeData(wb, "endsWith", sapply(1:100, fn))
#' conditionalFormatting(wb, "endsWith", cols = 1, rows = 1:100, type = "endsWith", rule = "A")
#'
#' ## colourscale colours cells based on cell value
#' df <- read.xlsx(system.file("extdata", "readTest.xlsx", package = "openxlsx"), sheet = 4)
#' writeData(wb, "colourScale", df, colNames = FALSE) ## write data.frame
#'
#' ## rule is a vector or colours of length 2 or 3 (any hex colour or any of colours())
#' ## If rule is NULL, min and max of cells is used. Rule must be the same length as style or NULL.
#' conditionalFormatting(wb, "colourScale",
#'   cols = 1:ncol(df), rows = 1:nrow(df),
#'   style = c("black", "white"),
#'   rule = c(0, 255),
#'   type = "colourScale"
#' )
#'
#' setColWidths(wb, "colourScale", cols = 1:ncol(df), widths = 1.07)
#' setRowHeights(wb, "colourScale", rows = 1:nrow(df), heights = 7.5)
#'
#' ## Databars
#' writeData(wb, "databar", -5:5)
#' conditionalFormatting(wb, "databar", cols = 1, rows = 1:11, type = "databar") ## Default colours
#'
#' ## Between
#' # Highlight cells in interval [-2, 2]
#' writeData(wb, "between", -5:5)
#' conditionalFormatting(wb, "between", cols = 1, rows = 1:11, type = "between", rule = c(-2, 2))
#'
#' ## Top N 
#' writeData(wb, "topN", data.frame(x = 1:10, y = rnorm(10)))
#' # Highlight top 5 values in column x
#' conditionalFormatting(wb, "topN", cols = 1, rows = 2:11, 
#'  style = posStyle, type = "topN", rank = 5)#'
#' # Highlight top 20 percentage in column y
#' conditionalFormatting(wb, "topN", cols = 2, rows = 2:11, 
#'  style = posStyle, type = "topN", rank = 20, percent = TRUE)
#'
#'## Bottom N 
#' writeData(wb, "bottomN", data.frame(x = 1:10, y = rnorm(10)))
#' # Highlight bottom 5 values in column x
#' conditionalFormatting(wb, "bottomN", cols = 1, rows = 2:11, 
#'  style = negStyle, type = "topN", rank = 5)
#' # Highlight bottom 20 percentage in column y
#' conditionalFormatting(wb, "bottomN", cols = 2, rows = 2:11, 
#'  style = negStyle, type = "topN", rank = 20, percent = TRUE)
#'  
#' ## cells containing blanks
#' sample_data <- sample(c("X", NA_character_), 10, replace = TRUE)
#' writeData(wb, "containsBlanks", sample_data)
#' conditionalFormatting(wb, "containsBlanks", cols = 1, rows = 1:10, 
#' type = "blanks", style = negStyle)
#' 
#' ## cells not containing blanks
#' sample_data <- sample(c("X", NA_character_), 10, replace = TRUE)
#' writeData(wb, "notContainsBlanks", sample_data)
#' conditionalFormatting(wb, "notContainsBlanks", cols = 1, rows = 1:10, 
#' type = "notBlanks", style = posStyle)
#'
#' ## Logical Operators
#' # You can use Excels logical Operators
#' writeData(wb, "logical operators", 1:10)
#' conditionalFormatting(wb, "logical operators",
#'   cols = 1, rows = 1:10,
#'   rule = "OR($A1=1,$A1=3,$A1=5,$A1=7)"
#' )
#' \dontrun{
#' saveWorkbook(wb, "conditionalFormattingExample.xlsx", TRUE)
#' }
#'
#'
#' #########################################################################
#' ## Databar Example
#'
#' wb <- createWorkbook()
#' addWorksheet(wb, "databar")
#'
#' ## Databars
#' writeData(wb, "databar", -5:5, startCol = 1)
#' conditionalFormatting(wb, "databar", cols = 1, rows = 1:11, type = "databar") ## Defaults
#'
#' writeData(wb, "databar", -5:5, startCol = 3)
#' conditionalFormatting(wb, "databar", cols = 3, rows = 1:11, type = "databar", border = FALSE)
#'
#' writeData(wb, "databar", -5:5, startCol = 5)
#' conditionalFormatting(wb, "databar",
#'   cols = 5, rows = 1:11,
#'   type = "databar", style = c("#a6a6a6"), showValue = FALSE
#' )
#'
#' writeData(wb, "databar", -5:5, startCol = 7)
#' conditionalFormatting(wb, "databar",
#'   cols = 7, rows = 1:11,
#'   type = "databar", style = c("#a6a6a6"), showValue = FALSE, gradient = FALSE
#' )
#'
#' writeData(wb, "databar", -5:5, startCol = 9)
#' conditionalFormatting(wb, "databar",
#'   cols = 9, rows = 1:11,
#'   type = "databar", style = c("#a6a6a6", "#a6a6a6"), showValue = FALSE, gradient = FALSE
#' )
#' \dontrun{
#' saveWorkbook(wb, file = "databarExample.xlsx", overwrite = TRUE)
#' }
#'
conditionalFormatting <-
  function(wb,
           sheet,
           cols,
           rows,
           rule = NULL,
           style = NULL,
           type = "expression",
           ...) {
    op <- get_set_options()
    on.exit(options(op), add = TRUE)

    type <- tolower(type)
    params <- list(...)

    if (type %in% c("colorscale", "colourscale")) {
      type <- "colorScale"
    } else if (type == "databar") {
      type <- "dataBar"
    } else if (type == "duplicates") {
      type <- "duplicatedValues"
    } else if (type == "contains") {
      type <- "containsText"
    } else if (type == "notcontains") {
      type <- "notContainsText"
    } else if (type == "beginswith") {
      type <- "beginsWith"
    } else if (type == "endswith") {
      type <- "endsWith"
    } else if (type == "between") {
      type <- "between"
    } else if (type == "topn") {
      type <- "topN"
    } else if (type == "bottomn") {
      type <- "bottomN"
    } else if (type == "blanks") {
      type <- "containsBlanks"
    } else if (type == "notblanks") {
      type <- "notContainsBlanks"
    }else if (type != "expression") {
      stop(
        "Invalid type argument.  Type must be one of 'expression', 'colourScale', 'databar', 'duplicates', 'beginsWith', 'endsWith', 'blanks', 'notBlanks', 'contains' or 'notContains'"
      )
    }

    ## rows and cols
    if (!is.numeric(cols)) {
      cols <- convertFromExcelRef(cols)
    }
    rows <- as.integer(rows)


    ## check valid rule
    values <- NULL
    dxfId <- NULL

    if (type == "colorScale") {
      # type == "colourScale"
      # - style is a vector of colours with length 2 or 3
      # - rule specifies the quantiles (numeric vector of length 2 or 3), if NULL min and max are used

      if (is.null(style)) {
        stop("If type == 'colourScale', style must be a vector of colours of length 2 or 3.")
      }

      if (!inherits(style, "character")) {
        stop("If type == 'colourScale', style must be a vector of colours of length 2 or 3.")
      }

      if (!length(style) %in% 2:3) {
        stop("If type == 'colourScale', style must be a vector of length 2 or 3.")
      }

      if (!is.null(rule)) {
        if (length(rule) != length(style)) {
          stop("If type == 'colourScale', rule and style must have equal lengths.")
        }
      }

      style <-
        validateColour(style, errorMsg = "Invalid colour specified in style.")

      values <- rule
      rule <- style
    } else if (type == "dataBar") {
      # type == "databar"
      # - style is a vector of colours of length 2 or 3
      # - rule specifies the quantiles (numeric vector of length 2 or 3), if NULL min and max are used

      if (is.null(style)) {
        style <- "#638EC6"
      }

      if (!inherits(style, "character")) {
        stop("If type == 'dataBar', style must be a vector of colours of length 1 or 2.")
      }

      if (!length(style) %in% 1:2) {
        stop("If type == 'dataBar', style must be a vector of length 1 or 2.")
      }

      if (!is.null(rule)) {
        if (length(rule) != length(style)) {
          stop("If type == 'dataBar', rule and style must have equal lengths.")
        }
      }


      ## Additional parameters passed by ...
      if ("showValue" %in% names(params)) {
        params$showValue <- as.integer(params$showValue)
        if (is.na(params$showValue)) {
          stop("showValue must be 0/1 or TRUE/FALSE")
        }
      }

      if ("gradient" %in% names(params)) {
        params$gradient <- as.integer(params$gradient)
        if (is.na(params$gradient)) {
          stop("gradient must be 0/1 or TRUE/FALSE")
        }
      }

      if ("border" %in% names(params)) {
        params$border <- as.integer(params$border)
        if (is.na(params$border)) {
          stop("border must be 0/1 or TRUE/FALSE")
        }
      }

      style <-
        validateColour(style, errorMsg = "Invalid colour specified in style.")

      values <- rule
      rule <- style
    } else if (type == "expression") {
      # type == "expression"
      # - style = createStyle()
      # - rule is an expression to evaluate

      # rule <- gsub(" ", "", rule)
      rule <- replaceIllegalCharacters(rule)
      rule <- gsub("!=", "&lt;&gt;", rule)
      rule <- gsub("==", "=", rule)

      if (!grepl("[A-Z]", substr(rule, 1, 2))) {
        ## formula looks like "operatorX" , attach top left cell to rule
        rule <-
          paste0(getCellRefs(data.frame(
            "x" = min(rows), "y" = min(cols)
          )), rule)
      } ## else, there is a letter in the formula and apply as is

      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }

      if (!"Style" %in% class(style)) {
        stop("If type == 'expression', style must be a Style object.")
      }

      invisible(dxfId <- wb$addDXFS(style))
    } else if (type == "duplicatedValues") {
      # type == "duplicatedValues"
      # - style is a Style object
      # - rule is ignored

      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }

      if (!"Style" %in% class(style)) {
        stop("If type == 'duplicates', style must be a Style object.")
      }

      invisible(dxfId <- wb$addDXFS(style))
      rule <- style
    } else if (type == "containsText") {
      # type == "contains"
      # - style is Style object
      # - rule is text to look for
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }
      
      
      if (!"character" %in% class(rule)) {
        stop("If type == 'contains', rule must be a character vector of length 1.")
      }
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'contains', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style))
      values <- rule
      rule <- style
    } else if (type == "notContainsText") {
      # type == "contains"
      # - style is Style object
      # - rule is text to look for
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }
      
      
      if (!"character" %in% class(rule)) {
        stop("If type == 'notContains', rule must be a character vector of length 1.")
      }
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'notContains', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style))
      values <- rule
      rule <- style
    } else if (type == "beginsWith") {
      # type == "contains"
      # - style is Style object
      # - rule is text to look for
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }
      
      
      if (!"character" %in% class(rule)) {
        stop("If type == 'beginsWith', rule must be a character vector of length 1.")
      }
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'beginsWith', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style))
      values <- rule
      rule <- style
    } else if (type == "endsWith") {
      # type == "contains"
      # - style is Style object
      # - rule is text to look for
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }
      
      
      if (!"character" %in% class(rule)) {
        stop("If type == 'endsWith', rule must be a character vector of length 1.")
      }
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'endsWith', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style))
      values <- rule
      rule <- style
    } else if (type == "between") {
      rule <- range(rule)

      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }

      if (!"Style" %in% class(style)) {
        stop("If type == 'between', style must be a Style object.")
      }

      invisible(dxfId <- wb$addDXFS(style))
    } else if (type == "topN") {
      # type == "topN"
      # - rule is ignored
      # - 'rank' and 'percent' are named params 
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      } 
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'topN', style must be a Style object.")
      } 
      
      invisible(dxfId <- wb$addDXFS(style)) 
      
      ## Additional parameters passed by ...
      if ("percent" %in% names(params)) {
        params$percent <- as.integer(params$percent)
        if (is.na(params$percent)) {
          stop("percent must be 0/1 or TRUE/FALSE")
        }
      } 
      
      if ("rank" %in% names(params)) {
        params$rank <- as.integer(params$rank)
        if (is.na(params$rank)) {
          stop("rank must be a number")
        }
      } 
      
      invisible(dxfId <- wb$addDXFS(style))
      values <- params
      rule <- style
    } else if (type == "bottomN") {
      # type == "bottomN"
      # - rule is ignored
      # - 'rank' and 'percent' are named params 
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      } 
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'bottomN', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style)) 
      
      ## Additional parameters passed by ...
      if ("percent" %in% names(params)) {
        params$percent <- as.integer(params$percent)
        if (is.na(params$percent)) {
          stop("percent must be 0/1 or TRUE/FALSE")
        }
      } 
      
      if ("rank" %in% names(params)) {
        params$rank <- as.integer(params$rank)
        if (is.na(params$rank)) {
          stop("rank must be a number")
        }
      } 
      
      invisible(dxfId <- wb$addDXFS(style))
      values <- params
      rule <- style
    }  else if (type == "containsBlanks") {
      # rule is ignored
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'blanks', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style))
    } else if (type == "notContainsBlanks") {
      # rule is ignored
      
      if (is.null(style)) {
        style <-
          createStyle(fontColour = "#9C0006", bgFill = "#FFC7CE")
      }
      
      if (!"Style" %in% class(style)) {
        stop("If type == 'notBlanks', style must be a Style object.")
      }
      
      invisible(dxfId <- wb$addDXFS(style))
    }



    invisible(
      wb$conditionalFormatting(
        sheet,
        startRow = min(rows),
        endRow = max(rows),
        startCol = min(cols),
        endCol = max(cols),
        dxfId = dxfId,
        formula = rule,
        type = type,
        values = values,
        params = params
      )
    )

    invisible(0)
  }