Skip to content
Open
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
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: readxlsb
Type: Package
Title: Read 'Excel' Binary (.xlsb) Workbooks
Version: 0.1.4
Version: 0.2.0
Date: 2020-09-29
Authors@R: person("Michael", "Allen", role = c("aut", "cre"), email = "michael@velofrog.com")
Depends: R (>= 3.3.0)
Expand All @@ -22,4 +22,4 @@ Suggests:
rmarkdown
VignetteBuilder:
knitr
RoxygenNote: 7.0.1
RoxygenNote: 7.1.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export(read_xlsb)
export(xlsb_sheets)
importFrom(Rcpp,evalCpp)
importFrom(utils,unzip)
useDynLib(readxlsb, .registration = TRUE)
18 changes: 18 additions & 0 deletions R/parse_xlsb.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Backend function for superior modularity
parse_xlsb <- function(path) {
## Parameter checks
stop_if_not_defined(path, "path missing")

xlsb_env = new.env()

xlsb_contents = unzip(zipfile = path, list = TRUE)
idx = which(grepl("xl/workbook.bin", xlsb_contents$Name))
if (length(idx) == 0) stop("Failed to find xl/workbook.bin in file")
cn = unz(path, filename = xlsb_contents[idx, "Name"], open = "rb")
xlsb_env$stream = readBin(cn, "raw", xlsb_contents[idx, "Length"])
close(cn)

## Parse workbook.bin
ParseWorkbook(xlsb_env)
xlsb_env
}
15 changes: 1 addition & 14 deletions R/read_xlsb.R
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,8 @@ read_xlsb = function(path, sheet = NULL, range = NULL, col_names = TRUE, col_typ
na = "", trim_ws = TRUE, skip = 0, ...) {

## Parameter checks
stop_if_not_defined(path, "path missing")
if (!is.numeric(skip) || (skip < 0)) stop("Expecting non-negative value for skip")

## Store intermediate objects in an environment
xlsb_env = new.env()

xlsb_contents = unzip(zipfile = path, list = TRUE)
idx = which(grepl("xl/workbook.bin", xlsb_contents$Name))
if (length(idx) == 0) stop("Failed to find xl/workbook.bin in file")
cn = unz(path, filename = xlsb_contents[idx, "Name"], open = "rb")
xlsb_env$stream = readBin(cn, "raw", xlsb_contents[idx, "Length"])
close(cn)

## Parse workbook.bin
ParseWorkbook(xlsb_env)
xlsb_env <- parse_xlsb(path)

## Use workbook.bin.rels XML file to extract Type and Target fields
idx = which(grepl("xl/_rels/workbook.bin.rels", xlsb_contents$Name))
Expand Down
12 changes: 12 additions & 0 deletions R/xlsb_sheets.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#' List all sheets in a .xlsb workbook
#'
#' Analogous to the \code{excel_sheets} function from \code{readxl}. Efficiently gets the sheet names without needing to read any surplus data.
#' @usage xlsb_sheets(path)
#' @param path Path to the xlsb workbook
#' @examples
#' xlsb_sheets(path = system.file("extdata", "TestBook.xlsb", package = "readxlsb"))
#'
#' @export
xlsb_sheets = function(path) {
parse_xlsb(path)$sheets$Name
}
2 changes: 1 addition & 1 deletion man/read_xlsb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions man/xlsb_sheets.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// ParseWorkbook
void ParseWorkbook(Rcpp::Environment xlsb_env);
RcppExport SEXP _readxlsb_ParseWorkbook(SEXP xlsb_envSEXP) {
Expand Down