Skip to content
Table into a Placeholder

Table into a Placeholder

A layout can carry a table placeholder, the empty grid PowerPoint shows with an Insert Table icon in the middle of it. PlaceHolder.AddTable builds a table for one of those, so a template can decide where the table goes and the program only has to fill it.

The name is slightly misleading. AddTable on a placeholder does not put the table inside the placeholder; it clears the placeholder and adds the table to the slide as a sibling shape. That is why the placeholder is removed afterwards:

CallResult
slide.AddTable()A table on the slide at offset 1 EMU. You position it.
ph.AddTable()The placeholder is cleared, and a table is added to the slide.

Neither inherits the placeholder’s position, so both need SetOffsetX and SetOffsetY.

Finding and replacing the placeholder

for _, slide := range ppt.Slides() {
    for _, ph := range slide.PlaceHolders() {
        if ph.Type() != pml.ST_PlaceholderTypeTbl {
            continue
        }
        tbl := ph.AddTable()
        // add columns, rows and cell content
        tbl.SetOffsetX(measurement.Inch)
        tbl.SetOffsetY(measurement.Millimeter * 20)

        if err := ph.Remove(); err != nil {
            log.Fatalf("error on removing placeholder: %v", err)
        }
    }
}

PlaceHolders() returns every placeholder on the slide and Type() gives its pml.ST_PlaceholderType, so scanning for ST_PlaceholderTypeTbl finds table placeholders wherever the template put them. GetPlaceholder would work when there is exactly one; scanning handles a deck with several slides and several tables.

ph.Remove() deletes the placeholder shape from the slide and returns an error if it is not found there. Without it the empty placeholder frame renders on top of or beside the table you just added.

Filling the table is the same work as the tables guide: AddCol per column, AddRow per row, and a text body built from the DrawingML types for each cell.

Limitations

Removing a placeholder while ranging over PlaceHolders() is safe only because the range holds a snapshot slice; Remove mutates the slide’s shape tree underneath it. Do not index back into PlaceHolders() after a removal.

The table does not take the placeholder’s position or size. If the point of using a placeholder was to let the designer decide where the table sits, read the position off the placeholder shape before removing it and pass it to SetOffsetX and SetOffsetY.

ph.AddTable calls Clear on the placeholder first, which discards its content. There is no way back once the table has been added, so operate on a copy of the template rather than in place.

Run the example

The example opens placeholder.pptx, finds the table placeholder on each slide, builds a four by three orange table in its place, removes the placeholder and writes result.pptx.

git clone https://github.com/unidoc/unioffice-examples.git
cd unioffice-examples/presentation/placeholder-add-table
go run main.go

If this is your first time using UniOffice, follow the getting started guide to create an API key and set up your development environment.

View the full source
Last updated on