awalker89 / openxlsx

R package for .xlsx file reading and writing.
Other
364 stars 79 forks source link

addStyle accepts nonsensical row values #420

Open md0u80c9 opened 6 years ago

md0u80c9 commented 6 years ago

Expected Behavior

If the row or column values passed to addStyle are outside of the permitted row and column range for Excel, generate an error.

Actual Behavior

Nonsensical values are accepted which allows corrupt Excel files to be produced. On Mac Office they will usually crash the machine.

Sample code chunk which produces this:

    border_table <- tibble::tribble(
      ~border,            ~columns,             ~rows,
      # Make a border band around the header row top and bottom
      c("top", "bottom"), 1:sheet_width,        header_row_length,
      # Border the header columns
      "right",            header_columns_width, 0:sheet_length,
      # Box the data
      "top",              1:sheet_width,        1,
      "bottom",           1:sheet_width,        sheet_length,
      "left",             1,                    1:sheet_length,
      "right",            sheet_width,          1:sheet_length,
    )

    purrr::pmap(.l = border_table,
      .f = function(border, columns, rows) {
        openxlsx::addStyle(wb = workbook,
        sheet = cohort_name,
        rows = rows,
        cols = columns,
        style = openxlsx::createStyle(border = border,
                                      borderStyle = "medium"),
        stack = TRUE)
      })

Note that in one of the data lines we slipped a zero in the formatter (pretty sure the example can be reduced to a single line demonstrating a border of 0 but wanted to give you the actual code used).

Only tested with Mac Office which crashes horribly if you try to load the resulting Excel file (report filed to Microsoft). Windows Excel likely to fare better I'm sure at least at spotting the file is borked.

OpenxlsxTest.xlsx