Font

SpreadJS provides a rich font style, such as font, font style, bold, italic, text decorations (including accounting underline), and so on.

You can customize the font style using code such as the following: You can customize the font by splitted font properties using code such as the following: You can also set text decorations, such as underline, doubleUnderline, lineThrough, overline, accountingUnderline, and doubleAccountingUnderline. Accounting underline differs from normal underline: for text cells the underline spans the full column width, while for number cells it matches the text width. You can set it like this: You can set the font alignment like this: You can set the font color and cell background color like this: You can set the font format, such as wordwrap, indent, shrinkToFit, etc.
function setSettings(spread) { let sheet = spread.getActiveSheet(); let activeCell = sheet.getCell(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); document.getElementById('font').value = activeCell.font(); document.getElementById('fontStyle').value = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontStyle()); document.getElementById('fontWeight').value = fontStyleOrFontWeightSpreadValueToCssValue(activeCell.fontWeight()); let fontSize = activeCell.fontSize(); if (fontSize) { let fontSizeDigit = parseFloat(fontSize); let fontSizeUnit = fontSize.substring(('' + fontSizeDigit).length); document.getElementById('fontSize').value = fontSizeDigit; document.getElementById('fontSizeUnit').value = fontSizeUnit; } else { document.getElementById('fontSize').value = ''; document.getElementById('fontSizeUnit').value = 'px'; } document.getElementById('fontFamily').value = activeCell.fontFamily() || ''; } function fontStyleOrFontWeightCssValueToSpreadValue(value) { // transfer css font style/weight to spread font style/weight if (value === '') { return undefined; } else if (value === 'normal') { return null; } else { return value; } } function fontStyleOrFontWeightSpreadValueToCssValue(value) { // transfer spread font style/weight to css font style/weight if (value === undefined) { return ''; } else if (value === null) { return 'normal'; } else { return value; } } function setFontToSelectedRanges(spread, setFunc) { let activeSheet = spread.getActiveSheet(); let selections = activeSheet.getSelections(); if (!selections) { return; } activeSheet.suspendPaint(); for (let i = 0; i < selections.length; i++) { let selection = selections[i]; if (!selection) { return; } for (let r = 0; r < selection.rowCount; r++) { for (let c = 0; c < selection.colCount; c++) { let cell = activeSheet.getCell(r + selection.row, c + selection.col); setFunc(cell); } } } activeSheet.resumePaint(); } function setFontBySplittedFont(spread) { let fontStyle = document.getElementById('fontStyle').value, fontWeight = document.getElementById('fontWeight').value, fontFamily = document.getElementById('fontFamily').value, fontSizeDigit = document.getElementById('fontSize').value, fontSizeUnit = document.getElementById('fontSizeUnit').value, fontSize; if (fontSizeDigit) { fontSize = fontSizeDigit + (fontSizeUnit ? fontSizeUnit : 'px'); } setFontToSelectedRanges(spread, (cell) => { cell.fontStyle(fontStyleOrFontWeightCssValueToSpreadValue(fontStyle)); cell.fontWeight(fontStyleOrFontWeightCssValueToSpreadValue(fontWeight)); cell.fontSize(fontSize); cell.fontFamily(fontFamily); }); setSettings(spread); } function setFontByFont(spread) { let font = document.getElementById('font').value; setFontToSelectedRanges(spread, (cell) => { cell.font(font); }); setSettings(spread); } function setStyles(sheet) { sheet.suspendPaint(); //Font sheet.getCell(2, 0).font('italic normal 12px Mangal'); sheet.getCell(4, 0).font('normal bold 15px Arial Black'); sheet.getCell(6, 0).font('normal normal 18px Georgia'); //FontFamily sheet.getCell(2, 1).fontFamily('Mangal'); sheet.getCell(4, 1).fontFamily('Arial Black'); sheet.getCell(6, 1).fontFamily('Georgia'); //FontSize sheet.getCell(2, 2).fontSize('12px'); sheet.getCell(4, 2).fontSize('20px'); sheet.getCell(6, 2).fontSize('28px'); //Bold sheet.getCell(2, 3).fontWeight('bold'); sheet.getCell(4, 3).fontWeight('normal'); //Italic sheet.getCell(2, 4).fontStyle('italic'); sheet.getCell(4, 4).fontStyle('normal'); //Line sheet.getCell(2, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.underline); sheet.getCell(4, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.doubleUnderline); sheet.getCell(6, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.lineThrough); sheet.getCell(8, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.overline); //Accounting Underline sheet.getCell(10, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.accountingUnderline); sheet.setValue(12, 5, 123.45); sheet.getCell(12, 5).formatter("$#,##0.00"); sheet.getCell(12, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.accountingUnderline); sheet.getCell(14, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.doubleAccountingUnderline); sheet.setValue(16, 5, 456.78); sheet.getCell(16, 5).formatter("$#,##0.00"); sheet.getCell(16, 5).textDecoration(GC.Spread.Sheets.TextDecorationType.doubleAccountingUnderline); //Comments for accounting underline cells var commentTextAcct = sheet.comments.add(10, 5, "Text value: accounting underline spans the full column width."); commentTextAcct.displayMode(GC.Spread.Sheets.Comments.DisplayMode.hoverShown); var commentNumAcct = sheet.comments.add(12, 5, "Number value: accounting underline width matches the text width."); commentNumAcct.displayMode(GC.Spread.Sheets.Comments.DisplayMode.hoverShown); var commentTextDblAcct = sheet.comments.add(14, 5, "Text value: double accounting underline spans the full column width."); commentTextDblAcct.displayMode(GC.Spread.Sheets.Comments.DisplayMode.hoverShown); var commentNumDblAcct = sheet.comments.add(16, 5, "Number value: double accounting underline width matches the text width."); commentNumDblAcct.displayMode(GC.Spread.Sheets.Comments.DisplayMode.hoverShown); //Align sheet.getCell(2, 6).vAlign(GC.Spread.Sheets.VerticalAlign.top).hAlign(GC.Spread.Sheets.HorizontalAlign.left); sheet.getCell(4, 6).vAlign(GC.Spread.Sheets.VerticalAlign.center).hAlign(GC.Spread.Sheets.HorizontalAlign.center); sheet.getCell(6, 6).vAlign(GC.Spread.Sheets.VerticalAlign.bottom).hAlign(GC.Spread.Sheets.HorizontalAlign.right); //Forecolor sheet.getCell(2, 7).foreColor('#F7A711'); sheet.getCell(4, 7).foreColor('#82BC00'); sheet.getCell(6, 7).foreColor('#C3C3C3'); //backgroundColor sheet.getCell(2, 8).backColor('#F7A711'); sheet.getCell(4, 8).backColor('#82BC00'); sheet.getCell(6, 8).backColor('#C3C3C3'); //WordWrap sheet.getCell(2, 9).wordWrap(true); sheet.getCell(3, 9).wordWrap(true); //Indent sheet.getCell(2, 10).textIndent(1); sheet.getCell(4, 10).textIndent(3); sheet.getCell(6, 10).textIndent(-2); //ShrinkToFit sheet.getCell(2, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.center); sheet.getCell(4, 11).shrinkToFit(true).vAlign(GC.Spread.Sheets.VerticalAlign.bottom); sheet.resumePaint(); } window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss')); var sheet = spread.getActiveSheet(); sheet.fromJSON(jsonData); setStyles(sheet); setSettings(spread); bindEvents(spread); }; function bindEvents(spread) { document.getElementById("set-splitted-font").addEventListener('click', function () { setFontBySplittedFont(spread); }); document.getElementById("set-font").addEventListener('click', function () { setFontByFont(spread); }); spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () { setSettings(spread); }); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="data.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <div class="option-card"> <div class="option-card-title">Font Properties</div> <div class="option-field"> <label for="fontStyle">Style</label> <input id="fontStyle" type="text" placeholder="e.g. italic"> </div> <div class="option-field"> <label for="fontWeight">Weight</label> <input id="fontWeight" type="text" placeholder="e.g. bold"> </div> <div class="option-field"> <label for="fontSize">Size</label> <div class="size-group"> <input id="fontSize" type="number" min="0" placeholder="12"> <select id="fontSizeUnit"> <option value="px">px</option> <option value="pt">pt</option> </select> </div> </div> <div class="option-field"> <label for="fontFamily">Family</label> <input id="fontFamily" type="text" placeholder="e.g. Arial"> </div> <button id="set-splitted-font" class="option-btn">Apply Font Properties</button> </div> <div class="option-card"> <div class="option-card-title">Font Shorthand</div> <div class="option-field"> <label for="font">Font</label> <input type="text" id="font" placeholder="e.g. italic bold 12px Arial"> </div> <button id="set-font" class="option-btn">Apply Font</button> </div> <div class="option-card option-card-info"> <div class="option-card-title">About</div> <p>Select a cell to view and edit its font properties. The sheet demonstrates various text styles including <strong>accounting underline</strong> &mdash; hover over the accounting underline cells to see comments.</p> </div> </div> </div> </body> </html>
var jsonData = { "name": "Sheet1", "isSelected": true, "activeRow": 10, "activeCol": 5, "visible": 1, "theme": "Office", "data": { "dataTable": { "1": { "0": { "value": "Font", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "1": { "value": "FontFamily", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "2": { "value": "FontSize", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "3": { "value": "Bold", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "4": { "value": "Italic", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "5": { "value": "TextDecoration", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "6": { "value": "Align", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "7": { "value": "ForeColor", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "8": { "value": "BackgroundColor", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "9": { "value": "WordWrap", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "10": { "value": "Indent", "style": { "backColor": "#82bc00", "foreColor": "white" } }, "11": { "value": "ShrinkToFit", "style": { "backColor": "#82bc00", "foreColor": "white" } } }, "2": { "0": { "value": "Font" }, "1": { "value": "FontFamily" }, "2": { "value": "FontSize" }, "3": { "value": "Bold" }, "4": { "value": "Italic" }, "5": { "value": "UnderLine" }, "6": { "value": "Align" }, "7": { "value": "Forecolor" }, "8": { "value": "BackColor" }, "9": { "value": "Word-Wrap Word-Wrap Word-Wrap" }, "10": { "value": "Indent" }, "11": { "value": "This is a long text" } }, "3": { "9": { "value": "Word\r\nWrap" } }, "4": { "0": { "value": "Font" }, "1": { "value": "FontFamily" }, "2": { "value": "FontSize" }, "3": { "value": "Bold" }, "4": { "value": "Italic" }, "5": { "value": "DbUnderLine" }, "6": { "value": "Align" }, "7": { "value": "Forecolor" }, "8": { "value": "BackColor" }, "10": { "value": "Indent" }, "11": { "value": "ShrinkToFit" } }, "6": { "0": { "value": "Font" }, "1": { "value": "FontFamily" }, "2": { "value": "FontSize" }, "5": { "value": "lineThrough" }, "6": { "value": "Align" }, "7": { "value": "Forecolor" }, "8": { "value": "BackColor" }, "10": { "value": "Indent" } }, "8": { "5": { "value": "overline" } }, "10": { "5": { "value": "Accounting" } }, "14": { "5": { "value": "Dbl Accounting" } } }, "defaultDataNode": { "style": { "themeFont": "Body" } } }, "rowHeaderData": { "defaultDataNode": { "style": { "themeFont": "Body" } } }, "colHeaderData": { "defaultDataNode": { "style": { "themeFont": "Body" } } }, "rows": [ null, null, { "size": 50 }, { "size": 50 }, { "size": 50 }, null, { "size": 50 }, null, { "size": 50 }, null, { "size": 30 }, null, { "size": 30 }, null, { "size": 30 }, null, { "size": 30 } ], "columns": [ { "size": 120 }, { "size": 120 }, { "size": 120 }, null, null, { "size": 140 }, { "size": 100 }, null, { "size": 120 }, { "size": 80 }, { "size": 80 }, { "size": 100 } ], "defaultData": {}, "leftCellIndex": 0, "topCellIndex": 0, "selections": { "0": { "row": 10, "col": 5, "rowCount": 1, "colCount": 1 }, "length": 1 }, "rowOutlines": { "items": [] }, "columnOutlines": { "items": [] }, "cellStates": {}, "states": {}, "outlineColumnOptions": {}, "autoMergeRangeInfos": [], "shapeCollectionOption": { "snapMode": 0 }, "printInfo": { "paperSize": { "width": 850, "height": 1100, "kind": 1 } } }
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #f5f7f9; overflow: auto; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; font-size: 13px; color: #333; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; margin: 0; } /* Card layout */ .option-card { background: #fff; border: 1px solid #e0e4e8; border-radius: 6px; padding: 14px; margin-bottom: 10px; } .option-card-title { font-size: 13px; font-weight: 600; color: #555; margin-bottom: 12px; padding-bottom: 6px; border-bottom: 2px solid #82bc00; } /* Form fields */ .option-field { margin-bottom: 10px; } .option-field label { display: block; font-size: 12px; color: #777; margin-bottom: 4px; } .option-field input[type="text"], .option-field input[type="number"] { width: 100%; padding: 6px 8px; border: 1px solid #d0d5da; border-radius: 4px; font-size: 13px; box-sizing: border-box; outline: none; transition: border-color 0.2s; } .option-field input:focus { border-color: #82bc00; } .size-group { display: flex; gap: 6px; } .size-group input { flex: 1; padding: 6px 8px; border: 1px solid #d0d5da; border-radius: 4px; font-size: 13px; outline: none; box-sizing: border-box; } .size-group input:focus { border-color: #82bc00; } .size-group select { width: 56px; padding: 6px 4px; border: 1px solid #d0d5da; border-radius: 4px; font-size: 13px; outline: none; background: #fff; cursor: pointer; } .size-group select:focus { border-color: #82bc00; } /* Button */ .option-btn { display: block; width: 100%; padding: 8px 0; margin-top: 12px; border: none; border-radius: 4px; background: #82bc00; color: #fff; font-size: 13px; font-weight: 500; cursor: pointer; transition: background 0.2s; } .option-btn:hover { background: #6fa300; } .option-btn:active { background: #5e8c00; } /* Info card */ .option-card-info p { font-size: 12px; line-height: 1.6; color: #666; margin: 0; } .option-card-info strong { color: #333; }