Skip to content

Instantly share code, notes, and snippets.

@mrclay
Last active April 23, 2024 16:00
Show Gist options
  • Save mrclay/45e5d8f51b16385d097b9fe7d5c24b1d to your computer and use it in GitHub Desktop.
Save mrclay/45e5d8f51b16385d097b9fe7d5c24b1d to your computer and use it in GitHub Desktop.
Convert Exceljs rich text values to HTML or plaintext strings
function isRichValue(value) {
return Boolean(value && Array.isArray(value.richText));
}
function richToString(rich) {
return rich.richText.map(({ text }) => text).join('');
}
function richToHtml(rich) {
let str = rich.richText.map(({ font = {}, text }) => {
return [
font.bold ? '<strong>' : '',
font.italic ? '<em>' : '',
text,
font.italic ? '</em>' : '',
font.bold ? '</strong>' : '',
].join('');
}).join('');
// simple tag combining where possible
return str.replace(/<\/strong><strong>/g, '').replace(/<\/em><em>/g, '');
}
module.exports = {
isRichValue,
richToHtml,
richToString,
};
@rajuAhmed1705
Copy link

Thank you so much. Saved my time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment