From a6c0a4a16887eb6e6805904fd1c06ad2a1763df2 Mon Sep 17 00:00:00 2001 From: Priidik Vaikla Date: Mon, 7 May 2018 11:48:27 +0300 Subject: [PATCH] List /xl/sharedStrings.xml in [Content_Types].xml only if one of the worksheets use shared strings --- lib/xlsx/xform/core/content-types-xform.js | 12 ++++++++-- .../xform/core/content-types-xform.spec.js | 22 +++++++++++++++---- .../xlsx/xform/core/data/content-types.03.xml | 13 +++++++++++ .../xlsx/xform/core/data/content-types.04.xml | 14 ++++++++++++ 4 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 spec/unit/xlsx/xform/core/data/content-types.03.xml create mode 100644 spec/unit/xlsx/xform/core/data/content-types.04.xml diff --git a/lib/xlsx/xform/core/content-types-xform.js b/lib/xlsx/xform/core/content-types-xform.js index 2029d92c2..a8b8edd7b 100644 --- a/lib/xlsx/xform/core/content-types-xform.js +++ b/lib/xlsx/xform/core/content-types-xform.js @@ -44,17 +44,25 @@ utils.inherits(ContentTypesXform, BaseXform, { xmlStream.leafNode('Override', {PartName: '/xl/workbook.xml', ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml'}); + var hasSharedStrings = false; model.worksheets.forEach(function(worksheet) { var name = '/xl/worksheets/sheet' + worksheet.id + '.xml'; xmlStream.leafNode('Override', {PartName: name, ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml'}); + + if (hasSharedStrings === false && worksheet.useSharedStrings === true) { + hasSharedStrings = true; + } }); xmlStream.leafNode('Override', {PartName: '/xl/theme/theme1.xml', ContentType: 'application/vnd.openxmlformats-officedocument.theme+xml'}); xmlStream.leafNode('Override', {PartName: '/xl/styles.xml', ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.styles+xml'}); - xmlStream.leafNode('Override', {PartName: '/xl/sharedStrings.xml', - ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'}); + + if (hasSharedStrings) { + xmlStream.leafNode('Override', {PartName: '/xl/sharedStrings.xml', + ContentType: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sharedStrings+xml'}); + } model.drawings && model.drawings.forEach(function(drawing) { xmlStream.leafNode('Override', {PartName: '/xl/drawings/' + drawing.name + '.xml', diff --git a/spec/unit/xlsx/xform/core/content-types-xform.spec.js b/spec/unit/xlsx/xform/core/content-types-xform.spec.js index 4d5c943bb..00a0eda58 100644 --- a/spec/unit/xlsx/xform/core/content-types-xform.spec.js +++ b/spec/unit/xlsx/xform/core/content-types-xform.spec.js @@ -7,18 +7,32 @@ var testXformHelper = require('./../test-xform-helper'); var expectations = [ { - title: 'Three Sheets', + title: 'Three Sheets with shared strings', create: function() { return new ContentTypesXform(); }, - preparedModel: { worksheets: [{id: 1}, {id: 2}, {id: 3}, ], media: [], drawings: [] }, + preparedModel: { worksheets: [{id: 1, useSharedStrings: true}, {id: 2, useSharedStrings: false}, {id: 3}, ], media: [], drawings: [] }, xml: fs.readFileSync(__dirname + '/data/content-types.01.xml').toString().replace(/\r\n/g, '\n'), tests: ['render'] }, { - title: 'Images', + title: 'Images with shared strings', create: function() { return new ContentTypesXform(); }, - preparedModel: { worksheets: [{id: 1}, {id: 2}], media: [{type: 'image', extension: 'png'}, {type: 'image', extension: 'jpg'}], drawings: [] }, + preparedModel: { worksheets: [{id: 1, useSharedStrings: false}, {id: 2, useSharedStrings: true}], media: [{type: 'image', extension: 'png'}, {type: 'image', extension: 'jpg'}], drawings: [] }, xml: fs.readFileSync(__dirname + '/data/content-types.02.xml').toString().replace(/\r\n/g, '\n'), tests: ['render'] + }, + { + title: 'Three Sheets without shared strings', + create: function() { return new ContentTypesXform(); }, + preparedModel: { worksheets: [{id: 1}, {id: 2}, {id: 3}, ], media: [], drawings: [] }, + xml: fs.readFileSync(__dirname + '/data/content-types.03.xml').toString().replace(/\r\n/g, '\n'), + tests: ['render'] + }, + { + title: 'Images without shared strings', + create: function() { return new ContentTypesXform(); }, + preparedModel: { worksheets: [{id: 1}, {id: 2, useSharedStrings: false}], media: [{type: 'image', extension: 'png'}, {type: 'image', extension: 'jpg'}], drawings: [] }, + xml: fs.readFileSync(__dirname + '/data/content-types.04.xml').toString().replace(/\r\n/g, '\n'), + tests: ['render'] } ]; diff --git a/spec/unit/xlsx/xform/core/data/content-types.03.xml b/spec/unit/xlsx/xform/core/data/content-types.03.xml new file mode 100644 index 000000000..af273600f --- /dev/null +++ b/spec/unit/xlsx/xform/core/data/content-types.03.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/spec/unit/xlsx/xform/core/data/content-types.04.xml b/spec/unit/xlsx/xform/core/data/content-types.04.xml new file mode 100644 index 000000000..4b1c1adad --- /dev/null +++ b/spec/unit/xlsx/xform/core/data/content-types.04.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file