Skip to content

Instantly share code, notes, and snippets.

@or9
Last active June 24, 2024 14:25
Show Gist options
  • Save or9/89786be76de424c4d66573b8717aa98f to your computer and use it in GitHub Desktop.
Save or9/89786be76de424c4d66573b8717aa98f to your computer and use it in GitHub Desktop.
Heavy handed approach adds text wrapping to all cells exported by SheetJS library
/**
* Heavy handed approach adds text wrapping to all cells.
* Doesn't seem to break anything for other field types….
* Haven't thoroughly tested.
* New:
<cellXfs count="2">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1">
<alignment wrapText="true"/>
</xf>
<xf numFmtId="3" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1">
<alignment wrapText="true"/>
</xf>
</cellXfs>
Old:
<cellXfs count="2">
<xf numFmtId="0" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
<xf numFmtId="3" fontId="0" fillId="0" borderId="0" xfId="0" applyNumberFormat="1"/>
</cellXfs>
*/
function write_cellXfs(cellXfs = [])/*:string*/ {
console.debug(`@xlsx.mjs#write_cellXfs modified to allow cell wrapping`);
var o/*:Array<string>*/ = [];
o[o.length] = (writextag('cellXfs',null));
cellXfs.forEach(function(c) {
const alignmentEl = writextag('alignment', null, {wrapText: true});
o[o.length] = writextag('xf', null, c).replace(`/>`, `>${alignmentEl}</xf>`);
});
o[o.length] = ("</cellXfs>");
if(o.length === 2) return "";
o[0] = writextag('cellXfs',null, {count:o.length-2}).replace("/>",">");
return o.join("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment