Skip to content

Commit f4f7570

Browse files
committed
chore(doc): add tipp to create custom modifier #86
1 parent 4507e0d commit f4f7570

File tree

1 file changed

+53
-2
lines changed

1 file changed

+53
-2
lines changed

README.md

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ If you require commercial support for complex .pptx automation, you can explore
4444
- [Import and modify slide Masters](#import-and-modify-slide-masters)
4545
- [Track status of automation process](#track-status-of-automation-process)
4646
- [More examples](#more-examples)
47+
- [Create a new modifier](#create-a-new-modifier)
4748
- [Troubleshooting](#troubleshooting)
4849
- [Testing](#testing)
4950
- [Special Thanks](#special-thanks)
@@ -589,7 +590,7 @@ If you would like to modify elements in a single .pptx file, it is important to
589590

590591
This is how it works internally:
591592

592-
- Load a root template to append slides to
593+
- Load a root template to append slides to it
593594
- (Probably) load root template again to modify slides
594595
- Load other templates
595596
- Append a loaded slide to (probably truncated) root template
@@ -621,7 +622,7 @@ const run = async () => {
621622
// Defining a "name" as second params makes it a little easier
622623
.load(`SlideWithShapes.pptx`, 'myTemplate');
623624

624-
// This is brandnew: get useful information about loaded templates:
625+
// Get useful information about loaded templates:
625626
const myTemplates = await pres.getInfo();
626627
const mySlides = myTemplates.slidesByTemplate(`myTemplate`);
627628

@@ -856,6 +857,55 @@ const automizer = new Automizer({
856857
});
857858
```
858859

860+
## Create a new modifier
861+
862+
If the built-in modifiers of `pptx-automizer` are not sufficient to your task, you can access the target xml elements with [xmldom](https://github.com/xmldom/xmldom). A modifier is a wrapper for such an operation.
863+
864+
Let's first take a look at a (simplified) existing modifier: `ModifyTextHelper.content('This is my text')`.
865+
866+
```ts
867+
// "setTextContent" is a function that returns a function.
868+
// A "label" argument needs to be passed to "setTextContent".
869+
const setTextContent = function (label: number | string) {
870+
// On setup, we can handle the argument.
871+
const newTextContent = String(label);
872+
873+
// A new function is returned to apply the label at runtime.
874+
return function (shape: XmlElement) {
875+
// "shape" contains a modifiable xmldom object.
876+
// You can use a selector to find the required 'a:t' element:
877+
const textElement = shape.getElementsByTagName('a:t').item(0);
878+
879+
// You can now apply the "newTextContent".
880+
if (textElement?.firstChild) {
881+
// Refer to xmldom for available functions.
882+
textElement.firstChild.textContent = newTextContent;
883+
}
884+
// It is possible to output the xml to console at any time.
885+
// XmlHelper.dump(element);
886+
};
887+
};
888+
```
889+
This function will construct an anonymous callback function on setup, while the callback function itself will be executed on runtime, when it's up to the target element on a slide.
890+
891+
You can use the modifier e.g. on adding an element:
892+
893+
```ts
894+
pres.addSlide('SlideWithShapes.pptx', 2, (slide) => {
895+
// This will import the 'Drum' shape
896+
slide.modifyElement('Cloud', [
897+
// 1. Dump the original xml:
898+
// Notice: don't call XmlHelper.dump, just pass it
899+
XmlHelper.dump,
900+
// 2. Apply modifier from the example above:
901+
setTextContent('New text'),
902+
XmlHelper.dump,
903+
]);
904+
});
905+
```
906+
907+
We can wrap any xml modification by such a modifier. If you have a working example and you think it will be useful to others, you are very welcome to fork this repo and send a pull request or simply [post it](https://github.com/singerla/pptx-automizer/issues/new).
908+
859909
## More examples
860910

861911
Take a look into [**tests**-directory](https://github.com/singerla/pptx-automizer/blob/main/__tests__) to see a lot of examples for several use cases, e.g.:
@@ -868,6 +918,7 @@ Take a look into [**tests**-directory](https://github.com/singerla/pptx-automize
868918
- [Update chart legend](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-legend.test.ts)
869919

870920
## Troubleshooting
921+
871922
If you encounter problems when opening a `.pptx`-file modified by this library, you might worry about PowerPoint not giving any details about the error. It can be hard to find the cause, but there are some things you can check:
872923

873924
- **Broken relation**: There are still unsupported shape types and `pptx-automizer` wil not copy required relations of those. You can inflate `.pptx`-output and check `ppt/slides/_rels/slide[#].xml.rels`-files to find possible missing files.

0 commit comments

Comments
 (0)