diff --git a/NEWS.md b/NEWS.md index 2729a45d..9336d77e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -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. diff --git a/R/read_excel.R b/R/read_excel.R index cf512248..76ccd1f5 100644 --- a/R/read_excel.R +++ b/R/read_excel.R @@ -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)