Skip to content

feat: header and footer support image #2563

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,12 @@ export interface Worksheet {
range: ImageRange;
}>;

/**
* footer heaedr
*/
addHFImage(imageId: number, options: { id: string, width: string, height: string }): void


commit(): void;

model: WorksheetModel;
Expand Down
46 changes: 39 additions & 7 deletions lib/doc/worksheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class Worksheet {
options.headerFooter
);

this.hfImages = [];

this.dataValidations = new DataValidations();

// for freezepanes, split, zoom, gridlines, etc
Expand Down Expand Up @@ -156,11 +158,15 @@ class Worksheet {
// Illegal character in worksheet name: asterisk (*), question mark (?),
// colon (:), forward slash (/ \), or bracket ([])
if (/[*?:/\\[\]]/.test(name)) {
throw new Error(`Worksheet name ${name} cannot include any of the following characters: * ? : \\ / [ ]`);
throw new Error(
`Worksheet name ${name} cannot include any of the following characters: * ? : \\ / [ ]`
);
}

if (/(^')|('$)/.test(name)) {
throw new Error(`The first or last character of worksheet name cannot be a single quotation mark: ${name}`);
throw new Error(
`The first or last character of worksheet name cannot be a single quotation mark: ${name}`
);
}

if (name && name.length > 31) {
Expand Down Expand Up @@ -529,7 +535,9 @@ class Worksheet {
if (cell._value.constructor.name === 'MergeValue') {
const cellToBeMerged = this.getRow(cell._row._number + nInserts).getCell(colNumber);
const prevMaster = cell._value._master;
const newMaster = this.getRow(prevMaster._row._number + nInserts).getCell(prevMaster._column._number);
const newMaster = this.getRow(prevMaster._row._number + nInserts).getCell(
prevMaster._column._number
);
cellToBeMerged.merge(newMaster);
}
});
Expand Down Expand Up @@ -751,6 +759,21 @@ class Worksheet {
return image && image.imageId;
}

addHFImage(imageId, options) {
const model = {
type: 'hfimage',
id: options.id,
style: {
width: options.width,
height: options.height,
},
imagedata: {
index: imageId,
},
};
this.hfImages.push(model);
}

// =========================================================================
// Worksheet Protection
protect(password, options) {
Expand All @@ -762,12 +785,15 @@ class Worksheet {
};
if (options && 'spinCount' in options) {
// force spinCount to be integer >= 0
options.spinCount = Number.isFinite(options.spinCount) ? Math.round(Math.max(0, options.spinCount)) : 100000;
options.spinCount = Number.isFinite(options.spinCount)
? Math.round(Math.max(0, options.spinCount))
: 100000;
}
if (password) {
this.sheetProtection.algorithmName = 'SHA-512';
this.sheetProtection.saltValue = Encryptor.randomBytes(16).toString('base64');
this.sheetProtection.spinCount = options && 'spinCount' in options ? options.spinCount : 100000; // allow user specified spinCount
this.sheetProtection.spinCount =
options && 'spinCount' in options ? options.spinCount : 100000; // allow user specified spinCount
this.sheetProtection.hashValue = Encryptor.convertPasswordToHash(
password,
'SHA512',
Expand Down Expand Up @@ -846,13 +872,17 @@ Please leave feedback at https://github.com/exceljs/exceljs/discussions/2575`
// Deprecated
get tabColor() {
// eslint-disable-next-line no-console
console.trace('worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor');
console.trace(
'worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor'
);
return this.properties.tabColor;
}

set tabColor(value) {
// eslint-disable-next-line no-console
console.trace('worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor');
console.trace(
'worksheet.tabColor property is now deprecated. Please use worksheet.properties.tabColor'
);
this.properties.tabColor = value;
}

Expand All @@ -868,6 +898,7 @@ Please leave feedback at https://github.com/exceljs/exceljs/discussions/2575`
state: this.state,
pageSetup: this.pageSetup,
headerFooter: this.headerFooter,
hfImages: this.hfImages,
rowBreaks: this.rowBreaks,
views: this.views,
autoFilter: this.autoFilter,
Expand Down Expand Up @@ -943,6 +974,7 @@ Please leave feedback at https://github.com/exceljs/exceljs/discussions/2575`
}, {});
this.pivotTables = value.pivotTables;
this.conditionalFormattings = value.conditionalFormattings;
this.hfImages = value.hfImages || [];
}
}

Expand Down
3 changes: 3 additions & 0 deletions lib/xlsx/xform/comment/vml-client-data-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class VmlClientDataXform extends BaseXform {
}

render(xmlStream, model) {
if (!model.note) {
return;
}
const {protection, editAs} = model.note;
xmlStream.openNode(this.tag, {ObjectType: 'Note'});
this.map['x:MoveWithCells'].render(xmlStream, editAs, POSITION_TYPE);
Expand Down
1 change: 1 addition & 0 deletions lib/xlsx/xform/comment/vml-shape-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class VmlShapeXform extends BaseXform {
anchor: '',
editAs: '',
protection: {},
attributes: node.attributes,
};
break;
default:
Expand Down
7 changes: 7 additions & 0 deletions lib/xlsx/xform/core/content-types-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ class ContentTypesXform extends BaseXform {
});
}

if (model.vmlDrawings && model.vmlDrawings.length > 0 && !model.commentRefs) {
xmlStream.leafNode('Default', {
Extension: 'vml',
ContentType: 'application/vnd.openxmlformats-officedocument.vmlDrawing',
});
}

xmlStream.leafNode('Override', {
PartName: '/docProps/core.xml',
ContentType: 'application/vnd.openxmlformats-package.core-properties+xml',
Expand Down
2 changes: 1 addition & 1 deletion lib/xlsx/xform/core/relationship-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const BaseXform = require('../base-xform');

class RelationshipXform extends BaseXform {
render(xmlStream, model) {
xmlStream.leafNode('Relationship', model);
xmlStream.leafNode('Relationship', {...model, isHFRel:undefined});
}

parseOpen(node) {
Expand Down
39 changes: 39 additions & 0 deletions lib/xlsx/xform/drawing/vlm-drawing/o-lock-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const BaseXform = require('../../base-xform');

class OLockform extends BaseXform {
get tag() {
return 'o:lock';
}

render(xmlStream, model) {
xmlStream.leafNode(this.tag, {
'v:ext': 'edit',
rotation: 't',
aspectratio: 't',
});
}

parseOpen(node) {
switch (node.name) {
case this.tag:
this.model = {};
return true;
default:
return true;
}
}

parseText() {}

parseClose(name) {
switch (name) {
case this.tag:
return false;
default:
// unprocessed internal nodes
return true;
}
}
}

module.exports = OLockform;
110 changes: 110 additions & 0 deletions lib/xlsx/xform/drawing/vlm-drawing/vml-drawing-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
const XmlStream = require('../../../../utils/xml-stream');
const VmlShapeXform = require('./vml-shap-xform');
const BaseXform = require('../../base-xform');

class VmlDrawingXform extends BaseXform {
constructor() {
super();
this.map = {
'v:shape': new VmlShapeXform(),
};
}

get tag() {
return 'xml';
}

render(xmlStream, model) {
xmlStream.openXml(XmlStream.StdDocAttributes);
xmlStream.openNode(this.tag, VmlDrawingXform.DRAWING_ATTRIBUTES);

xmlStream.openNode('o:shapelayout', {'v:ext': 'edit'});
xmlStream.leafNode('o:idmap', {'v:ext': 'edit', data: 1});
xmlStream.closeNode();

xmlStream.openNode('v:shapetype', {
id: '_x0000_t202',
coordsize: '21600,21600',
'o:spt': 202,
path: 'm,l,21600r21600,l21600,xe',
});
xmlStream.leafNode('v:stroke', {joinstyle: 'miter'});
xmlStream.leafNode('v:path', {
gradientshapeok: 't',
'o:connecttype': 'rect',
});
xmlStream.closeNode();

[...(model.comments || []), ...(model.hfImages || [])].forEach((item, index) => {
this.map['v:shape'].render(xmlStream, item, index);
});

xmlStream.closeNode();
}

parseOpen(node) {
if (this.parser) {
this.parser.parseOpen(node);
return true;
}
switch (node.name) {
case this.tag:
this.reset();
this.model = {
comments: [],
hfImages: [],
};
break;
default:
this.parser = this.map[node.name];
if (this.parser) {
this.parser.parseOpen(node);
}
break;
}
return true;
}

parseText(text) {
if (this.parser) {
this.parser.parseText(text);
}
}

parseClose(name) {
if (this.parser) {
if (!this.parser.parseClose(name)) {
if (this.parser.model.type === 'hfimage') {
this.model.hfImages.push(this.parser.model);
} else {
this.model.comments.push(this.parser.model);
}
this.parser = undefined;
}
return true;
}
switch (name) {
case this.tag:
return false;
default:
// could be some unrecognised tags
return true;
}
}

reconcile(model, options) {
model.hfImages.forEach(hfImage => {
this.map['v:shape'].reconcile(hfImage, options);
});
}
}

VmlDrawingXform.DRAWING_ATTRIBUTES = {
'xmlns:oa': 'urn:schemas-microsoft-com:office:activation',
'xmlns:p': 'urn:schemas-microsoft-com:office:powerpoint',
'xmlns:x': 'urn:schemas-microsoft-com:office:excel',
'xmlns:o': 'urn:schemas-microsoft-com:office:office',
'xmlns:v': 'urn:schemas-microsoft-com:vml',
};

module.exports = VmlDrawingXform;
45 changes: 45 additions & 0 deletions lib/xlsx/xform/drawing/vlm-drawing/vml-fill-xform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const BaseXform = require('../../base-xform');

class VmlFillform extends BaseXform {
get tag() {
return 'v:fill';
}

render(xmlStream, model) {
xmlStream.leafNode(this.tag, {
focussize: model.focussize,
on: model.on,
color2: model.color2,
});
}

parseOpen(node) {
switch (node.name) {
case this.tag:
this.reset();
this.model = {
// 'f'
on: node.attributes.on,
focussize: node.attributes.focussize, // '0.0'
color2: node.attributes.color2,
};
return true;
default:
return true;
}
}

parseText() {}

parseClose(name) {
switch (name) {
case this.tag:
return false;
default:
// unprocessed internal nodes
return true;
}
}
}

module.exports = VmlFillform;
Loading