davidgohel / officer

:cop: officer: office documents from R
https://ardata-fr.github.io/officeverse/
Other
594 stars 107 forks source link

fix: ph_location_type() - throw error for out of range type id (fix #602) and more info if ph type not present (close #601) #603

Closed markheckmann closed 3 weeks ago

markheckmann commented 3 weeks ago

ph_location_type():

  1. now throws an error if the id for a type is out of range. Before, it went unnoticed (no error or warning) and caused a corrupted .pptx file (fix #602).
  2. generates a more informative error message when the type exists but is not present in the current layout (close #601).
  3. error message if the type is unknown was rewritten using cli style message

BEFORE

library(officer)
x <- read_pptx()
x <- x |> add_slide("Two Content")

# 1) id=3 does not exists => no error or warning
x |> ph_with("unknown id", ph_location_type("body", id = 3)) 
pptx document with 1 slide(s)
Available layouts and their associated master(s) are:
             layout       master
1       Title Slide Office Theme
2 Title and Content Office Theme
3    Section Header Office Theme
4       Two Content Office Theme
5        Comparison Office Theme
6        Title Only Office Theme
7             Blank Office Theme

# 2) tbl is allowed type but not present in slide => error code not informative
x |> ph_with("unknown id", ph_location_type("tbl", id = 3))   
Error in get_ph_loc(doc, layout = layout, master = master, type = x$type,  : 
  no selected row

# 3) type is unkown
> x |> ph_with("unknown id", ph_location_type("xxx")) 
Error in ph_location_type("xxx") : 
  argument type ('xxx') expected to be a value of "ctrTitle", "subTitle", "dt", "ftr", "sldNum", "title", "body", "pic", "chart", "tbl", "dgm", "media", "clipArt".

NEW

# 1) generates error now
x |> ph_with("unknown id", ph_location_type("body", id = 3))
Error:
! `id` is out of range.
✖ Must be between 1 and 2 for ph type "body".
ℹ see `layout_properties(x, 'Two Content', 'Office Theme')` for all phs with type 'body'

# 2) more informative error 
x |> ph_with("unknown id", ph_location_type("tbl", id = 3)) 
Error:
! Found no placeholder of type "tbl" on layout "Two Content".
✖ Available types are "body", "dt", "ftr", "sldNum", and "title"
ℹ see `layout_properties(x, 'Two Content', 'Office Theme')`

# 3) error message in cli style
x |> ph_with("unknown id", ph_location_type("xxx")) 
Error:
! type "xxx" is unknown.
✖ Must be one of "ctrTitle", "subTitle", "dt", "ftr", "sldNum", "title",
  "body", "pic", "chart", "tbl", "dgm", "media", or "clipArt"