Retrieving named images using media #1105
Unanswered
MaxManek1110
asked this question in
Q&A
Replies: 1 comment 1 reply
-
Hi @MaxManek1110 , maybe something like this could get you started? There are probably a few corner cases I haven't thought of, but might be good for a first draft. A corner case might be a native drawing - a chart - which might not have a target file. library(openxlsx2)
img <- system.file("extdata", "einstein.jpg", package = "openxlsx2")
wb <- wb_workbook()$
add_worksheet()$
add_image("Sheet 1", dims = "C5", file = img, width = 6, height = 5)$
add_worksheet()$
add_image(dims = "B2", file = img)$
add_image(dims = "D2", file = img)$
add_worksheet()$
add_image(dims = "G3", file = img, width = 15, height = 12, units = "cm")
media_sheets <- lapply(seq_along(wb$sheet_names), function(x) {
if (length(wb$worksheets[[x]]$relships$drawing)) {
c(wb$get_sheet_names()[x], x)
}
})
ms <- as.data.frame(do.call("rbind", media_sheets))
ms$V2 <- as.integer(ms$V2)
files <- lapply(wb$drawings_rels[ms$V2], function(x) {
basename(openxlsx2:::rbindlist(xml_attr(x, "Relationship"))$Target)
})
names(files) <- ms$`Sheet 1`
files <- unlist(files)
media <- wb$media[unlist(files)]
names(media) <- names(files)
media
#> $`Sheet 1`
#> [1] "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/openxlsx2/extdata/einstein.jpg"
#>
#> $`Sheet 21`
#> [1] "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/openxlsx2/extdata/einstein.jpg"
#>
#> $`Sheet 22`
#> [1] "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/openxlsx2/extdata/einstein.jpg"
#>
#> $`Sheet 3`
#> [1] "/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/openxlsx2/extdata/einstein.jpg" |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
In a current Project I need to retrieve images of a workbook. Which works perfectly fine using
wb <- wb_load(file)
wb$media
The problem : The images are listed in chronological order and not with the names given in excel. Since the input is not static and later needs to dynamically be added to different cases.
Is there a way to see from which worksheet they are? Or even better retrieve there actual given names in the excel sheet?
Example current list output
image1.png
"/tmp/.../image1.png"
image2.jpeg
"/tmp/.../image2.jpeg"
Beta Was this translation helpful? Give feedback.
All reactions