File: setWindowSize.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 (70 lines) | stat: -rw-r--r-- 2,118 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
#' Set and Get Window Size for xlsx file
#'
#' @param wb A Workbook object
#' @param xWindow the horizontal coordinate of the top left corner of the window
#' @param yWindow the vertical coordinate of the top left corner of the window
#' @param windowWidth the width of the window
#' @param windowHeight the height of the window
#' 
#' Set the size and position of the window when you open the xlsx file.  The units are in twips.  See
#' [Microsoft's documentation for the xlsx standard](https://learn.microsoft.com/en-us/dotnet/api/documentformat.openxml.spreadsheet.workbookview?view=openxml-2.8.1)
#'
#' @export
#'
#' @examples
#' ## Create Workbook object and add worksheets
#' wb <- createWorkbook()
#' addWorksheet(wb, "S1")
#' getWindowSize(wb)
#' setWindowSize(wb, windowWidth = 10000)
setWindowSize <- function(wb, xWindow = NULL, yWindow = NULL, windowWidth = NULL, windowHeight= NULL) {

  bookViews <- wb$workbook$bookViews

  if(!is.null(xWindow)) {
    if(as.integer(xWindow) >= 0L) {
      bookViews <- sub("xWindow=\"\\d+", paste0("xWindow=\"", xWindow), bookViews)
    } else {
      stop("xWindow must be >= 0")
    }
  }

  if(!is.null(yWindow)) {
    if(as.integer(yWindow) >= 0L) {
      bookViews <- sub("yWindow=\"\\d+", paste0("yWindow=\"", yWindow), bookViews)
    } else {
      stop("yWindow must be >= 0")
    }
  }

  if(!is.null(windowWidth)) {
    if(as.integer(windowWidth) >= 100L) {
      bookViews <- sub("windowWidth=\"\\d+", paste0("windowWidth=\"", windowWidth), bookViews)
    } else {
      stop("windowWidth must be >= 100")
    }
  }

  if(!is.null(windowHeight)) {
    if(as.integer(windowHeight) >= 100L) {
      bookViews <- sub("windowHeight=\"\\d+", paste0("windowHeight=\"", windowHeight), bookViews)
    } else {
      stop("windowHeight must be >= 100")
    }
  }

  wb$workbook$bookViews <- bookViews
}

#' @rdname setWindowSize
#' @export

getWindowSize <- function(wb) {
  bookViews <- wb$workbook$bookViews

  c(getAttrs(bookViews, "xWindow"),
    getAttrs(bookViews, "yWindow"),
    getAttrs(bookViews, "windowWidth"),
    getAttrs(bookViews, "windowHeight"))

}