Skip to content

Commit 35a4c63

Browse files
committed
fix: use prop in instead of .hasOwn(prop) & add Drawings tests
1 parent 47f7ac8 commit 35a4c63

File tree

4 files changed

+63
-9
lines changed

4 files changed

+63
-9
lines changed

packages/excel-builder-vanilla/src/Excel/StyleSheet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ export class StyleSheet {
677677
const node = doc.createElement('protection');
678678
// eslint-disable-next-line no-restricted-syntax
679679
for (const k in protectionData) {
680-
if (protectionData.hasOwn(k)) {
680+
if (k in protectionData) {
681681
node.setAttribute(k, protectionData[k]);
682682
}
683683
}

packages/excel-builder-vanilla/src/Excel/Workbook.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class Workbook {
8989
this.printTitles[inSheet].left = String.fromCharCode(64 + inRowCount);
9090
}
9191

92-
addMedia(_type: string, fileName: string, fileData: any, contentType: any) {
92+
addMedia(_type: string, fileName: string, fileData: any, contentType?: string | null) {
9393
const fileNamePieces = fileName.split('.');
9494
const extension = fileNamePieces[fileNamePieces.length - 1];
9595
if (!contentType) {
@@ -148,12 +148,12 @@ export class Workbook {
148148

149149
const extensions: any = {};
150150
for (const filename in this.media) {
151-
if (this.media.hasOwn(filename)) {
151+
if (filename in this.media) {
152152
extensions[this.media[filename].extension] = this.media[filename].contentType;
153153
}
154154
}
155155
for (const extension in extensions) {
156-
if (extensions.hasOwn(extension)) {
156+
if (extension in extensions) {
157157
types.appendChild(
158158
Util.createElement(doc, 'Default', [
159159
['Extension', extension],
@@ -293,7 +293,7 @@ export class Workbook {
293293
}
294294

295295
for (const fileName in this.media) {
296-
if (this.media.hasOwn(fileName)) {
296+
if (fileName in this.media) {
297297
const media = this.media[fileName];
298298
files[`/xl/media/${fileName}`] = media.data;
299299
Paths[fileName] = `/xl/media/${fileName}`;

packages/excel-builder-vanilla/src/Excel/Worksheet.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ export class Worksheet {
111111
this.relations.addRelation(table, 'table');
112112
}
113113

114-
addDrawings(table: Table) {
115-
this._drawings.push(table);
116-
this.relations.addRelation(table, 'drawingRelationship');
114+
addDrawings(drawings: Drawings) {
115+
this._drawings.push(drawings);
116+
this.relations.addRelation(drawings, 'drawingRelationship');
117117
}
118118

119119
setRowInstructions(rowIndex: number, instructions: ExcelStyleInstruction) {

packages/excel-builder-vanilla/src/__tests__/excel-builder.spec.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, test } from 'vitest';
22

3-
import { Table, Workbook } from '../Excel';
3+
import { Drawings, Picture, Positioning, Table, Workbook } from '../Excel';
44
import { createWorkbook } from '../factory';
55

66
describe('Excel-Builder-Vanilla', () => {
@@ -1026,4 +1026,58 @@ describe('Excel-Builder-Vanilla', () => {
10261026
'xmlns:mc': 'http://schemas.openxmlformats.org/markup-compatibility/2006',
10271027
});
10281028
});
1029+
1030+
test('Drawings', async () => {
1031+
const fruitWorkbook = createWorkbook();
1032+
const berryList = fruitWorkbook.createWorksheet({ name: 'Berry List' });
1033+
const picRef = fruitWorkbook.addMedia('image', 'text.txt', new Blob());
1034+
1035+
const drawings = new Drawings();
1036+
const strawberryPicture1 = new Picture();
1037+
strawberryPicture1.createAnchor('twoCellAnchor', {
1038+
from: {
1039+
x: 0,
1040+
y: 0,
1041+
},
1042+
to: {
1043+
x: 3,
1044+
y: 3,
1045+
},
1046+
});
1047+
1048+
strawberryPicture1.setMedia(picRef);
1049+
drawings.addDrawing(strawberryPicture1);
1050+
1051+
const strawberryPicture2 = new Picture();
1052+
strawberryPicture2.createAnchor('absoluteAnchor', {
1053+
x: Positioning.pixelsToEMUs(300),
1054+
y: Positioning.pixelsToEMUs(300),
1055+
width: Positioning.pixelsToEMUs(300),
1056+
height: Positioning.pixelsToEMUs(300),
1057+
});
1058+
1059+
strawberryPicture2.setMedia(picRef);
1060+
drawings.addDrawing(strawberryPicture2);
1061+
1062+
const strawberryPicture3 = new Picture();
1063+
strawberryPicture3.createAnchor('oneCellAnchor', {
1064+
x: 1,
1065+
y: 4,
1066+
width: Positioning.pixelsToEMUs(300),
1067+
height: Positioning.pixelsToEMUs(300),
1068+
});
1069+
1070+
strawberryPicture3.setMedia(picRef);
1071+
drawings.addDrawing(strawberryPicture3);
1072+
1073+
berryList.addDrawings(drawings);
1074+
fruitWorkbook.addDrawings(drawings);
1075+
fruitWorkbook.addWorksheet(berryList);
1076+
1077+
const file = await fruitWorkbook.generateFiles();
1078+
const dwgs = fruitWorkbook.drawings;
1079+
1080+
expect(file).toBeTruthy();
1081+
expect(dwgs[0].drawings.length).toBe(3);
1082+
});
10291083
});

0 commit comments

Comments
 (0)