Skip to content

Commit

Permalink
NULL needs one more layer of wrapping
Browse files Browse the repository at this point in the history
Closes #203
  • Loading branch information
jennybc committed Jul 15, 2021
1 parent f8b5b40 commit ef06901
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ googlesheets4 now throws errors with class `"googlesheets4_error"` (#12).
The `na` argument of `read_sheet()` has become more capable and more consistent with readr.
Specifically, `na = character()` (or the general lack of `""` among the `na` strings) results in cells with no data appearing as the empty string `""` within a character vector, as opposed to `NA` (#174).

Explicit `NULL`s are now written properly, i.e. as an empty cell (#203).

## Dependency changes

* cli is new in Imports.
Expand Down
2 changes: 1 addition & 1 deletion R/schema_CellData.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ cell_data <- function(x, val_type, .na = NULL) {

#' @export
as_CellData.NULL <- function(x, .na = NULL) {
empty_cell(.na)
list(empty_cell(.na))
}

#' @export
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-range_write.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ test_that("range_write() works", {
expect_equal(df, data[1:3])
})

# https://github.com/tidyverse/googlesheets4/issues/203
test_that("we can write a hole-y tibble containing NULLs", {
skip_if_offline()
skip_if_no_token()

dat_write <- tibble::tibble(A = list(NULL, "HI"), B = month.abb[1:2])

ss <- local_ss(me_("write-NULL"), sheets = dat_write)
write_sheet(dat_write, ss, sheet = 1)

dat_read <- read_sheet(ss)
expect_equal(dat_read$A, c(NA, "HI"))
expect_equal(dat_read$B, dat_write$B)

dat_read <- read_sheet(ss, col_types = "Lc")
expect_equal(dat_read$A, dat_write$A)
expect_equal(dat_read$B, dat_write$B)
})

# ---- helpers ----
test_that("prepare_loc() makes the right call re: `start` vs. `range`", {
expect_loc <- function(x, loc) {
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-schema_CellData.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test_that("expect_empty_cell() is synced with empty_cell()", {
})

test_that("as_CellData() treats NULL as empty cell", {
expect_empty_cell(as_CellData(NULL))
expect_empty_cell(as_CellData(NULL)[[1]])
})

test_that("as_CellData() works for logical", {
Expand Down

0 comments on commit ef06901

Please sign in to comment.