Swap element in slide master?
Closed this issue · 5 comments
I am trying to swap an element on roots; however, I am not understanding the syntax. From the example the addMaster function is used, but trying to run a similar code (because I don't have the presentations) I am getting the error. Can someone help with the syntax? Once I understand it I can add some new examples
Can't find element on slide 1 in root:
{
presName: 'root',
slideNumber: 1,
selector: 'MasterRectangle',
mode: 'modify',
callback: [Function (anonymous)]
}
Hi! You can also take a look at this file.
If you are planning to remove/add elements on a master slide, you could try this:
const automizer = new Automizer({
templateDir: `${__dirname}/../__tests__/pptx-templates`,
outputDir: `${__dirname}/../__tests__/pptx-output`,
removeExistingSlides: true,
});
const pres = automizer
.loadRoot(`RootTemplate.pptx`)
.load(`RootTemplate.pptx`, 'root')
.load('SlideWithShapes.pptx', 'shapes')
// Import first slide master and all its slide layouts:
.addMaster('root', 1, (master) => {
master.removeElement('UnusedShape');
master.addElement(`shapes`, 1, 'Cloud 1');
});
// Add a slide:
.addSlide('root', 1, (slide) => {
// use the modified master
slide.useSlideLayout();
});
pres.write(`myOutputPresentation.pptx`).then((summary) => {
console.log(summary);
});
It is important to know that this library is currently not able to modify something in place. You always need to add something to apply a modification. The code above needs to import "RootTemplate.pptx" twice, one for root and one for appending slides. Hope this helps! :)
There weren't any existing templates with an image in the slide master, so I needed to make my own. As far as I can tell this should be analogous to the "ModifyTextHelper" test in the file you linked. However, I am now throwing an error. Code and error both below
export async function test() {
const automizer = new Automizer({
templateDir: `~/Downloads`,
outputDir: `~/Downloads`,
mediaDir: `~/Downloads`,
removeExistingSlides: true,
cleanup: true,
});
const pres = automizer
.loadRoot(`blue.pptx`)
.load(`blue.pptx`, 'base')
.loadMedia([`blue_with_red_cross.png`]);
pres.addMaster('base', 1, (master) => {
master.modifyElement(
'Sidebar', [
ModifyImageHelper.setRelationTarget('blue_with_red_cross.png'),
]);
});
const result = await pres.write(`modify-image-in-master.test.pptx`);
}
Importing slideMaster 4
Importing slideLayout 22
Importing slideLayout 23
Importing slideLayout 24
Importing slideLayout 25
Importing slideLayout 26
~\node_modules\pptx-automizer\dist\shapes\image.js:20
this.sourceFile = shape.target.file.replace('../media/', '');
^
TypeError: Cannot read properties of undefined (reading 'file')
at new Image (~\node_modules\pptx-automizer\dist\shapes\image.js:20:40)
at Master.<anonymous> (~\node_modules\pptx-automizer\dist\classes\has-shapes.js:201:31)
at Generator.next (<anonymous>)
at fulfilled (~\node_modules\pptx-automizer\dist\classes\has-shapes.js:5:58)
Node.js v20.9.0
I can confirm this should work, but it doesn't. I need to go deeper into it, thanks a lot for reporting on this.
The relation target for swapping an image on a slideMaster was not set correctly. This is fixed with v 0.4.7
Thank you!