Skip to content

Commit 18d246c

Browse files
authored
Merge pull request #58 from ghiscoding/bugfix/hasown
fix: use `prop in` instead of `.hasOwn(prop)` & add Drawings tests
2 parents 47f7ac8 + 28aa08c commit 18d246c

File tree

4 files changed

+98
-27
lines changed

4 files changed

+98
-27
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: 21 additions & 22 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],
@@ -240,27 +240,26 @@ export class Workbook {
240240
const definedNames = Util.createElement(doc, 'definedNames');
241241
let ctr = 0;
242242
for (const name in this.printTitles) {
243-
if (!this.printTitles.hasOwn(name)) {
244-
continue;
245-
}
246-
const entry = this.printTitles[name];
247-
const definedName = doc.createElement('definedName');
248-
definedName.setAttribute('name', '_xlnm.Print_Titles');
249-
definedName.setAttribute('localSheetId', ctr++);
250-
251-
let value = '';
252-
if (entry.top) {
253-
value += `${name}!$1:$${entry.top}`;
243+
if (name in this.printTitles) {
244+
const entry = this.printTitles[name];
245+
const definedName = doc.createElement('definedName');
246+
definedName.setAttribute('name', '_xlnm.Print_Titles');
247+
definedName.setAttribute('localSheetId', ctr++);
248+
249+
let value = '';
250+
if (entry.top) {
251+
value += `${name}!$1:$${entry.top}`;
252+
if (entry.left) {
253+
value += ',';
254+
}
255+
}
254256
if (entry.left) {
255-
value += ',';
257+
value += `${name}!$A:$${entry.left}`;
256258
}
257-
}
258-
if (entry.left) {
259-
value += `${name}!$A:$${entry.left}`;
260-
}
261259

262-
definedName.appendChild(doc.createTextNode(value));
263-
definedNames.appendChild(definedName);
260+
definedName.appendChild(doc.createTextNode(value));
261+
definedNames.appendChild(definedName);
262+
}
264263
}
265264
wb.appendChild(definedNames);
266265

@@ -293,7 +292,7 @@ export class Workbook {
293292
}
294293

295294
for (const fileName in this.media) {
296-
if (this.media.hasOwn(fileName)) {
295+
if (fileName in this.media) {
297296
const media = this.media[fileName];
298297
files[`/xl/media/${fileName}`] = media.data;
299298
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: 73 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,76 @@ 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 picRef1 = fruitWorkbook.addMedia('image', 'file1.jpeg', new Blob());
1034+
const picRef2 = fruitWorkbook.addMedia('image', 'file2.gif', new Blob());
1035+
const picRef3 = fruitWorkbook.addMedia('image', 'file3.png', new Blob());
1036+
const picRef4 = fruitWorkbook.addMedia('image', 'file4.txt', new Blob());
1037+
1038+
expect(picRef1.contentType).toBe('image/jpeg');
1039+
expect(picRef2.contentType).toBe('image/gif');
1040+
expect(picRef3.contentType).toBe('image/png');
1041+
expect(picRef4.contentType).toBe(null);
1042+
1043+
const drawings = new Drawings();
1044+
const strawberryPicture1 = new Picture();
1045+
strawberryPicture1.createAnchor('twoCellAnchor', {
1046+
from: {
1047+
x: 0,
1048+
y: 0,
1049+
},
1050+
to: {
1051+
x: 3,
1052+
y: 3,
1053+
},
1054+
});
1055+
1056+
strawberryPicture1.setMedia(picRef1);
1057+
drawings.addDrawing(strawberryPicture1);
1058+
1059+
const strawberryPicture2 = new Picture();
1060+
strawberryPicture2.createAnchor('absoluteAnchor', {
1061+
x: Positioning.pixelsToEMUs(300),
1062+
y: Positioning.pixelsToEMUs(300),
1063+
width: Positioning.pixelsToEMUs(300),
1064+
height: Positioning.pixelsToEMUs(300),
1065+
});
1066+
1067+
strawberryPicture2.setMedia(picRef1);
1068+
drawings.addDrawing(strawberryPicture2);
1069+
1070+
const strawberryPicture3 = new Picture();
1071+
strawberryPicture3.createAnchor('oneCellAnchor', {
1072+
x: 1,
1073+
y: 4,
1074+
width: Positioning.pixelsToEMUs(300),
1075+
height: Positioning.pixelsToEMUs(300),
1076+
});
1077+
1078+
strawberryPicture3.setMedia(picRef1);
1079+
drawings.addDrawing(strawberryPicture3);
1080+
1081+
berryList.addDrawings(drawings);
1082+
fruitWorkbook.addDrawings(drawings);
1083+
fruitWorkbook.addWorksheet(berryList);
1084+
1085+
const file = await fruitWorkbook.generateFiles();
1086+
const dwgs = fruitWorkbook.drawings;
1087+
1088+
expect(file).toBeTruthy();
1089+
expect(dwgs[0].drawings.length).toBe(3);
1090+
1091+
// print titles offset of 2 => left B and top 2
1092+
fruitWorkbook.setPrintTitleLeft('sheet1', 2);
1093+
fruitWorkbook.setPrintTitleTop('sheet1', 2);
1094+
1095+
const titles = fruitWorkbook.printTitles;
1096+
expect(titles).toEqual({ sheet1: { left: 'B', top: 2 } });
1097+
1098+
const wsXML = fruitWorkbook.toXML();
1099+
expect(wsXML.documentElement.children.length).toBe(2);
1100+
});
10291101
});

0 commit comments

Comments
 (0)