openxlsx2 icon indicating copy to clipboard operation
openxlsx2 copied to clipboard

treat namedRegion as dims

Open JanMarvin opened this issue 3 years ago • 6 comments

when calling writeData() it should be possible to easily write into a named region instead of just dims

JanMarvin avatar Jan 17 '22 07:01 JanMarvin

well, at least for now it's possible to write a named redgion. one step at a time

JanMarvin avatar Feb 10 '22 15:02 JanMarvin

Maybe a named region can be used with empty dims and namedRegion2dims(). Will think about it a bit more.

JanMarvin avatar Oct 13 '22 08:10 JanMarvin

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.

JanMarvin avatar Oct 15 '22 12:10 JanMarvin

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

JanMarvin avatar Feb 25 '23 12:02 JanMarvin

Should add some update named region function by name and sheet

JanMarvin avatar Feb 28 '23 10:02 JanMarvin

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.

JanMarvin avatar Mar 23 '24 09:03 JanMarvin