openxlsx2
openxlsx2 copied to clipboard
treat namedRegion as dims
when calling writeData()
it should be possible to easily write into a named region instead of just dims
well, at least for now it's possible to write a named redgion. one step at a time
Maybe a named region can be used with empty dims and namedRegion2dims()
. Will think about it a bit more.
getNamedRegions()
already provides all we need. We even split sheet name and dims. Not sure why we never picked it up. We can use the top left corner as a starting point and rewrite the named region for the size we are writing.
I started work on #548 I wanted to improve the ability sketched below
library(openxlsx2)
# create a workbook with a named region
wb <- wb_workbook() |>
wb_add_worksheet() |>
wb_add_named_region(
name = "named_region",
dims = wb_dims(
rows = 5:7,
cols = 6:8
)
)
# get the named region
nr <- wb$get_named_regions()
nr
#> name value sheets coords id local sheet
#> 1 named_region 'Sheet 1'!$F$5:$H$7 Sheet 1 F5:H7 1 0 1
# create a matrix of 1
mat <- dims_to_dataframe(nr$coords)
mat[is.na(mat)] <- 1
# write the data on the sheet
wb$add_data(sheet = nr$sheets, dims = nr$coords, x = mat, colNames = FALSE)
# read the data from the sheet
wb_to_df(wb, sheet = nr$sheets, dims = nr$coords, colNames = FALSE)
#> F G H
#> 5 1 1 1
#> 6 1 1 1
#> 7 1 1 1
Should add some update named region function by name and sheet
Maybe we can integrate this into an assert_dims()
function. Updated code snippet above, still this is something that I really really like to see.