Skip to content

Commit 9cc5176

Browse files
authored
Merge branch 'main' into dependabot/npm_and_yarn/image-size-1.2.1
2 parents a15e204 + 1602f44 commit 9cc5176

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1830
-1317
lines changed

README.md

Lines changed: 102 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ If you require commercial support for complex .pptx automation, you can explore
2727
- [As a Cloned Repository](#as-a-cloned-repository)
2828
- [As a Package](#as-a-package)
2929
- [Usage](#usage)
30+
3031
- [Basic Example](#basic-example)
3132
- [How to Select Slides Shapes](#how-to-select-slides-shapes)
3233
- [Select slide by number and shape by name](#select-slide-by-number-and-shape-by-name)
@@ -37,11 +38,9 @@ If you require commercial support for complex .pptx automation, you can explore
3738
- [Modify Tables](#modify-tables)
3839
- [Modify Charts](#modify-charts)
3940
- [Modify Extended Charts](#modify-extended-charts)
41+
- [Generate shapes with PptxGenJs](#generate-shapes-with-pptxgenjs)
4042
- [Remove elements from a slide](#remove-elements-from-a-slide)
41-
- [🔗 Hyperlink Management](#🔗-hyperlink-management)
42-
- [Hyperlink Helper Functions](#hyperlink-helper-functions)
43-
- [Adding Hyperlinks](#adding-hyperlinks)
44-
43+
- [Hyperlink Management](#hyperlink-management)
4544

4645
- [Tipps and Tricks](#tipps-and-tricks)
4746
- [Loop through the slides of a presentation](#loop-through-the-slides-of-a-presentation)
@@ -181,13 +180,16 @@ const automizer = new Automizer({
181180

182181
// use a callback function to track pptx generation process.
183182
// statusTracker: myStatusTracker,
184-
183+
185184
// Use 1 to show warnings or 2 for detailed information
186185
// 0 disables logging
187186
verbosity: 1,
188187

189188
// Remove all unused placeholders to prevent unwanted overlays:
190-
cleanupPlaceholders: false
189+
cleanupPlaceholders: false,
190+
191+
// Use a customized version of pptxGenJs if required:
192+
// pptxGenJs: PptxGenJS,
191193
});
192194

193195
// Now we can start and load a pptx template.
@@ -579,6 +581,64 @@ pres.addSlide('charts', 2, (slide) => {
579581
});
580582
```
581583

584+
## Generate shapes with PptxGenJs
585+
586+
This library wraps around the [PptxGenJS](https://github.com/gitbrent/PptxGenJS) to generate shapes from scratch. It is possible to use the `pptxGenJS` wrapper to generate shapes on a slide.
587+
588+
Here's an example of how to use `pptxGenJS` to add a text shape to a slide:
589+
```ts
590+
pres.addSlide('empty', 1, (slide) => {
591+
// Use pptxgenjs to add text from scratch:
592+
slide.generate((pptxGenJSSlide) => {
593+
pptxGenJSSlide.addText('Test 1', {
594+
x: 1,
595+
y: 1,
596+
h: 5,
597+
w: 10,
598+
color: '363636',
599+
});
600+
}, 'custom object name');
601+
});
602+
```
603+
604+
You can as well create charts with `pptxGenJS`:
605+
606+
```ts
607+
608+
const dataChartAreaLine = [
609+
{
610+
name: 'Actual Sales',
611+
labels: ['Jan', 'Feb', 'Mar'],
612+
values: [1500, 4600, 5156],
613+
},
614+
{
615+
name: 'Projected Sales',
616+
labels: ['Jan', 'Feb', 'Mar'],
617+
values: [1000, 2600, 3456],
618+
},
619+
];
620+
621+
pres.addSlide('empty', 1, (slide) => {
622+
// Use pptxgenjs to add generated contents from scratch:
623+
slide.generate((pSlide, pptxGenJs) => {
624+
pSlide.addChart(pptxGenJs.ChartType.line, dataChartAreaLine, {
625+
x: 1,
626+
y: 1,
627+
w: 8,
628+
h: 4,
629+
});
630+
});
631+
});
632+
```
633+
634+
You can use the following functions to generate shapes with `pptxGenJS`:
635+
* addChart
636+
* addImage
637+
* addShape
638+
* addTable
639+
* addText
640+
641+
582642
## Remove elements from a slide
583643

584644
You can as well remove elements from slides.
@@ -596,28 +656,18 @@ pres
596656
});
597657
```
598658

599-
## 🔗 Hyperlink Management
600-
601-
PowerPoint presentations often use hyperlinks to connect to external websites or internal slides. The PPTX Automizer provides simple and powerful functions to manage hyperlinks in your presentations.
602-
603-
### Hyperlink Helper Functions
659+
## Hyperlink Management
604660

605-
Three core functions are available for all your hyperlink needs:
661+
PowerPoint presentations often use hyperlinks to connect to external websites or internal slides. The `pptx-automizer` provides simple and powerful functions to manage hyperlinks in your presentations.
606662

607-
| Function | Description |
608-
|----------|-------------|
609-
| `addHyperlink` | Add a new hyperlink to an element |
663+
### Add Hyperlinks to existing shapes
610664

611-
612-
### Adding Hyperlinks
613-
614-
You can add hyperlinks to text elements using the `addHyperlink` helper function. The function accepts either a URL string for external links or a slide number for internal slide links:
665+
You can add hyperlinks to template text shapes using the `addHyperlink` helper function. The function accepts either a URL string for external links or a slide number for internal slide links:
615666

616667
```ts
617668
// Add an external hyperlink
618669
slide.modifyElement('TextShape', modify.addHyperlink('https://example.com'));
619670

620-
621671
// Add an internal slide link (to slide 3)
622672
slide.modifyElement('TextShape', (element, relation) => {
623673
modify.addHyperlink(3)(element, relation);
@@ -626,6 +676,36 @@ slide.modifyElement('TextShape', (element, relation) => {
626676

627677
The `addHyperlink` function will automatically detect whether the target is an external URL or an internal slide number and set up the appropriate relationship type and attributes.
628678

679+
### Create a new hyperlinked text shape with pptxGenJS
680+
681+
It is also possible to create a new hyperlink from scratch with the `pptxGenJS` wrapper. This is useful if you want to add hyperlinks to shapes that are not part of the template.
682+
683+
```ts
684+
// Generate a new text shape pointing to an external site
685+
slide.generate((pptxGenJSSlide) => {
686+
pptxGenJSSlide.addText(`External Link`, {
687+
hyperlink: { url: 'https://github.com' },
688+
x: 1,
689+
y: 1,
690+
w: 2.5,
691+
h: 0.5,
692+
fontSize: 12,
693+
});
694+
});
695+
696+
// Or generate an internal hyperlink
697+
slide.generate((pptxGenJSSlide) => {
698+
pptxGenJSSlide.addText(`Go to slide 3`, {
699+
hyperlink: { slide: 3 },
700+
x: 1,
701+
y: 1,
702+
w: 2.5,
703+
h: 0.5,
704+
fontSize: 12,
705+
});
706+
});
707+
```
708+
629709
# Tipps and Tricks
630710

631711
## Loop through the slides of a presentation
@@ -913,6 +993,7 @@ Take a look into [**tests**-directory](https://github.com/singerla/pptx-automize
913993
- [Update chart legend](https://github.com/singerla/pptx-automizer/blob/main/__tests__/modify-chart-legend.test.ts)
914994

915995
## Troubleshooting
996+
916997
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:
917998

918999
- **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.
@@ -921,7 +1002,7 @@ If you encounter problems when opening a `.pptx`-file modified by this library,
9211002
- **Proprietary/Binary contents** (e.g. ThinkCell): Walk through all slides, slideMasters and slideLayouts and seek for hidden Objects. Hit `ALT+F10` to toggle the sidebar.
9221003
- **Chart datasheet won't open** If you encounter an error message on opening a chart's datasheet, please make sure that the data table (blue bordered rectangle in worksheet view) of your template starts at cell A:1. If not, open worksheet in Excel mode and edit the table size in the table draft tab.
9231004

924-
If none of these could help, please don't hesitate to [talk about it](https://github.com/singerla/pptx-automizer/issues/new).
1005+
If none of these could help, please don't hesitate to [talk about it](https://github.com/singerla/pptx-automizer/issues/new).
9251006

9261007
## Testing
9271008

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Automizer, { modify } from '../src/index';
2+
3+
test('add charts with external data and mix with default charts', async () => {
4+
const automizer = new Automizer({
5+
templateDir: `${__dirname}/pptx-templates`,
6+
outputDir: `${__dirname}/pptx-output`,
7+
});
8+
9+
const pres = automizer
10+
.loadRoot(`RootTemplate.pptx`)
11+
.load(`ChartBarsStacked.pptx`, 'charts')
12+
.load(`ChartUserShape.pptx`, 'chartUserShape');
13+
14+
const dataSmaller = {
15+
series: [{ label: 'series s1' }, { label: 'series s2' }],
16+
categories: [
17+
{ label: 'item test r1', values: [10, 16] },
18+
{ label: 'item test r2', values: [12, 18] },
19+
],
20+
};
21+
22+
pres
23+
.addSlide('charts', 1, (slide) => {
24+
slide.modifyElement('BarsStacked', [modify.setChartData(dataSmaller)]);
25+
})
26+
.addSlide('charts', 1, (slide) => {
27+
slide.addElement('chartUserShape', 1, 'BarsStackedWithUserShape');
28+
});
29+
30+
const result = await pres.write(`add-chart-with-user-shape.test.pptx`);
31+
32+
expect(result.charts).toBe(4);
33+
});
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import Automizer, { modify } from '../src/index';
2+
3+
test('add charts with external data and mix with default charts', async () => {
4+
const automizer = new Automizer({
5+
templateDir: `${__dirname}/pptx-templates`,
6+
outputDir: `${__dirname}/pptx-output`,
7+
});
8+
9+
const pres = automizer
10+
.loadRoot(`RootTemplate.pptx`)
11+
.load(`ChartBarsStacked.pptx`, 'charts')
12+
.load(`ChartWithExternalData.pptx`, 'externalData');
13+
14+
const dataSmaller = {
15+
series: [{ label: 'series s1' }, { label: 'series s2' }],
16+
categories: [
17+
{ label: 'item test r1', values: [10, 16] },
18+
{ label: 'item test r2', values: [12, 18] },
19+
],
20+
};
21+
22+
pres
23+
.addSlide('externalData', 1)
24+
.addSlide('externalData', 1, (slide) => {
25+
slide.removeElement('Gráfico 7');
26+
slide.addElement('charts', 1, 'BarsStacked');
27+
})
28+
.addSlide('charts', 1, (slide) => {
29+
slide.modifyElement('BarsStacked', [modify.setChartData(dataSmaller)]);
30+
})
31+
.addSlide('charts', 1, (slide) => {
32+
slide.removeElement('BarsStacked');
33+
// Add chart with external data to another slide
34+
slide.addElement('externalData', 1, 'Gráfico 7');
35+
});
36+
37+
const result = await pres.write(`add-chart-without-worksheet.test.pptx`);
38+
39+
expect(result.charts).toBe(9);
40+
});

0 commit comments

Comments
 (0)