diff --git a/src/gen-charts.ts b/src/gen-charts.ts index efe0c95e..dce5b11b 100644 --- a/src/gen-charts.ts +++ b/src/gen-charts.ts @@ -135,6 +135,9 @@ export async function createExcelWorksheet (chartObject: ISlideRelChart, zip: JS ) } + // dictionary of shared strings mapped to their indexes indexes + const sharedStringsObject: Record = {} + // sharedStrings.xml { // A: Start XML @@ -148,6 +151,7 @@ export async function createExcelWorksheet (chartObject: ISlideRelChart, zip: JS data[0].labels.forEach(arrLabel => (totCount += arrLabel.filter(label => label && label !== '').length)) strSharedStrings += `` strSharedStrings += '' + sharedStringsObject[''] = 0 } else { // series names + all labels of one series + number of label groups (data.labels.length) of one series (i.e. how many times the blank string is used) const totCount = data.length + data[0].labels.length * data[0].labels[0].length + data[0].labels.length @@ -157,20 +161,37 @@ export async function createExcelWorksheet (chartObject: ISlideRelChart, zip: JS strSharedStrings += `` // B: Add 'blank' for A1, B1, ..., of every label group inside data[n].labels strSharedStrings += '' + sharedStringsObject[''] = 0 } // C: Add `name`/Series if (chartObject.opts._type === CHART_TYPE.BUBBLE || chartObject.opts._type === CHART_TYPE.BUBBLE3D) { data.forEach((objData, idx) => { - if (idx === 0) strSharedStrings += 'X-Axis' - else { - strSharedStrings += `${encodeXmlEntities(objData.name || `Y-Axis${idx}`)}` - strSharedStrings += `${encodeXmlEntities(`Size${idx}`)}` + if (idx === 0) { + const sharedString = 'X-Axis' + if (!sharedStringsObject[sharedString]) { + strSharedStrings += `${sharedString}` + sharedStringsObject[sharedString] = Object.keys(sharedStringsObject).length + } + } else { + const sharedString = encodeXmlEntities(objData.name || `Y-Axis${idx}`) + if (!sharedStringsObject[sharedString]) { + strSharedStrings += `${sharedString}` + sharedStringsObject[sharedString] = Object.keys(sharedStringsObject).length + } + + const sizeSharedString = encodeXmlEntities(`Size${idx}`) + strSharedStrings += `${sizeSharedString}` + sharedStringsObject[sizeSharedString] = Object.keys(sharedStringsObject).length } }) } else { data.forEach(objData => { - strSharedStrings += `${encodeXmlEntities((objData.name || ' ').replace('X-Axis', 'X-Values'))}` + const sharedString = encodeXmlEntities((objData.name || ' ').replace('X-Axis', 'X-Values')) + if (!sharedStringsObject[sharedString]) { + strSharedStrings += `${sharedString}` + sharedStringsObject[sharedString] = Object.keys(sharedStringsObject).length + } }) } @@ -184,7 +205,11 @@ export async function createExcelWorksheet (chartObject: ISlideRelChart, zip: JS labelsGroup .filter(label => label && label !== '') .forEach(label => { - strSharedStrings += `${encodeXmlEntities(label)}` + const sharedString = encodeXmlEntities(label) + if (!sharedStringsObject[sharedString]) { + strSharedStrings += `${sharedString}` + sharedStringsObject[sharedString] = Object.keys(sharedStringsObject).length + } }) }) } @@ -460,7 +485,6 @@ export async function createExcelWorksheet (chartObject: ISlideRelChart, zip: JS // WIP: FIXME: // B: add a col for each label/cat - let totLabels = TOT_SER const revLabelGroups = data[0].labels.slice().reverse() revLabelGroups.forEach((labelsGroup, idy) => { /** @@ -471,9 +495,7 @@ export async function createExcelWorksheet (chartObject: ISlideRelChart, zip: JS */ const colLabel = labelsGroup[idx] if (colLabel) { - const totGrpLbls = idy === 0 ? 1 : revLabelGroups[idy - 1].filter(label => label && label !== '').length // get unique label so we can add to get proper shared-string # - totLabels += totGrpLbls - strSheetXml += `${totLabels}` + strSheetXml += `${sharedStringsObject[colLabel]}` } })