Skip to content

Instantly share code, notes, and snippets.

@sunnymui
Created March 10, 2021 04:42
Show Gist options
  • Save sunnymui/2f74968f10cc83ed94b8339496584ce6 to your computer and use it in GitHub Desktop.
Save sunnymui/2f74968f10cc83ed94b8339496584ce6 to your computer and use it in GitHub Desktop.
Excel JS Utility Functions
export const getColor = (color = "") => {
let colorCode = null;
switch (color) {
case "manila":
colorCode = "f7eed7";
break;
case "mustard":
colorCode = "dfdfc0";
break;
case "pale":
colorCode = "f7f9f2";
break;
case "black":
colorCode = "000000";
break;
case "red":
colorCode = "8d0808";
break;
case "gray":
colorCode = "777777";
break;
case "light-gray":
colorCode = "82847d";
break;
case "dark-gray":
colorCode = "555555";
break;
default:
// very dark gray
colorCode = "333333";
break;
}
export const setFontStyle = (item = {}, options = '') => {
// options string should look something like 'bold italic #red 11'
const isBold = options.includes('bold');
const isItalic = options.includes('italic');
// match any word starting with a # to find the color
const colorMatch = options.match(/#\w+/g);
// strip out the # in the color match for the actual value to pass
const color = colorMatch && colorMatch.length ? colorMatch[0].replace('#','') : null;
// match any number
const sizeMatch = options.match(/\d+/g);
// need to format the size match into a number type
const size = sizeMatch && sizeMatch.length ? Number(sizeMatch[0]) : null;
const fontStyle = {
name: 'Calibri',
family: 2,
color: getColor(color),
bold: isBold,
italic: isItalic,
size,
};
if (!item) {
return fontStyle
}
return item.font = fontStyle;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment