-
Notifications
You must be signed in to change notification settings - Fork 11
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
wb_page_setup()
argument fit_to_width
does not apply scaling
#965
Comments
Hi @asadow , thanks for the report. I could be that we are simply assigning the page setup xml string for fit to page to the wrong worksheet argument. Lines 6118 to 6127 in fce092f
Edit: on second thought, that does not seem to be the case. Will look into. |
I had a look unfortunately it does not look like there is something really going wrong, therefore we might have to look for another solution. The documentation for scale states that:
Unfortunately it look like either I do not understand this correctly or it is wrong. Setting In addition there is a typo and maybe a thinko in Therefore it looks like library(openxlsx2)
library(glue)
.data <- structure(list(`Bldg/Eqp` = "5", `WO Num` = "123421", type = "BE",
category = "",
Description = "I am a description..............",
date = "02-22-24", status = "S", est_time = 1),
row.names = c(NA, -1L),
class = c("tbl_df", "tbl", "data.frame"))
wb_backlog <- function(.data) {
nm <- names(.data)
letter_desc <- letters[which(nm == "Description")] |> toupper()
letter_last <- letters[ncol(.data)] |> toupper()
wb_workbook() |>
wb_add_worksheet("Sheet1") |>
wb_add_data("Sheet1", .data) |>
wb_add_cell_style(
dims = glue("{letter_desc}2:{letter_desc}{nrow(.data) + 1}"),
wrap_text = "1"
) |>
wb_set_col_widths(cols = which(nm == "Description"), widths = 75) |>
wb_add_filter(rows = 1, cols = which(nm != "Description")) |>
wb_add_border(
dims = glue("A1:{letter_last}1"),
bottom_color = wb_color(theme = 1), bottom_border = "thin",
top_color = wb_color(theme = 1), top_border = "double",
left_border = NULL, right_border = NULL
) |>
wb_page_setup(orientation = "landscape", fit_to_width = TRUE)
}
wb <- .data |> wb_backlog()
# get the scaling factor (or get some scaling factor?)
cols <- wb$worksheets[[1]]$unfold_cols()
num_pages <- 1
col_width <- sum(as.numeric(cols$width)) / 100
scale <- round(num_pages/col_width * 100, digits = -1)
# apply scaling factor
wb <- wb |> wb_page_setup(orientation = "landscape", scale = scale, fit_to_width = num_pages)
# fix a typo "pageSetupPr != pageSetUpPr"
wb$worksheets[[1]]$sheetPr <- "<sheetPr><pageSetUpPr fitToPage=\"1\"/></sheetPr>"
if (interactive()) wb$open() |
Unfortunately the logic to create scaling is a bit more complex than I initially thought. It works in the example code above, but this is merely a fluke. I still intent to overhaul the |
#966 is merged ( The following is now possible (I've replaced the glue code with @olivroy's library(openxlsx2)
.data <- structure(
list(
`Bldg/Eqp` = "5",
`WO Num` = "123421",
type = "BE",
category = "",
Description = "I am a description..............",
date = "02-22-24",
status = "S",
est_time = 1
),
row.names = c(NA, -1L),
class = c("tbl_df", "tbl", "data.frame")
)
wb_backlog <- function(.data) {
nm <- names(.data)
wb_workbook() |>
wb_add_worksheet("Sheet1") |>
wb_add_data("Sheet1", .data) |>
wb_add_cell_style(
dims = wb_dims(x = .data, cols = "Description"),
wrap_text = "1"
) |>
wb_set_col_widths(cols = which(nm == "Description"), widths = 75) |>
wb_add_filter(rows = 1, cols = which(nm != "Description")) |>
wb_add_border(
dims = wb_dims(x = .data),
bottom_color = wb_color(theme = 1), bottom_border = "thin",
top_color = wb_color(theme = 1), top_border = "double",
left_border = NULL, right_border = NULL
) |>
wb_page_setup(orientation = "landscape", scale = 90, fit_to_width = 1)
}
wb <- .data |> wb_backlog()
if (interactive()) wb$open() As written previously, automatic scaling is not possible (the openxml documentation was a bit misleading in this regard). Excel apparently uses some GDI+ logic to scale a page. I haven't figured how to mimic this, therefore it's currently not something left to the user to figure out. Currently there is no wrapper The page setup function is also not something I have any real use case for, therefore I'm a bit reluctant to invest much more time into this. If you are interested, please go ahead and test a few things and please report if they are working or if the implemented logic checks out. The missing real life reason is also why #929 is not yet a feature of the package. |
After running the below, I go to File, Print, and see that no scaling is applied. The solution to fit all columns on one page is to use
scale = 80
instead. But I was expectingfit_to_width = TRUE
to work.Created on 2024-03-07 with reprex v2.0.2
The text was updated successfully, but these errors were encountered: