Skip to content

Commit 020a456

Browse files
committed
chore(chart): minor fixes
1 parent d8395ba commit 020a456

File tree

3 files changed

+71
-151
lines changed

3 files changed

+71
-151
lines changed

src/classes/slide.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,10 @@ export class Slide implements ISlide {
464464

465465
this.unsupportedTags.forEach((tag) => {
466466
const drop = xml.getElementsByTagName(tag);
467-
if (drop.length) {
467+
const length = drop.length;
468+
if (length && length > 0) {
468469
XmlHelper.sliceCollection(drop, 0);
469-
console.log(
470-
'Removed ' + drop.length + ' unsupported ' + tag + ' elements.',
471-
);
470+
console.log('Removed ' + length + ' unsupported ' + tag + ' elements.');
472471
}
473472
});
474473
await XmlHelper.writeXmlToArchive(this.targetArchive, this.targetPath, xml);

src/dev.ts

Lines changed: 31 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -8,129 +8,46 @@ const automizer = new Automizer({
88
});
99

1010
const run = async () => {
11-
const data1 = {
12-
body: [
13-
{
14-
label: 'item test r1',
15-
values: ['test1', 10, 16, 12, 11, 15],
16-
styles: [
17-
null,
18-
<TableRowStyle>{
19-
color: {
20-
type: 'srgbClr',
21-
value: 'ff0000',
22-
},
23-
size: 1400,
24-
},
25-
],
26-
},
27-
{ label: 'item test r2', values: ['test2', 12, 18, 15, 12, 15] },
28-
{
29-
label: 'item test r3',
30-
values: ['test3', 14, 12, 11, 14, 15],
31-
styles: [
32-
null,
33-
null,
34-
null,
35-
null,
36-
<TableRowStyle>{
37-
color: {
38-
type: 'srgbClr',
39-
value: 'ff0000',
40-
},
41-
size: 1400,
42-
},
43-
// <TableRowStyle>{
44-
// color: {
45-
// type: 'srgbClr',
46-
// value: '00ff00',
47-
// },
48-
// size: 1400,
49-
// },
50-
],
51-
},
52-
{
53-
label: 'item test r4',
54-
values: ['test1', 10, 16, 12, 11, 15],
55-
styles: [
56-
null,
57-
<TableRowStyle>{
58-
color: {
59-
type: 'srgbClr',
60-
value: 'ff0000',
61-
},
62-
size: 1400,
63-
},
64-
],
65-
},
66-
{
67-
label: 'item test r5',
68-
values: ['test1', 'r5', 16, 12, 11, 15],
69-
styles: [],
70-
},
71-
{
72-
label: 'item test r6',
73-
values: ['test1', 'r6', 16, 12, 11, 15],
74-
styles: [],
75-
},
11+
const pres = automizer
12+
.loadRoot(`ChartLinesVerticalImageMarkers.pptx`)
13+
.load(`ChartLinesVerticalImageMarkers.pptx`, 'charts');
14+
15+
const data = {
16+
series: [
17+
{ label: 'series s1' },
18+
{ label: 'series s2' },
19+
{ label: 'series s3' },
7620
],
77-
};
78-
79-
const data2 = {
80-
body: [
81-
{ label: 'item test r1', values: ['test1', 10, 16, 12] },
82-
{ label: 'item test r2', values: ['test2', 12, 18, 15] },
83-
{ label: 'item test r3', values: ['test3', 14, 12, 11] },
84-
{ label: 'item test r4', values: ['test4', 14, 12, 18] },
85-
{ label: 'item test r5', values: ['test5', 14, 13, 15] },
86-
{ label: 'item test r6', values: ['test6', 999, 14, 14] },
87-
{ label: 'item test r7', values: ['test7', 998, 15, 13] },
88-
{ label: 'item test r8', values: ['test8', 997, 16, 19] },
89-
{ label: 'item test r9', values: ['test9', 996, 17, 18] },
21+
categories: [
22+
{ label: 'item test r1', y: 10, values: [10, 16, 12] },
23+
{ label: 'item test r2', y: 9, values: [12, 18, 15] },
24+
{ label: 'item test r3', y: 8, values: [14, 12, 11] },
25+
{ label: 'item test r4', y: 7, values: [8, 11, 9] },
26+
{ label: 'item test r5', y: 6, values: [6, 15, 7] },
27+
{ label: 'item test r6', y: 5, values: [16, 16, 9] },
28+
{ label: 'item test r7', y: 4, values: [10, 13, 12] },
29+
{ label: 'item test r8', y: 3, values: [11, 12, 14] },
30+
{ label: 'item test r9', y: 2, values: [9, 7, 11] },
31+
{ label: 'item test r10', y: 1, values: [7, 5, 17] },
9032
],
9133
};
9234

93-
const data3 = {
94-
body: [
95-
<TableRow>{
96-
label: 'item test r1',
97-
values: ['test1', 10, 16],
98-
styles: [
99-
null,
100-
<TableRowStyle>{
101-
color: {
102-
type: 'srgbClr',
103-
value: 'ff0000',
104-
},
105-
size: 1400,
106-
},
107-
],
108-
},
109-
{ label: 'item test r2', values: ['test2', 12, 18] },
110-
{ label: 'item test r3', values: ['test3', 14, 12] },
35+
const dataSmaller = {
36+
series: [{ label: 'series s1' }],
37+
categories: [
38+
{ label: 'item test r1', y: 10, values: [10] },
39+
{ label: 'item test r2', y: 9, values: [12] },
40+
{ label: 'item test r3', y: 8, values: [14] },
11141
],
11242
};
11343

114-
const pres = automizer
115-
.loadRoot(`SlideWithTables.pptx`)
116-
.load(`SlideWithTables.pptx`, 'tables');
117-
11844
const result = await pres
119-
.addSlide('tables', 1, (slide) => {
120-
slide.modifyElement('TableDefault', [modify.setTable(data1)]);
121-
122-
// slide.modifyElement('TableWithLabels', [
123-
// modify.setTable(data2),
124-
// // modify.dump
125-
// ]);
126-
//
127-
// slide.modifyElement('TableWithHeader', [
128-
// modify.setTableData(data3),
129-
// modify.adjustHeight(data3),
130-
// modify.adjustWidth(data3),
131-
// ]);
45+
.addSlide('charts', 1, (slide) => {
46+
slide.modifyElement('DotMatrixImageMarkers', [
47+
modify.setChartVerticalLines(dataSmaller),
48+
]);
13249
})
133-
.write(`modify-existing-table.test.pptx`);
50+
.write(`modify-chart-vertical-lines-marker.test.pptx`);
13451
};
13552

13653
run().catch((error) => {

src/shapes/chart.ts

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class Chart extends Shape implements IChart {
2121
relTypeChartImage: string;
2222
wbRelsPath: string;
2323
styleRelationFiles: {
24-
[key: string]: string;
24+
[key: string]: string[];
2525
};
2626

2727
constructor(shape: ImportedElement) {
@@ -234,54 +234,39 @@ export class Chart extends Shape implements IChart {
234234
async copyChartStyleFiles(): Promise<void> {
235235
await this.getChartStyles();
236236

237-
if (this.styleRelationFiles.relTypeChartStyle) {
237+
if (this.styleRelationFiles.relTypeChartStyle?.length) {
238238
await FileHelper.zipCopy(
239239
this.sourceArchive,
240-
`ppt/charts/${this.styleRelationFiles.relTypeChartStyle}`,
240+
`ppt/charts/${this.styleRelationFiles.relTypeChartStyle[0]}`,
241241
this.targetArchive,
242242
`ppt/charts/style${this.targetNumber}.xml`,
243243
);
244244
}
245245

246-
if (this.styleRelationFiles.relTypeChartColorStyle) {
246+
if (this.styleRelationFiles.relTypeChartColorStyle?.length) {
247247
await FileHelper.zipCopy(
248248
this.sourceArchive,
249-
`ppt/charts/${this.styleRelationFiles.relTypeChartColorStyle}`,
249+
`ppt/charts/${this.styleRelationFiles.relTypeChartColorStyle[0]}`,
250250
this.targetArchive,
251251
`ppt/charts/colors${this.targetNumber}.xml`,
252252
);
253253
}
254254

255255
if (this.styleRelationFiles.relTypeChartImage) {
256-
const imageInfo = await this.getTargetChartImageUri(
257-
this.styleRelationFiles.relTypeChartImage,
258-
);
259-
await this.appendImageExtensionToContentType(imageInfo.extension);
260-
await FileHelper.zipCopy(
261-
this.sourceArchive,
262-
imageInfo.source,
263-
this.targetArchive,
264-
imageInfo.target,
265-
);
256+
for (const relTypeChartImage of this.styleRelationFiles
257+
.relTypeChartImage) {
258+
const imageInfo = await this.getTargetChartImageUri(relTypeChartImage);
259+
await this.appendImageExtensionToContentType(imageInfo.extension);
260+
await FileHelper.zipCopy(
261+
this.sourceArchive,
262+
imageInfo.source,
263+
this.targetArchive,
264+
imageInfo.target,
265+
);
266+
}
266267
}
267268
}
268269

269-
getTargetChartImageUri(origin: string): {
270-
source: string;
271-
target: string;
272-
rel: string;
273-
extension: string;
274-
} {
275-
const file = origin.replace('../media/', '');
276-
const extension = path.extname(file).replace('.', '');
277-
return {
278-
source: `ppt/media/${file}`,
279-
target: `ppt/media/${file}-chart-${this.targetNumber}.${extension}`,
280-
rel: `../media/${file}-chart-${this.targetNumber}.${extension}`,
281-
extension: extension,
282-
};
283-
}
284-
285270
async getChartStyles(): Promise<void> {
286271
const styleTypes = [
287272
'relTypeChartStyle',
@@ -297,8 +282,12 @@ export class Chart extends Shape implements IChart {
297282
this[styleType],
298283
);
299284

285+
this.styleRelationFiles[styleType] =
286+
this.styleRelationFiles[styleType] || [];
300287
if (styleRelation.length) {
301-
this.styleRelationFiles[styleType] = styleRelation[0].file;
288+
styleRelation.forEach((styleRelation) => {
289+
this.styleRelationFiles[styleType].push(styleRelation.file);
290+
});
302291
}
303292
}
304293
}
@@ -352,7 +341,6 @@ export class Chart extends Shape implements IChart {
352341
case this.relTypeChartImage:
353342
const target = element.getAttribute('Target');
354343
const imageInfo = this.getTargetChartImageUri(target);
355-
vd(imageInfo);
356344
element.setAttribute('Target', imageInfo.rel);
357345
break;
358346
}
@@ -365,6 +353,22 @@ export class Chart extends Shape implements IChart {
365353
);
366354
}
367355

356+
getTargetChartImageUri(origin: string): {
357+
source: string;
358+
target: string;
359+
rel: string;
360+
extension: string;
361+
} {
362+
const file = origin.replace('../media/', '');
363+
const extension = path.extname(file).replace('.', '');
364+
return {
365+
source: `ppt/media/${file}`,
366+
target: `ppt/media/${file}-chart-${this.targetNumber}.${extension}`,
367+
rel: `../media/${file}-chart-${this.targetNumber}.${extension}`,
368+
extension: extension,
369+
};
370+
}
371+
368372
async copyWorksheetFile(): Promise<void> {
369373
await FileHelper.zipCopy(
370374
this.sourceArchive,

0 commit comments

Comments
 (0)