Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use negative or infinite sheet number to select sheet counting from the last #742

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# readxl (development version)

# readxl 1.5.0

* Sheet number in `read_excel()`, `read_xls()`, and `read_xlsx()` can now be negative or infinite to select the sheet counting from the last one (#742, @DanChaltiel).

# readxl 1.4.3

This release contains no user-facing changes.
Expand Down
7 changes: 5 additions & 2 deletions R/read_excel.R
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,12 @@ standardise_sheet <- function(sheet, range, sheet_names) {

if (is.numeric(sheet)) {
if (sheet < 1) {
stop("`sheet` must be positive", call. = FALSE)
if(is.infinite(sheet)) sheet <- -length(sheet_names)
length(sheet_names) + sheet
} else {
if(is.infinite(sheet)) sheet <- length(sheet_names)
floor(sheet) - 1L
}
floor(sheet) - 1L
} else if (is.character(sheet)) {
if (!(sheet %in% sheet_names)) {
stop("Sheet '", sheet, "' not found", call. = FALSE)
Expand Down