colearendt / xlsx

@@ -41,7 +41,7 @@
Loading
41 41
#' ranges <- getRanges(wb)
42 42
#'
43 43
#' # the call below fails on cran tests for MacOS.  You should see the
44 -
#' # FAQ: http://code.google.com/p/rexcel/wiki/FAQ
44 +
#' # FAQ: https://code.google.com/p/rexcel/wiki/FAQ
45 45
#' #res  <- readRange(ranges[[1]], sheet, colClasses="numeric") # read it
46 46
#'
47 47
#' ranges[[1]]$getNameName()  # get its name

@@ -1,39 +1,39 @@
Loading
1 1
# This file contains other utilities to make this a more useful package
2 2
3 3
# If you have an Excel file template and use xlsx to fill in some data and write
4 -
# it back to a file (which is a general technique to produce Excel-based reports), 
5 -
# then it works beautifully except that Excel will not refresh formulae and pivot 
6 -
# tables when you open the file.  
4 +
# it back to a file (which is a general technique to produce Excel-based reports),
5 +
# then it works beautifully except that Excel will not refresh formulae and pivot
6 +
# tables when you open the file.
7 7
#
8 8
# The first problem of forcing formula calculation is easy to solve but the pivot
9 -
# table issue is not fixable by Apache POI library.  It turns out that 
9 +
# table issue is not fixable by Apache POI library.  It turns out that
10 10
# Excel can refresh pivot tables when you open the file if the pivot cache definition
11 11
# XML file has the refreshOnLoad flag set to 1.
12 -
# See http://stackoverflow.com/questions/11670816/how-to-refresh-pivot-cache-of-excel-2010-using-open-xml#16624292
12 +
# See https://stackoverflow.com/questions/11670816/how-to-refresh-pivot-cache-of-excel-2010-using-open-xml#16624292
13 13
#
14 14
# This method automates the hack.  Basically, unzip the excel file, update
15 15
# the pivot cache definition files and writes it back.  It's really a hack.
16 -
# This hack should be temporary when apache-poi eventually allows adding the 
16 +
# This hack should be temporary when apache-poi eventually allows adding the
17 17
# refreshOnLoad attribute for existing pivot tables in the workbook.
18 18
19 19
# Make Excel refresh all formula when the file is open
20 20
# If output is NULL then overwrite the original file
21 21
#' @title Force Refresh Pivot Tables and Formulae
22 -
#' 
23 -
#' @description Functions to force formula calculation or refresh of pivot 
22 +
#'
23 +
#' @description Functions to force formula calculation or refresh of pivot
24 24
#' tables when the Excel file is opened.
25 -
#' 
26 -
#' @details   
25 +
#'
26 +
#' @details
27 27
#' \code{forcePivotTableRefresh} forces pivot tables to be refreshed when the Excel file is opened.
28 28
#' \code{forceFormulaRefresh} forces formulae to be recalculated when the Excel file is opened.
29 -
#' 
29 +
#'
30 30
#' @param file the path of the source file where formulae/pivot table needs to be refreshed
31 31
#' @param output the path of the output file.  If it is \code{NULL} then the source file will be overwritten
32 32
#' @param verbose Whether to make logging more verbose
33 -
#' 
33 +
#'
34 34
#' @return Does not return any results
35 -
#' 
36 -
#' @examples 
35 +
#'
36 +
#' @examples
37 37
#' # Patch a file where its pivot tables are not recalculated when the file is opened
38 38
#' \dontrun{
39 39
#' forcePivotTableRefresh("/tmp/file.xlsx")
@@ -44,14 +44,14 @@
Loading
44 44
#' forceFormulaRefresh("/tmp/file.xlsx")
45 45
#' forceFormulaRefresh("/tmp/file.xlsx", "/tmp/fixed_file.xlsx")
46 46
#' }
47 -
#' 
48 -
#' 
47 +
#'
48 +
#'
49 49
#' @author Tom Kwong
50 -
#' 
50 +
#'
51 51
#' @export
52 52
#' @rdname autoRefresh
53 53
forceFormulaRefresh <- function(file, output=NULL, verbose=FALSE) {
54 -
  
54 +
55 55
  # redirect output to source location?
56 56
  if (is.null(output)) {
57 57
    output <- file
@@ -59,11 +59,11 @@
Loading
59 59
      cat(sprintf("Overwriting source file at %s\n", file))
60 60
    }
61 61
  }
62 -
  
