文档主题

SpreadJS 提供文档主题功能,允许用户在整个工作簿中统一协调颜色与字体,从而实现一致的视觉风格。该组件内置了 Office、Apex、Metro 等多种专业主题,支持对主题颜色及亮度进行灵活调整,并能自动应用标题与正文字体设置,包括针对中文、日文、韩文等东亚文本的专项适配。

概述 本 Demo 展示了 SpreadJS 的文档主题功能,包括主题切换、主题色应用和主题字体设置。Demo 提供了一个交互式界面,用户可以切换不同的内置主题、为选中的单元格应用主题色(包括亮度变化)、以及根据区域设置自动应用东亚字体。 实现思路 初始化工作表,创建主题色展示网格,展示 Background 1/2、Text 1/2、Accent 1-6 的不同亮度变体(0、80、60、40、-25、-50) 为主题下拉菜单绑定 onchange 事件,切换工作表主题 为"设置前景"和"设置背景"按钮绑定点击事件,根据用户选择的主题色类型、索引和亮度,应用到选中的单元格 为区域设置下拉菜单绑定事件,切换区域后通过导出/导入重新应用东亚字体设置 代码解析 展示主题色的亮度变化 主题颜色的亮度值范围是 -100 到 100,负值表示较深的变体,正值表示较浅的变体,0 表示基础主题颜色。 切换文档主题 当用户选择不同的主题时,通过 currentTheme() 方法应用内置主题名称(如 "Office"、"Apex"、"Metro"),然后刷新布局和重绘工作表。 应用主题色到选中单元格 这段代码根据用户选择的主题色类型(Accent、Background、Text)、索引(1-6)和亮度值,组合成主题色字符串(如 "Accent 1 50"),然后应用到当前选中的单元格。 设置主题字体 使用 themeFont() 方法可以为单元格设置主题字体。"Headings" 表示使用主题的标题字体,"Body" 表示使用主题的正文字体。 切换区域设置以应用东亚字体 切换区域设置时,需要通过导出/导入操作重新应用字体设置。fontScriptCode 用于指定字体脚本代码,"Hans" 表示简体中文,"Jpan" 表示日文,"Hang" 表示韩文。 运行效果 表格上方展示了主题色的亮度变化效果,每列展示不同主题色(Background 1/2、Text 1/2、Accent 1-6),每行展示不同亮度值(0、80、60、40、-25、-50) 从"更改文档主题"下拉菜单选择不同的主题,所有主题色会随之改变 选中表格中的任意单元格,设置主题色类型、索引和亮度,点击"设置背景"或"设置前景"按钮,应用主题色到选中单元格 从"来自区域设置的主题字体"下拉菜单选择不同的区域(英语、中文、日语、韩语),表格会根据区域设置自动应用相应的东亚字体 API 参考 currentTheme 方法 获取或设置工作表的当前主题。 value:可选,主题名称(字符串)或主题实例 返回值:当前主题或工作表实例 backColor 和 foreColor 方法 设置单元格的背景色或前景色,可以使用主题色字符串。 value:颜色值,可以是标准颜色或主题色字符串(如 "Accent 1 50") themeFont 方法 设置单元格的主题字体。 value:主题字体名称,如 "Body"(正文)、"Headings"(标题)
var spreadNS = GC.Spread.Sheets; function getElement (id) { return document.getElementById(id); } window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 1}); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getSheet(0); sheet.suspendPaint(); sheet.setColumnWidth(0, 22); sheet.setColumnWidth(1, 30); sheet.setColumnWidth(2, 22); sheet.getCell(1, 3).value("Theme Colors").themeFont("Headings").hAlign(spreadNS.HorizontalAlign.center); sheet.addSpan(1, 3, 1, 10); sheet.getCell(2, 3).text("Background 1").themeFont("Body"); sheet.getCell(2, 4).text("Text 1").themeFont("Body"); sheet.getCell(2, 5).text("Background 2").themeFont("Body"); sheet.getCell(2, 6).text("Text 2").themeFont("Body"); sheet.getCell(2, 7).text("Accent 1").themeFont("Body"); sheet.getCell(2, 8).text("Accent 2").themeFont("Body"); sheet.getCell(2, 9).text("Accent 3").themeFont("Body"); sheet.getCell(2, 10).text("Accent 4").themeFont("Body"); sheet.getCell(2, 11).text("Accent 5").themeFont("Body"); sheet.getCell(2, 12).text("Accent 6").themeFont("Body"); sheet.getCell(4, 1).value("0").themeFont("Body"); sheet.getCell(4, 3).backColor("Background 1"); sheet.getCell(4, 4).backColor("Text 1"); sheet.getCell(4, 5).backColor("Background 2"); sheet.getCell(4, 6).backColor("Text 2"); sheet.getCell(4, 7).backColor("Accent 1"); sheet.getCell(4, 8).backColor("Accent 2"); sheet.getCell(4, 9).backColor("Accent 3"); sheet.getCell(4, 10).backColor("Accent 4"); sheet.getCell(4, 11).backColor("Accent 5"); sheet.getCell(4, 12).backColor("Accent 6"); sheet.getCell(5, 1).value("80").themeFont("Body"); sheet.getCell(5, 3).backColor("Background 1 80"); sheet.getCell(5, 4).backColor("Text 1 80"); sheet.getCell(5, 5).backColor("Background 2 80"); sheet.getCell(5, 6).backColor("Text 2 80"); sheet.getCell(5, 7).backColor("Accent 1 80"); sheet.getCell(5, 8).backColor("Accent 2 80"); sheet.getCell(5, 9).backColor("Accent 3 80"); sheet.getCell(5, 10).backColor("Accent 4 80"); sheet.getCell(5, 11).backColor("Accent 5 80"); sheet.getCell(5, 12).backColor("Accent 6 80"); sheet.getCell(6, 1).value("60").themeFont("Body"); sheet.getCell(6, 3).backColor("Background 1 60"); sheet.getCell(6, 4).backColor("Text 1 60"); sheet.getCell(6, 5).backColor("Background 2 60"); sheet.getCell(6, 6).backColor("Text 2 60"); sheet.getCell(6, 7).backColor("Accent 1 60"); sheet.getCell(6, 8).backColor("Accent 2 60"); sheet.getCell(6, 9).backColor("Accent 3 60"); sheet.getCell(6, 10).backColor("Accent 4 60"); sheet.getCell(6, 11).backColor("Accent 5 60"); sheet.getCell(6, 12).backColor("Accent 6 60"); sheet.getCell(7, 1).value("40").themeFont("Body"); sheet.getCell(7, 3).backColor("Background 1 40"); sheet.getCell(7, 4).backColor("Text 1 40"); sheet.getCell(7, 5).backColor("Background 2 40"); sheet.getCell(7, 6).backColor("Text 2 40"); sheet.getCell(7, 7).backColor("Accent 1 40"); sheet.getCell(7, 8).backColor("Accent 2 40"); sheet.getCell(7, 9).backColor("Accent 3 40"); sheet.getCell(7, 10).backColor("Accent 4 40"); sheet.getCell(7, 11).backColor("Accent 5 40"); sheet.getCell(7, 12).backColor("Accent 6 40"); sheet.getCell(8, 1).value("-25").themeFont("Body"); sheet.getCell(8, 3).backColor("Background 1 -25"); sheet.getCell(8, 4).backColor("Text 1 -25"); sheet.getCell(8, 5).backColor("Background 2 -25"); sheet.getCell(8, 6).backColor("Text 2 -25"); sheet.getCell(8, 7).backColor("Accent 1 -25"); sheet.getCell(8, 8).backColor("Accent 2 -25"); sheet.getCell(8, 9).backColor("Accent 3 -25"); sheet.getCell(8, 10).backColor("Accent 4 -25"); sheet.getCell(8, 11).backColor("Accent 5 -25"); sheet.getCell(8, 12).backColor("Accent 6 -25"); sheet.getCell(9, 1).value("-50").themeFont("Body"); sheet.getCell(9, 3).backColor("Background 1 -50"); sheet.getCell(9, 4).backColor("Text 1 -50"); sheet.getCell(9, 5).backColor("Background 2 -50"); sheet.getCell(9, 6).backColor("Text 2 -50"); sheet.getCell(9, 7).backColor("Accent 1 -50"); sheet.getCell(9, 8).backColor("Accent 2 -50"); sheet.getCell(9, 9).backColor("Accent 3 -50"); sheet.getCell(9, 10).backColor("Accent 4 -50"); sheet.getCell(9, 11).backColor("Accent 5 -50"); sheet.getCell(9, 12).backColor("Accent 6 -50"); sheet.resumePaint(); getElement('currentThemes').onchange = function (e) { var sheet = spread.getActiveSheet(); sheet.currentTheme(e.target.value); spread.invalidateLayout(); spread.repaint(); }; getElement('theme_type').onchange = function (e) { var selectElement = getElement('theme_index'); while (selectElement.firstChild) { selectElement.removeChild(selectElement.firstChild); } var options = [1, 2]; var themeType = e.target.value; if (themeType == "Accent") { options = [1, 2, 3, 4, 5, 6]; } options.forEach(function (option) { var optionElement = document.createElement('option'); optionElement.value = option.toString(); optionElement.text = option.toString(); selectElement.appendChild(optionElement); }); }; getElement('btnSetForeground').onclick = function () { var sheet = spread.getActiveSheet(); var brightness = parseInt(getElement('theme_brightness').value); if (isNaN(brightness)) return; if (brightness < -100) { brightness = -100; } else if (brightness > 100) { brightness = 100; } var themeColor = getElement('theme_type').value + " " + getElement('theme_index').value + " " + brightness.toString(); sheet.getSelections().forEach(function(range) { var actualRange = getActualCellRange(range, sheet.getRowCount(), sheet.getColumnCount()); sheet.getRange(actualRange.row, actualRange.col, actualRange.rowCount, actualRange.colCount).foreColor(themeColor); }); }; getElement('btnSetBackground').onclick = function () { var sheet = spread.getActiveSheet(); var brightness = parseInt(getElement('theme_brightness').value); if (isNaN(brightness)) return; if (brightness < -100) { brightness = -100; } else if (brightness > 100) { brightness = 100; } var themeColor = getElement('theme_type').value + " " + getElement('theme_index').value + " " + brightness.toString(); sheet.getSelections().forEach(function(range) { var actualRange = getActualCellRange(range, sheet.getRowCount(), sheet.getColumnCount()); sheet.getRange(actualRange.row, actualRange.col, actualRange.rowCount, actualRange.colCount).backColor(themeColor); }); }; function getActualCellRange(cellRange, rowCount, columnCount) { if (cellRange.row == -1 && cellRange.col == -1) { return new spreadNS.Range(0, 0, rowCount, columnCount); } else if (cellRange.row == -1) { return new spreadNS.Range(0, cellRange.col, rowCount, cellRange.colCount); } else if (cellRange.col == -1) { return new spreadNS.Range(cellRange.row, 0, cellRange.rowCount, columnCount); } return cellRange; } document.getElementById('cultureName').onchange = function (e) { exportImportSpread(spread, () => { let cultureName = e.target.value, fontScriptCode = { 'zh-cn': 'Hans', 'ja-jp': 'Jpan', 'ko-kr': 'Hang', }[cultureName]; GC.Spread.Common.CultureManager.getCultureInfo(cultureName).fontScriptCode = fontScriptCode; GC.Spread.Common.CultureManager.culture(cultureName); }); }; GC.Spread.Common.CultureManager.culture(document.getElementById('cultureName').value); } function exportImportSpread (spread, beforeImport) { return new Promise((resolve, reject) => { try { spread.export(function (blob) { beforeImport(); spread.import(blob, function () { resolve(); }, function (e) { reject(e); }); }, function (e) { reject(e); }, { fileType: GC.Spread.Sheets.FileType.excel }); } catch (e) { reject(e); } }); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-io/dist/gc.spread.sheets.io.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-ko/dist/gc.spread.sheets.resources.ko.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.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"> <p> 使用这些选项定义要用于当前所选单元格的“设置背景”和“设置前景”按钮的颜色。 </p> <div class="option-row"> <label>更改文档主题:</label> <select id="currentThemes" class="position"> <option>Default</option> <option selected>Office</option> <option>Apex</option> <option>Aspect</option> <option>Concourse</option> <option>Civic</option> <option>Oriel</option> <option>Origin</option> <option>Paper</option> <option>Solstice</option> <option>Technic</option> <option>Trek</option> <option>Urban</option> <option>Verve</option> <option>Equity</option> <option>Flow</option> <option>Foundry</option> <option>Median</option> <option>Metro</option> <option>Module</option> <option>Opulent</option> </select> </div> <div class="option-row"> <select id="theme_type" class="position"> <option value="Accent">强调色</option> <option value="Background">背景</option> <option value="Text">文本</option> </select> <select id="theme_index" class="position"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> </select> </div> <div class="option-row"> <label for="theme_brightness">亮度(-100 - 100):</label> <input id="theme_brightness" value="50" /> </div> <div class="option-row"> <input type="button" value="设置背景" id="btnSetBackground" /> <input type="button" value="设置前景" id="btnSetForeground" /> </div> <p> 更改区域设置会导出到 Excel 文件并重新导入,这将根据区域设置应用字体以显示文本并测量列宽。 </p> <div class="option-row"> <label>来自区域设置的主题字体:</label> <select id="cultureName"> <option value="en-us">英语</option> <option value="zh-cn" selected>中文</option> <option value="ja-jp">日语</option> <option value="ko-kr">韩语</option> </select> </div> </div> </div> </body> </html>
.sample { position: relative; height: 100%; overflow: auto; } .sample::after { display: block; content: ""; clear: both; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } p { margin: 0; padding: 6px 12px; background-color:#F4F8EB } input, select { padding: 4px 6px; box-sizing: border-box; margin-top: 6px; } .options-toggle { display: none; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }