diff --git a/R/gtsave.R b/R/gtsave.R index 5cba3c709..320631e70 100644 --- a/R/gtsave.R +++ b/R/gtsave.R @@ -485,14 +485,18 @@ gt_save_docx <- function( con = word_md_file ) + temp_filename <- tempfile(fileext = paste0(".",tools::file_ext(filename))) rmarkdown::pandoc_convert( input = word_md_file, - output = filename + output = temp_filename, ) if (needs_gt_as_word_post_processing(word_md_text)) { - gt_as_word_post_processing(path = filename) + gt_as_word_post_processing(path = temp_filename) } + + file.rename(temp_filename, filename) + } #' Get the lowercase extension from a filename diff --git a/R/utils.R b/R/utils.R index 02f681d7d..0dae313f9 100644 --- a/R/utils.R +++ b/R/utils.R @@ -1177,6 +1177,7 @@ markdown_to_xml <- function(text) { USE.NAMES = FALSE, FUN = function(x, ...) commonmark::markdown_xml(linebreak_br(x), ...) ) + vapply( res, FUN.VALUE = character(1L), @@ -1225,9 +1226,21 @@ markdown_to_xml <- function(text) { } } - res <- lapply(children, apply_rules) - res <- vapply(res, FUN = as.character, FUN.VALUE = character(1L)) - res <- paste0(res, collapse = "") + if(length(children)){ + res <- lapply(children, apply_rules) + res <- vapply(res, FUN = as.character, FUN.VALUE = character(1L)) + res <- paste0(res, collapse = "") + }else{ + res <- xml_p( + xml_pPr( + xml_spacing(before = 0, after = 60) + ), + xml_r( + xml_rPr(), + xml_t() + ) + ) + } paste0("", res, "") } ) diff --git a/R/utils_render_xml.R b/R/utils_render_xml.R index ca89da601..9b8a90e2c 100644 --- a/R/utils_render_xml.R +++ b/R/utils_render_xml.R @@ -2958,7 +2958,7 @@ parse_to_xml <- function(x, ...) { ##check if wrapped in ooxml ## get what it starts with and assign - if (is.null(x)) { + if (is.null(x) | identical(x,"")) { x <- xml_p( xml_pPr( diff --git a/tests/testthat/test-as_word.R b/tests/testthat/test-as_word.R index 0df5b6dcd..0980964f3 100644 --- a/tests/testthat/test-as_word.R +++ b/tests/testthat/test-as_word.R @@ -2835,3 +2835,33 @@ test_that("multicolumn stub are supported", { expect_equal(xml_attr(xml_find_all(tcPr[[1]], ".//w:gridSpan"), "val"), "3") }) + +test_that("word ooxml handles empty markdown cells gt object", { + + # Create a one-row table for these tests + gt_empty_md <- exibble[1, 1:2] |> + dplyr::mutate(num = " ") |> + gt() |> + cols_label( + num = md(" "), + char = md("char") + ) |> + fmt_markdown() + + ## convert to word xml nodeset + xml <- gt_empty_md %>% as_word() %>% xml2::read_xml() + + table_cells <- xml_find_all(xml, ".//w:tc//w:t") + + expect_equal( + as.character(table_cells), + c( + "", + "char", + "", + "apricot" + ) + ) + +}) +