62 +
63 63
  wb <- loadWorkbook(file)
64 64
  wb$setForceFormulaRecalculation(TRUE)
65 65
  saveWorkbook(wb, output)
66 -
  
66 +
67 67
  if (verbose) {
68 68
    cat(sprintf("Successfully patched file to auto calculate formulae. File saved at %s\n", output))
69 69
  }
@@ -72,7 +72,7 @@
Loading
72 72
#' @export
73 73
#' @rdname autoRefresh
74 74
forcePivotTableRefresh <- function(file, output=NULL, verbose=FALSE) {
75 -
  
75 +
76 76
  if (!file.exists(file)) {
77 77
    stop("File does not exist ", file)
78 78
  }
@@ -84,30 +84,30 @@
Loading
84 84
      cat(sprintf("Overwriting source file at %s\n", file))
85 85
    }
86 86
  }
87 -
  
87 +
88 88
  # create a temp directory to hold the unzip'ed Excel content
89 89
  tmpDir <- tempfile()
90 90
  dir.create(tmpDir)
91 91
  #cat(sprintf("Temp directory: %s", tmpDir))
92 -
  
92 +
93 93
  # unzip the excel file
94 94
  unzip(file, exdir = tmpDir)
95 -
  
95 +
96 96
  # find pivot cache definition files & patch them
97 97
  pivotTablesPatched <- 0
98 98
  pivotCacheDir <- file.path(tmpDir, "xl", "pivotCache")
99 99
  if (dir.exists(pivotCacheDir)) {
100 100
    pivotCacheFiles <- list.files(path = pivotCacheDir)
101 101
    pivotCacheDefFiles <- pivotCacheFiles[grepl("pivotCacheDefinition", pivotCacheFiles)]
102 -
    result <- lapply(pivotCacheDefFiles, 
102 +
    result <- lapply(pivotCacheDefFiles,
103 103
                     function(defFile) {
104 104
                       # Read pivot cache definition file
105 105
                       workingFile <- file.path(pivotCacheDir, defFile)
106 106
                       text <- readLines(workingFile, warn = FALSE)
107 -
                       
107 +
108 108
                       # Add refreshOnLoad attribute
109 109
                       text <- gsub('refreshedBy=', 'refreshOnLoad="1" refreshedBy=', text)
110 -
                       
110 +
111 111
                       # Write back to the file now.
112 112
                       # Excel is particular about the file format... cannot use writeLines
113 113
                       # or else it will be a unix-style (newline, has EOL at last line)
@@ -115,14 +115,14 @@
Loading
115 115
                       cat(text, file = workingFile)
116 116
                     })
117 117
    pivotTablesPatched <- length(result)
118 -
  } 
119 -
  
118 +
  }
