Skip to content
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

addStyle does not allow for rectangular inputs #492

Open
thisCoolNerd opened this issue Aug 24, 2024 · 1 comment
Open

addStyle does not allow for rectangular inputs #492

thisCoolNerd opened this issue Aug 24, 2024 · 1 comment

Comments

@thisCoolNerd
Copy link

Describe the bug
addStyle accepts only one row across columns, one column across rows, or an equal number of columns and rows. It does not accept a combination (e.g., 15 rows across 4 columns).

To Reproduce
df <- data.frame(
Age = sample(18:65, 15), # Numeric column
Gender = sample(c("Male", "Female"), 15, replace = TRUE), # String column
Income = sample(20000:100000, 15), # Numeric column
Occupation = sample(c("Teacher", "Engineer", "Doctor", "Artist", "Scientist"), 15, replace = TRUE), # String column
Married = sample(c("Yes", "No"), 15, replace = TRUE), # String column
Children = sample(0:5, 15, replace = TRUE), # Numeric column
Owns_House = sample(c(TRUE, FALSE), 15, replace = TRUE), # Logical column
Owns_Car = sample(c(TRUE, FALSE), 15, replace = TRUE) # Logical column
)

sample_style <- createStyle(valign = "center", halign = "left")

Any empty Excel file can be uploaded here

xl_workbook <- loadWorkbook("excel_error_template.xlsx")
writeData(xl_workbook, "contacts", df, startCol = 1, startRow = 1, rowNames = F, colnames = T)

This is where I get an error

addStyle(xl_workbook, "contacts", sample_style, rows = c(2:nrow(df)), cols = c(1:ncol(df)))

This is the error

Error in addStyle(xl.workbook, tab.name, text_align, rows = c(1:nrow(table.df)), :
Length of rows and cols must be equal.

Expected behavior
I expected that it would apply the style across the selection. Instead, I had to create a loop to hack it, but it's not efficient.

Example files
excel_error_template.xlsx

@JanMarvin
Copy link
Collaborator

Hi @thisCoolNerd , try something like this in openxlsx2, it's unlikely to be fixed in openxlsx.

df <- data.frame(
  Age = sample(18:65, 15), # Numeric column
  Gender = sample(c("Male", "Female"), 15, replace = TRUE), # String column
  Income = sample(20000:100000, 15), # Numeric column
  Occupation = sample(c("Teacher", "Engineer", "Doctor", "Artist", "Scientist"), 15, replace = TRUE), # String column
  Married = sample(c("Yes", "No"), 15, replace = TRUE), # String column
  Children = sample(0:5, 15, replace = TRUE), # Numeric column
  Owns_House = sample(c(TRUE, FALSE), 15, replace = TRUE), # Logical column
  Owns_Car = sample(c(TRUE, FALSE), 15, replace = TRUE) # Logical column
)

library(openxlsx2)
wb <- wb_workbook()$
  add_worksheet("contacts")$
  add_data(x = df)$
  add_cell_style(vertical = "center", horizontal = "left", dims = wb_dims(x = df, select = "data"))

if (interactive()) wb$open()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants