Spread 背景

工作簿的背景可以通过JavaScript API自定义。 下面的示例展示了如何修改背景。

你可以设置 backColor 或者 backgroundImage 选项为每一个表单设置背景色或者背景图片。如果你同时设置背景色和背景图片, 那么背景图片将会优先显示。 或者,您可以使用workbook的** backgroundImage **选项为工作表设置背景图像。请注意,设置背景图像会覆盖所有设置的背景颜色,因此必须将其删除才能使背景颜色生效。 SpreadJS 提供控制图片布局方式的能力, 你可以设置 backgroundImageLayout 选项来完成此目标。图片布局包含以下几种类型: stretch: 改变图像尺寸使其充满整个区域,无视实际的长宽比。 center: 图片将显示在区域的正中央。 zoom: 改变图像的尺寸使其适合控件尺寸,图片实际的长宽比不变。 none: 在显示区域中使用图像实际的尺寸显示该图像,没有任何尺寸变化。 你也可以设置 grayAreaBackColor 选项来设置表单灰色区域的背景色。
import * as React from 'react'; import { createRoot } from 'react-dom/client'; import './styles.css'; import { AppFunc } from './app-func'; import { App } from './app-class'; // 1. Functional Component sample createRoot(document.getElementById('app')).render(<AppFunc />); // 2. Class Component sample createRoot(document.getElementById('app')).render(<App />);
import * as React from 'react'; import { useState } from 'react'; import GC from '@grapecity-software/spread-sheets'; import { SpreadSheets, Worksheet } from '@grapecity-software/spread-sheets-react'; import '@grapecity-software/spread-sheets-resources-zh'; GC.Spread.Common.CultureManager.culture("zh-cn"); import './styles.css'; export function AppFunc() { const [spread, setSpread] = useState(null); //const [grayAreaBackColor, setGrayAreaBackColor] = useState(null); //const [backColor, setBackColor] = useState(null); const [pictureUrl, setPictureUrl] = useState(null); const initSpread = (spread) => { setSpread(spread); spread.suspendPaint(); let spreadNS = GC.Spread.Sheets; let sheet = spread.getSheet(0); sheet.setRowCount(15); sheet.setColumnCount(20); spread.options.backColor = 'white'; spread.options.grayAreaBackColor = 'gray'; spread.options.backgroundImageLayout = spreadNS.ImageLayout.stretch; spread.options.backgroundImage = '$DEMOROOT$/spread/source/images/backImage.png'; spread.resumePaint(); } const changeGrayAreaBackColor = (e) => { let selectElement = e.target; spread.options.grayAreaBackColor = selectElement.options[selectElement.selectedIndex].value; } const changeSpreadBackColor = (e) => { let selectElement = e.target; spread.options.backColor = selectElement.options[selectElement.selectedIndex].value; } const selectImage = (e) => { if (typeof e.target.files[0] !== 'object') { return; } let file = e.target.files[0]; if (!/image\/\w+/.test(file.type)) { alert('The file must be an image!'); return false; } let reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { setPictureUrl(this.result); }; } const changeLayout = (e) => { let layout = parseInt(e.target.value); spread.options.backgroundImageLayout = layout; } const setSpreadBackgroundImage = (e) => { spread.options.backgroundImage = pictureUrl; } const deleteSpreadBackgroundImage = (e) => { spread.options.backgroundImage = ''; } return <div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => initSpread(spread)}> <Worksheet> </Worksheet> </SpreadSheets> </div> <div className="options-container"> <div className="option-row"> <label>设置背景图片:</label> <input type="file" name="image" id="file_input" onChange={e => selectImage(e)} /> </div> <br /> <div className="option-row"> <label>工作簿背景图片布局:</label> <select id="layout" onChange={e => changeLayout(e)}> <option value={0} selected="selected">拉伸</option> <option value={1}>居中</option> <option value={2}>缩放</option> <option value={3}>无</option> </select> </div> <div className="option-row"> <input type="button" id="setSpreadBackgroundImage" defaultValue="设置" className="narrow-button" onClick={e => setSpreadBackgroundImage(e)} /> <input type="button" id="delSpreadBackgroundImage" defaultValue="删除背景图片" onClick={e => deleteSpreadBackgroundImage(e)} /> </div> <hr /> <div class="option-row"> <label>选择一种颜色作为工作簿灰色区域的颜色。</label> <select id="grayAreaBackColor" onChange={e => changeGrayAreaBackColor(e)}> <option value={"gray"}>灰色</option> <option value={"rgb(220, 236, 244)"}>浅蓝色</option> <option value={"#F4F8EB"}>浅绿色</option> <option value={"white"}>白色</option> </select> </div> <div class="option-row"> <label>选择一种颜色作为工作簿背景颜色。</label> <select id="spreadBackColor" onChange={e => changeSpreadBackColor(e)}> <option value={"white"}>白色</option> <option value={"#F4F8EB"}>浅绿色</option> <option value={"rgb(220, 236, 244)"}>浅蓝色</option> <option value={"gray"}>灰色</option> </select> </div> <label class="note"><u>注意</u>: 移除背景图片以查看此更改。</label> </div> </div>; }
import * as React from 'react'; import GC from '@grapecity-software/spread-sheets'; import { SpreadSheets, Worksheet } from '@grapecity-software/spread-sheets-react'; import '@grapecity-software/spread-sheets-resources-zh'; GC.Spread.Common.CultureManager.culture("zh-cn"); import './styles.css'; const Component = React.Component; export class App extends Component { constructor(props) { super(props); this.spread = null; this.grayAreaBackColor = null; this.backColor = null; this.pictureUrl = null; } render() { return <div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => this.initSpread(spread)}> <Worksheet> </Worksheet> </SpreadSheets> </div> <div className="options-container"> <div className="option-row"> <label>设置背景图片:</label> <input type="file" name="image" id="file_input" onChange={e => this.selectImage(e)} /> </div> <br /> <div className="option-row"> <label>工作簿背景图片布局:</label> <select id="layout" onChange={e => this.changeLayout(e)}> <option value={0} selected="selected">拉伸</option> <option value={1}>居中</option> <option value={2}>缩放</option> <option value={3}>无</option> </select> </div> <div className="option-row"> <input type="button" id="setSpreadBackgroundImage" defaultValue="设置" className="narrow-button" onClick={e => this.setSpreadBackgroundImage(e)} /> <input type="button" id="delSpreadBackgroundImage" defaultValue="删除背景图片" onClick={e => this.deleteSpreadBackgroundImage(e)} /> </div> <hr /> <div class="option-row"> <label>选择一种颜色作为工作簿灰色区域的颜色。</label> <select id="grayAreaBackColor" onChange={e => this.changeGrayAreaBackColor(e)}> <option value={"gray"}>灰色</option> <option value={"rgb(220, 236, 244)"}>浅蓝色</option> <option value={"#F4F8EB"}>浅绿色</option> <option value={"white"}>白色</option> </select> </div> <div class="option-row"> <label>选择一种颜色作为工作簿背景颜色。</label> <select id="spreadBackColor" onChange={e => this.changeSpreadBackColor(e)}> <option value={"white"}>白色</option> <option value={"#F4F8EB"}>浅绿色</option> <option value={"rgb(220, 236, 244)"}>浅蓝色</option> <option value={"gray"}>灰色</option> </select> </div> <label class="note"><u>注意</u>: 移除背景图片以查看此更改。</label> </div> </div>; } initSpread(spread) { this.spread = spread; spread.suspendPaint(); let spreadNS = GC.Spread.Sheets; let sheet = spread.getSheet(0); sheet.setRowCount(15); sheet.setColumnCount(20); spread.options.backColor = 'white'; spread.options.grayAreaBackColor = 'gray'; spread.options.backgroundImageLayout = spreadNS.ImageLayout.stretch; spread.options.backgroundImage = '$DEMOROOT$/spread/source/images/backImage.png'; spread.resumePaint(); } changeGrayAreaBackColor(e) { let spread = this.spread; let selectElement = e.target; spread.options.grayAreaBackColor = selectElement.options[selectElement.selectedIndex].value; } changeSpreadBackColor(e) { let spread = this.spread; let selectElement = e.target; spread.options.backColor = selectElement.options[selectElement.selectedIndex].value; } selectImage(e) { if (typeof e.target.files[0] !== 'object') { return; } let file = e.target.files[0]; if (!/image\/\w+/.test(file.type)) { alert('The file must be an image!'); return false; } let self = this; let reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { self.pictureUrl = this.result; }; } changeLayout(e) { let spread = this.spread; let layout = parseInt(e.target.value); spread.options.backgroundImageLayout = layout; } setSpreadBackgroundImage(e) { let spread = this.spread; spread.options.backgroundImage = this.pictureUrl; } deleteSpreadBackgroundImage(e) { let spread = this.spread; spread.options.backgroundImage = ''; } }
<!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$/zh/react/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- SystemJS --> <script src="$DEMOROOT$/zh/react/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('$DEMOROOT$/zh/lib/react/license.js').then(function() { System.import('./src/app'); }); </script> </head> <body> <div id="app"></div> </body> </html>
input[type="button"] { width: 180px; } input[type="text"] { padding: 4px; margin-top: 4px; width: 100%; box-sizing: border-box; } label { display: inline-block; margin-bottom: 6px; } .note{ margin-top: 0px; } .colorLabel { background-color: #F4F8EB; } .wide-label { width: 260px; } input.narrow-button, .narrow-label { width: 74px; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 300px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 300px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row:nth-child(1){ padding-bottom: 0px; } .option-row:nth-child(2){ margin-top: 0px; padding-top: 0px; } .option-row:nth-child(3){ padding-bottom: 0px; } .option-row:nth-child(4){ margin-top: 0px; padding-top: 0px; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; } hr { border-color: #fff; opacity: .2; margin-top: 20px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } #app { height: 100%; }
(function(global) { System.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true, react: true }, meta: { '*.css': { loader: 'css' } }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, // map tells the System loader where to look for things map: { '@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js', '@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js', '@grapecity-software/spread-sheets-react': 'npm:@grapecity-software/spread-sheets-react/index.js', '@grapecity/jsob-test-dependency-package/react-components': 'npm:@grapecity/jsob-test-dependency-package/react-components/index.js', 'react': 'npm:react/cjs/react.production.js', 'react-dom': 'npm:react-dom/cjs/react-dom.production.js', 'react-dom/client': 'npm:react-dom/cjs/react-dom-client.production.js', 'scheduler': 'npm:scheduler/cjs/scheduler.production.js', 'css': 'npm:systemjs-plugin-css/css.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', 'systemjs-babel-build': 'npm:systemjs-plugin-babel/systemjs-babel-browser.js' }, // packages tells the System loader how to load when no filename and/or no extension packages: { src: { defaultExtension: 'jsx' }, "node_modules": { defaultExtension: 'js' }, } }); })(this);