Array Formulas

Formulas may include functions that operate on arrays. An array formula is a formula that can perform multiple calculations on one or more of the items in an array.

Description
app.jsx
app-func.jsx
app-class.jsx
index.html
styles.css
Copy to CodeMine

Array formulas are often referred to as CSE (Ctrl+Shift+Enter) formulas because instead of just pressing Enter, you press Ctrl+Shift+Enter to complete the formula.

SpreadJS supports array constants in formulas. Use curly brackets ({ }) to enclose the array elements. Use a comma to separate elements within a row. Use a semicolon to separate rows within the array. Individual elements can be number values, text values, logical values, or error values. For example:

    ={1, 2, 3} * {4; 5; 6}
    /* Returns an array as follows:
       [[4,  8, 12],
        [5, 10, 15],
        [6, 12, 18]]
    */

You can use the setArrayFormula method to set an array formula, similar to the setFormula method. The formula text box shows the array formula surrounded by curly brackets ({ }).

    // set array formula use range
    sheet.setArrayFormula(6, 4, 4, 3, '=A1:A4*A1:C1');
Array formulas are often referred to as CSE (Ctrl+Shift+Enter) formulas because instead of just pressing Enter, you press Ctrl+Shift+Enter to complete the formula. SpreadJS supports array constants in formulas. Use curly brackets ({ }) to enclose the array elements. Use a comma to separate elements within a row. Use a semicolon to separate rows within the array. Individual elements can be number values, text values, logical values, or error values. For example: You can use the setArrayFormula method to set an array formula, similar to the setFormula method. The formula text box shows the array formula surrounded by curly brackets ({ }).
import * as React from 'react'; import * as ReactDOM from 'react-dom'; import './styles.css'; import { AppFunc } from './app-func'; import { App } from './app-class'; // 1. Functional Component sample ReactDOM.render(<AppFunc />, document.getElementById('app')); // 2. Class Component sample // ReactDOM.render(<App />, document.getElementById('app'));
import * as React from 'react'; import * as ReactDOM from 'react-dom'; import GC from '@mescius/spread-sheets'; import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react'; import './styles.css'; const Component = React.Component; const spreadNS = GC.Spread.Sheets; export function AppFunc() { let spread = null; let initSpread = function (value) { spread = value; spread.suspendPaint(); var fbx = new spreadNS.FormulaTextBox.FormulaTextBox(document.getElementById('formulaBar')); fbx.workbook(spread); var oldArrayRange; var arrayFormulaStyle = new spreadNS.Style(); arrayFormulaStyle.backColor = "#E0E0FF"; for (var i = 0; i < spread.sheets.length; i++) { (function (sheet1) { sheet1.bind(spreadNS.Events.SelectionChanged, null, function (args) { sheet1.suspendPaint(); var formulaBar = document.getElementById("formulaBar"); var formulaInfo = sheet1.getFormulaInformation(sheet1.getActiveRowIndex(), sheet1.getActiveColumnIndex()); if (oldArrayRange && (!formulaInfo || !formulaInfo.isArrayFormula || !formulaInfo.baseRange.equals(oldArrayRange))) { for (var r = oldArrayRange.row; r < oldArrayRange.row + oldArrayRange.rowCount; r++) { for (var c = oldArrayRange.col; c < oldArrayRange.col + oldArrayRange.colCount; c++) { sheet1.setStyle(r, c, null); } } oldArrayRange = null; } if (formulaInfo && formulaInfo.hasFormula) { var formula = "=" + formulaInfo.formula; formula = formulaInfo.isArrayFormula ? "{" + formula + "}" : formula; formulaBar.value = formula; if (formulaInfo.isArrayFormula && !formulaInfo.baseRange.equals(oldArrayRange)) { oldArrayRange = formulaInfo.baseRange; for (var r = oldArrayRange.row; r < oldArrayRange.row + oldArrayRange.rowCount; r++) { for (var c = oldArrayRange.col; c < oldArrayRange.col + oldArrayRange.colCount; c++) { sheet1.setStyle(r, c, arrayFormulaStyle); } } } } else { formulaBar.value = sheet1.getValue(sheet1.getActiveRowIndex(), sheet1.getActiveColumnIndex()); } sheet1.resumePaint(); }); })(spread.sheets[i]); } setBasic(spread.sheets[0]); spread.resumePaint(); } let setBasic = function (sheet) { sheet.name("Simple"); sheet.setArray(0, 0, [[1, 2, 3, 4, 5, 6], [4, 5, 6], [7, 8, 9], [10, 11, 12]]); sheet.addSpan(0, 7, 4, 3); sheet.setValue(0, 7, "Set array formula by:\n 1st select the cell / range;\n 2nd enter the formula;\n last press Ctrl+Shift+Enter."); setDescriptionStyle(sheet.getCell(0, 7)); sheet.setRowHeight(5, 40); sheet.addSpan(5, 0, 1, 3); sheet.setValue(5, 0, "Array formula use constants\n ex. ={1,2,3}*{4;5;6}"); setDescriptionStyle(sheet.getCell(5, 0)); sheet.setArrayFormula(6, 0, 3, 3, "={1,2,3}*{4;5;6}"); sheet.addSpan(5, 4, 1, 3); sheet.setValue(5, 4, "Array formula use range\n ex. =A1:A4*A1:C1"); setDescriptionStyle(sheet.getCell(5, 4)); sheet.setArrayFormula(6, 4, 4, 3, "=A1:A4*A1:C1"); sheet.addSpan(5, 8, 1, 4); sheet.setValue(5, 8, "Array formula use range & function\n ex. =SUM(A1:A4*A1:C1)"); setDescriptionStyle(sheet.getCell(5, 8)); sheet.setArrayFormula(6, 8, 4, 2, "=SUM(A1:A4*A1:C1)"); sheet.setRowHeight(11, 40); sheet.addSpan(11, 0, 1, 4); sheet.setValue(11, 0, "Array formula copy column range\n ex. =A1:A2"); setDescriptionStyle(sheet.getCell(11, 0)); sheet.setArrayFormula(12, 0, 2, 3, "=A1:A2"); sheet.addSpan(11, 5, 1, 3); sheet.setValue(11, 5, "Array formula copy row range\n ex. =A1:B1"); setDescriptionStyle(sheet.getCell(11, 5)); sheet.setArrayFormula(12, 5, 4, 2, "=A1:B1"); sheet.setRowHeight(17, 40); sheet.addSpan(17, 0, 1, 4); sheet.setValue(17, 0, "Array formula out of range value is #N/A:\n ex. ={1,2,3}*{4;5;6}"); setDescriptionStyle(sheet.getCell(17, 0)); sheet.setArrayFormula(18, 0, 4, 4, "={1,2,3}*{4;5;6}"); } let setDescriptionStyle = function (cell) { cell.wordWrap(true).backColor("Accent 5 80").vAlign(spreadNS.VerticalAlign.center); } return (<div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => initSpread(spread)}> <Worksheet> </Worksheet> </SpreadSheets> </div> <Panel /> </div>); } class Panel extends Component { constructor(props) { super(props); } render() { return ( <div class="options-container"> <textarea id="formulaBar" rows={10} readOnly="readonly" /> </div> ) } }
import * as React from 'react'; import * as ReactDOM from 'react-dom'; import GC from '@mescius/spread-sheets'; import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react'; import './styles.css'; const Component = React.Component; const spreadNS = GC.Spread.Sheets; export class App extends Component { constructor(props) { super(props); this.spread = null; } render() { return <div class="sample-tutorial"> <div class="sample-spreadsheets"> <SpreadSheets workbookInitialized={spread => this.initSpread(spread)}> <Worksheet> </Worksheet> </SpreadSheets> </div> <Panel /> </div>; } initSpread(spread) { this.spread = spread; spread.suspendPaint(); var fbx = new spreadNS.FormulaTextBox.FormulaTextBox(document.getElementById('formulaBar')); fbx.workbook(spread); var oldArrayRange; var arrayFormulaStyle = new spreadNS.Style(); arrayFormulaStyle.backColor = "#E0E0FF"; for (var i = 0; i < spread.sheets.length; i++) { (function (sheet1) { sheet1.bind(spreadNS.Events.SelectionChanged, null, function (args) { sheet1.suspendPaint(); var formulaBar = document.getElementById("formulaBar"); var formulaInfo = sheet1.getFormulaInformation(sheet1.getActiveRowIndex(), sheet1.getActiveColumnIndex()); if (oldArrayRange && (!formulaInfo || !formulaInfo.isArrayFormula || !formulaInfo.baseRange.equals(oldArrayRange))) { for (var r = oldArrayRange.row; r < oldArrayRange.row + oldArrayRange.rowCount; r++) { for (var c = oldArrayRange.col; c < oldArrayRange.col + oldArrayRange.colCount; c++) { sheet1.setStyle(r, c, null); } } oldArrayRange = null; } if (formulaInfo && formulaInfo.hasFormula) { var formula = "=" + formulaInfo.formula; formula = formulaInfo.isArrayFormula ? "{" + formula + "}" : formula; formulaBar.value = formula; if (formulaInfo.isArrayFormula && !formulaInfo.baseRange.equals(oldArrayRange)) { oldArrayRange = formulaInfo.baseRange; for (var r = oldArrayRange.row; r < oldArrayRange.row + oldArrayRange.rowCount; r++) { for (var c = oldArrayRange.col; c < oldArrayRange.col + oldArrayRange.colCount; c++) { sheet1.setStyle(r, c, arrayFormulaStyle); } } } } else { formulaBar.value = sheet1.getValue(sheet1.getActiveRowIndex(), sheet1.getActiveColumnIndex()); } sheet1.resumePaint(); }); })(spread.sheets[i]); } this.setBasic(spread.sheets[0]); spread.resumePaint(); } setBasic(sheet) { sheet.name("Simple"); sheet.setArray(0, 0, [[1, 2, 3, 4, 5, 6], [4, 5, 6], [7, 8, 9], [10, 11, 12]]); sheet.addSpan(0, 7, 4, 3); sheet.setValue(0, 7, "Set array formula by:\n 1st select the cell / range;\n 2nd enter the formula;\n last press Ctrl+Shift+Enter."); this.setDescriptionStyle(sheet.getCell(0, 7)); sheet.setRowHeight(5, 40); sheet.addSpan(5, 0, 1, 3); sheet.setValue(5, 0, "Array formula use constants\n ex. ={1,2,3}*{4;5;6}"); this.setDescriptionStyle(sheet.getCell(5, 0)); sheet.setArrayFormula(6, 0, 3, 3, "={1,2,3}*{4;5;6}"); sheet.addSpan(5, 4, 1, 3); sheet.setValue(5, 4, "Array formula use range\n ex. =A1:A4*A1:C1"); this.setDescriptionStyle(sheet.getCell(5, 4)); sheet.setArrayFormula(6, 4, 4, 3, "=A1:A4*A1:C1"); sheet.addSpan(5, 8, 1, 4); sheet.setValue(5, 8, "Array formula use range & function\n ex. =SUM(A1:A4*A1:C1)"); this.setDescriptionStyle(sheet.getCell(5, 8)); sheet.setArrayFormula(6, 8, 4, 2, "=SUM(A1:A4*A1:C1)"); sheet.setRowHeight(11, 40); sheet.addSpan(11, 0, 1, 4); sheet.setValue(11, 0, "Array formula copy column range\n ex. =A1:A2"); this.setDescriptionStyle(sheet.getCell(11, 0)); sheet.setArrayFormula(12, 0, 2, 3, "=A1:A2"); sheet.addSpan(11, 5, 1, 3); sheet.setValue(11, 5, "Array formula copy row range\n ex. =A1:B1"); this.setDescriptionStyle(sheet.getCell(11, 5)); sheet.setArrayFormula(12, 5, 4, 2, "=A1:B1"); sheet.setRowHeight(17, 40); sheet.addSpan(17, 0, 1, 4); sheet.setValue(17, 0, "Array formula out of range value is #N/A:\n ex. ={1,2,3}*{4;5;6}"); this.setDescriptionStyle(sheet.getCell(17, 0)); sheet.setArrayFormula(18, 0, 4, 4, "={1,2,3}*{4;5;6}"); } setDescriptionStyle(cell) { cell.wordWrap(true).backColor("Accent 5 80").vAlign(spreadNS.VerticalAlign.center); } } class Panel extends Component { constructor(props) { super(props); } render() { return ( <div class="options-container"> <textarea id="formulaBar" rows={10} readOnly="readonly" /> </div> ) } }
<!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/react/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <!-- SystemJS --> <script src="$DEMOROOT$/en/react/node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('$DEMOROOT$/en/lib/react/license.js').then(function () { System.import('./src/app'); }); </script> </head> <body> <div id="app"></div> </body> </html>
.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: #fbfbfb; overflow: auto; } textarea { width: 100%; box-sizing: border-box; } 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: { '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-react': 'npm:@mescius/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/umd/react.production.min.js', 'react-dom': 'npm:react-dom/umd/react-dom.production.min.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);