119 +
120 120
  if (pivotTablesPatched > 0) {
121 121
    oldwd <- getwd()
122 122
    setwd(tmpDir)
123 123
    tmpOutputFile <- paste0(tempfile(), ".xlsx")
124 -
    status <- zip(tmpOutputFile, 
125 -
                  files = list.files(tmpDir, recursive = TRUE, all.files = TRUE), 
124 +
    status <- zip(tmpOutputFile,
125 +
                  files = list.files(tmpDir, recursive = TRUE, all.files = TRUE),
126 126
                  flags = "-r9q")
127 127
    setwd(oldwd)
128 128
    if (status != 0) {
@@ -148,7 +148,7 @@
Loading
148 148
# # Unit testing
149 149
# # Run this file from the xlsx project home directory.
150 150
# input <- "resources/test_template1_stale.xlsx"
151 -
# 
151 +
#
152 152
# # unit test 1.  Take source file output.xlsx and patch & save to output2.xlsx
153 153
# cat("Unit test 1\n")
154 154
# tmp <- "/tmp/temp.xlsx"  # intermediate file for two operations below
@@ -158,7 +158,7 @@
Loading
158 158
# if (file.exists(output)) { file.remove(output) }
159 159
# forcePivotTableRefresh(input, tmp)
160 160
# forceFormulaRefresh(tmp, output)
161 -
# 
161 +
#
162 162
# # unit test 2. Make a copy of the source file.  Then patch in-place.
163 163
# # this usage seems more natural
164 164
# cat("Unit test 2\n")
@@ -167,7 +167,7 @@
Loading
167 167
# file.copy(input, output, overwrite = TRUE)
168 168
# forcePivotTableRefresh(output)
169 169
# forceFormulaRefresh(output)
170 -
# 
170 +
#
171 171
# # unit test 3.  Non-verbose.
172 172
# cat("Unit test 3 (expect no output)\n")
173 173
# output <- "/tmp/test_pivot_refresh_3.xlsx"
@@ -175,13 +175,13 @@
Loading
175 175
# file.copy(input, output, overwrite = TRUE)
176 176
# forcePivotTableRefresh(output, verbose = FALSE)
177 177
# forceFormulaRefresh(output, verbose = FALSE)
178 -
# 
178 +
#
179 179
# cat("Unit test completed!\n")
180 -
# 
181 -
# 
180 +
#
181 +
#
182 182
# # experiment how to patch Excel file
183 183
# # lines <- readLines("/tmp/good_pivot_cache_def_file.xml", warn = FALSE)
184 184
# # lines <- gsub('refreshedBy=', 'refreshOnLoad="1" refreshedBy=', lines)
185 185
# # lines <- paste(lines, collapse = '\r\n')
186 186
# # cat(lines, file = "/tmp/test_pivot_cache_def_file.xml", fill = FALSE)
187 -
# 
187 +
#

@@ -134,7 +134,7 @@
Loading
134 134
#'
135 135
#'   ## Add hyperlinks to a cell
136 136
#'   cell <- cells[[1,1]]
137 -
#'   address <- "http://poi.apache.org/"
137 +
#'   address <- "https://poi.apache.org/"
138 138
#'   setCellValue(cell, "click me!")
139 139
#'   addHyperlink(cell, address)
140 140
#'

@@ -9,7 +9,7 @@
Loading
9 9
# Create a DataFormat
10 10
# maybe allow to change the Locale
11 11
# .jfield("java/util/Locale", "Ljava/util/Locale;","FRENCH")
12 -
# http://docs.oracle.com/javase/7/docs/api/java/util/Locale.html
12 +
# https://docs.oracle.com/javase/7/docs/api/java/util/Locale.html
13 13
#' Create an DataFormat object.
14 14
#'
15 15
#' Create an DataFormat object, useful when working with cell styles.

@@ -65,7 +65,7 @@
Loading
65 65
#' The default formats for Date and DateTime columns can be changed via the two
66 66
#' package options \code{xlsx.date.format} and \code{xlsx.datetime.format}.
67 67
#' They need to be specified in Java date format
68 -
#' \url{http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html}.
68 +
#' \url{https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html}.
69 69
#'
70 70
#' Writing of password protected workbooks is supported for Excel 2007 OOXML
71 71
#' format only.  Note that in Linux, LibreOffice is not able to read password

@@ -16,7 +16,7 @@
Loading
16 16
#' The default formats for Date and DateTime columns can be changed via the two
17 17
#' package options \code{xlsx.date.format} and \code{xlsx.datetime.format}.
18 18
#' They need to be specified in Java date format
19 -
#' \url{http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html}.
19 +
#' \url{https://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html}.
20 20
#'
21 21
#' @param x a \code{data.frame}.
22 22
#' @param sheet a \code{\link{Sheet}} object.

@@ -180,7 +180,7 @@
Loading
180 180
## getWorksheets <- function(xls, ...)
181 181
## {
182 182
##   id <- .getUniqueId(xls@id)[[1]]
183 -
##   worksheetId <- paste("http://spreadsheets.google.com/feeds/spreadsheets/",
183 +
##   worksheetId <- paste("https://spreadsheets.google.com/feeds/spreadsheets/",
184 184
##                        id, sep="")
185 185
186 186
##   msg <- xls@con@ref$getWorksheetEntries(worksheetId)
@@ -392,7 +392,7 @@
Loading
392 392
## addWorksheet <- function(xls, title, nrow=100, ncol=20)
393 393
## {
394 394
##   key <- gsub("spreadsheet%3A(.*)", "\\1", xls@key)
395 -
##   id <- paste("http://spreadsheets.google.com/feeds/spreadsheets/", key,
395 +
##   id <- paste("https://spreadsheets.google.com/feeds/spreadsheets/", key,
396 396
##               sep="")
397 397
##   xls@con@ref$addWorksheet(title, as.integer(nrow), as.integer(ncol), id)
398 398
Files Coverage
R 84.14%
Project Totals (25 files) 84.14%
Notifications are pending CI completion. Waiting for GitHub's status webhook to queue notifications. Push notifications now.
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading