@protobi/exceljs
Version:
Excel Workbook Manager - Temporary fork with pivot table enhancements and bug fixes pending upstream merge
55 lines (49 loc) • 1.47 kB
JavaScript
class CacheField {
constructor({name, sharedItems}) {
// string type
//
// {
// 'name': 'A',
// 'sharedItems': ['a1', 'a2', 'a3']
// }
//
// or
//
// integer type
//
// {
// 'name': 'D',
// 'sharedItems': null
// }
this.name = name;
this.sharedItems = sharedItems;
}
// Helper function to escape XML special characters
escapeXml(unsafe) {
if (unsafe == null) return '';
return String(unsafe)
.replace(/&/g, '&')
.replace(/</g, '<')
.replace(/>/g, '>')
.replace(/"/g, '"')
.replace(/'/g, ''');
}
render() {
// PivotCache Field: http://www.datypic.com/sc/ooxml/e-ssml_cacheField-1.html
// Shared Items: http://www.datypic.com/sc/ooxml/e-ssml_sharedItems-1.html
// integer types
if (this.sharedItems === null) {
// TK(2023-07-18): left out attributes... minValue="5" maxValue="45"
return `<cacheField name="${this.escapeXml(this.name)}" numFmtId="0">
<sharedItems containsSemiMixedTypes="0" containsString="0" containsNumber="1" containsInteger="1" />
</cacheField>`;
}
// string types
return `<cacheField name="${this.escapeXml(this.name)}" numFmtId="0">
<sharedItems count="${this.sharedItems.length}">
${this.sharedItems.map(item => `<s v="${this.escapeXml(item)}" />`).join('')}
</sharedItems>
</cacheField>`;
}
}
module.exports = CacheField;