Skip to content

read.xlsx() returns wrong sheet if sheet is specified by name and workbook contains veryHidden sheets #434

@heseber

Description

@heseber

Expected Behavior

The read.xlsx() call should return the correct sheet if the sheet is specified by the sheet name, irrespective of whether or not the workbook contains veryHidden sheets.

Actual Behavior

If the workbook contains veryHidden sheets, the names of such sheets are included in the list of sheet names returned by getSheetNames(). If read.xlsx() is used to request a sheet by name and if this sheet occurs after a veryHidden sheet in the workbook, then the sheet index is off by the number of state="veryHidden" sheets preceding the requested sheet in the workbook, and the wrong sheet is returned.

Steps to Reproduce the Problem

Use any file with a veryHidden sheet and request a sheet (by name) following that veryHidden sheet.

Example:

<sheet name="modMain" sheetId="18" state="veryHidden" r:id=""/>

For proprietary reasons, I cannot provide the file that triggered this issue.

Versions

  • Version of openxlsx: 4.1.1
  • Version of R: 3.5.1

Patch

diff --git a/R/wrappers.R b/R/wrappers.R
index 2aa7c0a..e4a6917 100644
--- a/R/wrappers.R
+++ b/R/wrappers.R
@@ -3061,7 +3061,9 @@ getSheetNames <- function(file){
   workbook <- xmlFiles[grepl("workbook.xml$", xmlFiles, perl = TRUE)]
   workbook <- readLines(workbook, warn=FALSE, encoding="UTF-8")
   workbook <-  removeHeadTag(workbook)
-  sheets <- unlist(regmatches(workbook, gregexpr("<sheet .*/sheets>", workbook, perl = TRUE)))
+  sheets <- unlist(regmatches(workbook, gregexpr("(?<=<sheets>).*(?=</sheets>)", workbook, perl = TRUE)))
+  sheets <- unlist(regmatches(sheets, gregexpr("<sheet[^>]*>", sheets, perl=TRUE)))
+  sheets <- grep("state=\"veryHidden\"", sheets, invert = TRUE, value = TRUE)
   sheetNames <- unlist(regmatches(sheets, gregexpr('(?<=name=")[^"]+', sheets, perl = TRUE)))
   sheetNames <- replaceXMLEntities(sheetNames)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions