Skip to content

Fix issue #1431 Streaming WorkbookReader _parseSharedStrings doesn't handle rich text within shared string nodes #1432

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

Merged
merged 3 commits into from
Aug 30, 2020
Merged
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
94 changes: 83 additions & 11 deletions lib/stream/xlsx/workbook-reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,28 +170,100 @@ class WorkbookReader extends EventEmitter {
return;
}

let inT = false;
let text = null;
let richText = [];
let index = 0;
let font = null;
for await (const events of parseSax(iterateStream(entry))) {
for (const {eventType, value} of events) {
if (eventType === 'opentag') {
const node = value;
if (node.name === 't') {
text = null;
inT = true;
switch (node.name) {
case 'b':
font = font || {};
font.bold = true;
break;
case 'charset':
font = font || {};
font.charset = parseInt(node.attributes.charset, 10);
break;
case 'color':
font = font || {};
font.color = {};
if (node.attributes.rgb) {
font.color.argb = node.attributes.argb;
}
if (node.attributes.val) {
font.color.argb = node.attributes.val;
}
if (node.attributes.theme) {
font.color.theme = node.attributes.theme;
}
break;
case 'family':
font = font || {};
font.family = parseInt(node.attributes.val, 10);
break;
case 'i':
font = font || {};
font.italic = true;
break;
case 'outline':
font = font || {};
font.outline = true;
break;
case 'rFont':
font = font || {};
font.name = node.value;
break;
case 'si':
font = null;
richText = [];
text = null;
break;
case 'sz':
font = font || {};
font.size = parseInt(node.attributes.val, 10);
break;
case 'strike':
break;
case 't':
text = null;
break;
case 'u':
font = font || {};
font.underline = true;
break;
case 'vertAlign':
font = font || {};
font.vertAlign = node.attributes.val;
break;
}
} else if (eventType === 'text') {
text = text ? text + value : value;
} else if (eventType === 'closetag') {
const node = value;
if (inT && node.name === 't') {
if (this.options.sharedStrings === 'cache') {
this.sharedStrings.push(text);
} else if (this.options.sharedStrings === 'emit') {
yield {index: index++, text};
}
text = null;
switch (node.name) {
case 'r':
richText.push({
font,
text,
});

font = null;
text = null;
break;
case 'si':
if (this.options.sharedStrings === 'cache') {
this.sharedStrings.push(richText.length ? {richText} : text);
} else if (this.options.sharedStrings === 'emit') {
yield {index: index++, text: richText.length ? {richText} : text};
}

richText = [];
font = null;
text = null;
break;
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions spec/integration/pr/test-pr-1431.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
const ExcelJS = verquire('exceljs');

describe('github issues', () => {
it('pull request 1431 - streaming reader should handle rich text within shared strings', async () => {
const rowData = [
{
richText: [
{font: {bold: true}, text: 'This should '},
{font: {italic: true}, text: 'be one shared string value'},
],
},
'this should be the second shared string',
];

const workbook = new ExcelJS.stream.xlsx.WorkbookWriter({
filename: './test.xlsx',
useSharedStrings: true,
});

const sheet = workbook.addWorksheet('data');

sheet.addRow(rowData);

await workbook.commit();

return new Promise((resolve, reject) => {
const workbookReader = new ExcelJS.stream.xlsx.WorkbookReader('./test.xlsx', {
entries: 'emit',
hyperlinks: 'cache',
sharedStrings: 'cache',
styles: 'cache',
worksheets: 'emit',
});

workbookReader.on('worksheet', worksheet =>
worksheet.on('row', row => {
expect(row.values[1]).to.eql(rowData[0]);
expect(row.values[2]).to.equal(rowData[1]);

resolve();
})
);
workbookReader.on('error', reject);

workbookReader.read();
});
});
});