Skip to content

Commit 22b2b26

Browse files
committed
feature(chart): support charts w/o style#.xml/colors#.xml
1 parent 34beab3 commit 22b2b26

File tree

1 file changed

+58
-20
lines changed

1 file changed

+58
-20
lines changed

src/shapes/chart.ts

Lines changed: 58 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export class Chart extends Shape implements IChart {
1414
worksheetFilePrefix: string;
1515
wbEmbeddingsPath: string;
1616
wbExtension: string;
17+
relTypeChartColorStyle: string;
18+
relTypeChartStyle: string;
19+
wbRelsPath: string;
20+
styleRelationFiles: {
21+
[key: string]: string;
22+
};
1723

1824
constructor(shape: ImportedElement) {
1925
super(shape);
@@ -25,6 +31,11 @@ export class Chart extends Shape implements IChart {
2531

2632
this.wbEmbeddingsPath = `../embeddings/`;
2733
this.wbExtension = '.xlsx';
34+
this.relTypeChartColorStyle =
35+
'http://schemas.microsoft.com/office/2011/relationships/chartColorStyle';
36+
this.relTypeChartStyle =
37+
'http://schemas.microsoft.com/office/2011/relationships/chartStyle';
38+
this.styleRelationFiles = {};
2839
}
2940

3041
async modify(
@@ -66,8 +77,10 @@ export class Chart extends Shape implements IChart {
6677
await this.setTarget(targetTemplate, targetSlideNumber);
6778

6879
this.targetNumber = this.targetTemplate.incrementCounter('charts');
80+
this.wbRelsPath = `ppt/charts/_rels/chart${this.sourceNumber}.xml.rels`;
6981

7082
await this.copyFiles();
83+
await this.copyChartStyleFiles();
7184
await this.appendTypes();
7285
await this.appendToSlideRels();
7386
}
@@ -154,13 +167,13 @@ export class Chart extends Shape implements IChart {
154167
async copyFiles(): Promise<void> {
155168
await this.copyChartFiles();
156169

157-
const wbRelsPath = `ppt/charts/_rels/chart${this.sourceNumber}.xml.rels`;
158-
159-
this.worksheetFilePrefix = await this.getWorksheetFilePrefix(wbRelsPath);
170+
this.worksheetFilePrefix = await this.getWorksheetFilePrefix(
171+
this.wbRelsPath,
172+
);
160173

161174
const worksheets = await XmlHelper.getTargetsFromRelationships(
162175
this.sourceArchive,
163-
wbRelsPath,
176+
this.wbRelsPath,
164177
`${this.wbEmbeddingsPath}${this.worksheetFilePrefix}`,
165178
this.wbExtension,
166179
);
@@ -205,20 +218,6 @@ export class Chart extends Shape implements IChart {
205218
`ppt/charts/chart${this.targetNumber}.xml`,
206219
);
207220

208-
await FileHelper.zipCopy(
209-
this.sourceArchive,
210-
`ppt/charts/colors${this.sourceNumber}.xml`,
211-
this.targetArchive,
212-
`ppt/charts/colors${this.targetNumber}.xml`,
213-
);
214-
215-
await FileHelper.zipCopy(
216-
this.sourceArchive,
217-
`ppt/charts/style${this.sourceNumber}.xml`,
218-
this.targetArchive,
219-
`ppt/charts/style${this.targetNumber}.xml`,
220-
);
221-
222221
await FileHelper.zipCopy(
223222
this.sourceArchive,
224223
`ppt/charts/_rels/chart${this.sourceNumber}.xml.rels`,
@@ -227,6 +226,45 @@ export class Chart extends Shape implements IChart {
227226
);
228227
}
229228

229+
async copyChartStyleFiles(): Promise<void> {
230+
await this.getChartStyles();
231+
232+
if (this.styleRelationFiles.relTypeChartStyle) {
233+
await FileHelper.zipCopy(
234+
this.sourceArchive,
235+
`ppt/charts/${this.styleRelationFiles.relTypeChartStyle}`,
236+
this.targetArchive,
237+
`ppt/charts/style${this.targetNumber}.xml`,
238+
);
239+
}
240+
241+
if (this.styleRelationFiles.relTypeChartColorStyle) {
242+
await FileHelper.zipCopy(
243+
this.sourceArchive,
244+
`ppt/charts/${this.styleRelationFiles.relTypeChartColorStyle}`,
245+
this.targetArchive,
246+
`ppt/charts/colors${this.targetNumber}.xml`,
247+
);
248+
}
249+
}
250+
251+
async getChartStyles(): Promise<void> {
252+
const styleTypes = ['relTypeChartStyle', 'relTypeChartColorStyle'];
253+
254+
for (const i in styleTypes) {
255+
const styleType = styleTypes[i];
256+
const styleRelation = await XmlHelper.getTargetsByRelationshipType(
257+
this.sourceArchive,
258+
this.wbRelsPath,
259+
this[styleType],
260+
);
261+
262+
if (styleRelation.length) {
263+
this.styleRelationFiles[styleType] = styleRelation[0].file;
264+
}
265+
}
266+
}
267+
230268
async appendToSlideRels(): Promise<HelperElement> {
231269
this.createdRid = await XmlHelper.getNextRelId(
232270
this.targetArchive,
@@ -268,10 +306,10 @@ export class Chart extends Shape implements IChart {
268306
`${this.wbEmbeddingsPath}${this.worksheetFilePrefix}${this.targetWorksheet}${this.wbExtension}`,
269307
);
270308
break;
271-
case 'http://schemas.microsoft.com/office/2011/relationships/chartColorStyle':
309+
case this.relTypeChartColorStyle:
272310
element.setAttribute('Target', `colors${this.targetNumber}.xml`);
273311
break;
274-
case 'http://schemas.microsoft.com/office/2011/relationships/chartStyle':
312+
case this.relTypeChartStyle:
275313
element.setAttribute('Target', `style${this.targetNumber}.xml`);
276314
break;
277315
}

0 commit comments

Comments
 (0)