워크시트에서 배열을 더 쉽게 조작할 수 있도록 SpreadJS에는 11개 배열 조작 함수로 구성된 컬렉션이 있습니다.
배열 결합
데이터 결합은 어려울 수 있는데, 데이터 소스의 크기가 유연한 경우 특히 그렇습니다. VSTACK과 HSTACK을 사용하면 동적 배열을 쉽게 결합하여 데이터를 세로 또는 가로로 누적할 수 있습니다.
VSTACK - 세로로 배열 누적
HSTACK- 가로로 배열 누적
배열 셰이프 지정
데이터의 "셰이프"를 변경하는 것은 어려울 수 있는데, 배열에서 목록으로 변경하는 경우가 특히 어렵고 그 반대의 경우도 마찬가지입니다. 2차원 배열을 간단한 목록으로 변환하려는 경우에는 TOROW와 TOCOL을 사용하여 2D 배열을 데이터의 단일 행 또는 열로 변환합니다.
WRAPROWS 및 WRAPCOLS 함수를 사용하여 반대 작업을 수행합니다. 선택한 너비/높이 제한에 도달하면 (이 문서의 텍스트처럼) 데이터를 다음 행으로 "래핑"하여 지정된 너비 또는 높이의 2D 배열을 만듭니다.
TOROW - 하나의 행으로 배열 반환
TOCOL - 하나의 열로 배열 반환
WRAPROWS - 행 배열을 2D 배열로 래핑
WRAPCOLS - 행 배열을 2D 배열로 래핑
배열 크기 조정
TAKE 및 DROP 함수를 사용하면 배열 시작 지점 또는 끝 지점에서 유지하거나 제거할 행 수를 지정하여 배열을 줄일 수 있습니다.
CHOOSEROWS 또는 CHOOSECOLS를 사용하면 인덱스로 배열의 특정 행 또는 열을 선택할 수 있습니다.
EXPAND를 사용하면 배열을 선택한 크기로 늘릴 수 있습니다. 추가 공간을 채울 값과 새로운 차원만 제공하면 됩니다.
TAKE - 배열 시작 또는 끝에서 행 또는 열 반환
DROP - 배열 시작 또는 끝에서 행 또는 열 드롭
CHOOSEROWS - 배열에서 지정된 행 반환
CHOOSECOLS - 배열에서 지정된 열 반환
EXPAND - 지정된 치수로 배열 확장
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 { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react';
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-resources-ko';
GC.Spread.Common.CultureManager.culture("ko-kr");
import './styles.css';
const Component = React.Component;
export function AppFunc() {
const initSpread=(workbook)=> {
var data = [
{
sheetName: 'VSTACK',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", null, "AA", "BB", "CC"],
["D", "E", "F", null, "DD", "EE", "FF"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A7)"],
[{ v: "Result", s: "title" }],
["=VSTACK(A2:C3,E2:G3)"]
],
columnsWidth: [69]
},
{
sheetName: 'HSTACK',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", null, "AA", "BB", "CC"],
["D", "E", "F", null, "DD", "EE", "FF"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A7)"],
[{ v: "Result", s: "title" }],
["=HSTACK(A2:C3,E2:G3)"]
],
columnsWidth: [69]
},
{
sheetName: 'TOROW',
cells: [
[{ v: "Data", s: "title" }],
["Ben", "Peter", "Mary", "Sam"],
["John", "Hillary", "Jenny", "James"],
["Agnes", "Harry", "Felicity", "Joe"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A8)"],
[{ v: "Result", s: "title" }],
["=TOROW(A2:D4)"]
],
columnsWidth: [69]
},
{
sheetName: 'TOCOL',
cells: [
[{ v: "Data", s: "title" }],
["Ben", "Peter", "Mary", "Sam"],
["John", "Hillary", "Jenny", "James"],
["Agnes", "Harry", "Felicity", "Joe"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A8)"],
[{ v: "Result", s: "title" }],
["=TOCOL(A2:D4)"]
],
columnsWidth: [69]
},
{
sheetName: 'WRAPROWS',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", "D", "E", "F", "G"],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B5)"],
[{ v: "Result", s: "title" }, "=WRAPROWS(A2:G2,3)"],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, '=WRAPROWS(A2:G2,3,"x")'],
],
columnsWidth: [69]
},
{
sheetName: 'WRAPCOLS',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", "D", "E", "F", "G"],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B5)"],
[{ v: "Result", s: "title" }, "=WRAPCOLS(A2:G2,3)"],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, '=WRAPCOLS(A2:G2,3,"x")'],
],
columnsWidth: [69]
},
{
sheetName: 'TAKE',
cells: [
[{ v: "Data", s: "title" }],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B7)"],
[{ v: "Result", s: "title" }, "=TAKE(A2:C4,2)"],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B11)"],
[{ v: "Result", s: "title" }, '=TAKE(A2:C4,,2)'],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B16)"],
[{ v: "Result", s: "title" }, '=TAKE(A2:C4,-2)'],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B20)"],
[{ v: "Result", s: "title" }, '=TAKE(A2:C4,2,2)'],
],
columnsWidth: [69]
},
{
sheetName: 'DROP',
cells: [
[{ v: "Data", s: "title" }],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B7)"],
[{ v: "Result", s: "title" }, "=DROP(A2:C4,2)"],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, '=DROP(A2:C4,,2)'],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B15)"],
[{ v: "Result", s: "title" }, '=DROP(A2:C4,-2)'],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B18)"],
[{ v: "Result", s: "title" }, '=DROP(A2:C4,2,2)'],
],
columnsWidth: [69]
},
{
sheetName: 'CHOOSEROWS',
cells: [
[{ v: "Data", s: "title" }],
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10],
[11, 12],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, "=CHOOSEROWS(A2:B7,1,3,5,1)"],
[],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B16)"],
[{ v: "Result", s: "title" }, '=CHOOSEROWS(A2:B7,-1,-2)'],
],
columnsWidth: [69]
},
{
sheetName: 'CHOOSECOLS',
cells: [
[{ v: "Data", s: "title" }],
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B9)"],
[{ v: "Result", s: "title" }, "=CHOOSECOLS(A2:E6,1,3,5,1)"],
[],
[],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B16)"],
[{ v: "Result", s: "title" }, '=CHOOSECOLS(A2:E6,-1,-2)'],
],
columnsWidth: [69]
},
{
sheetName: 'EXPAND',
cells: [
[{ v: "Data", s: "title" }],
[1, 2],
[3, 4],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B6)"],
[{ v: "Result", s: "title" }, "=EXPAND(A2:B3,3,3)"],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B11)"],
[{ v: "Result", s: "title" }, '=EXPAND(A2:B3,3,3, "-")'],
],
columnsWidth: [69]
},
];
workbook.options.allowDynamicArray = true;
workbook.suspendPaint();
workbook.suspendCalcService();
initStyles(workbook);
workbook.setSheetCount(data.length);
for (var i = 0; i < data.length; i++) {
var sheetData = data[i];
var sheet = workbook.sheets[i];
sheet.name(sheetData.sheetName);
setCells(sheet, sheetData.cells, 0, 0);
setColumnsWidth(sheet, sheetData.columnsWidth);
}
workbook.resumeCalcService();
workbook.resumePaint();
}
const initStyles=(workbook)=> {
var style = new GC.Spread.Sheets.Style();
style.name = 'title';
style.font = 'normal bold 16px Segoe UI';
style.foreColor = "#172b4d";
workbook.addNamedStyle(style);
}
const setCells=(sheet, cells, rowIndex, colIndex)=> {
for (var i = 0; i < cells.length; i++) {
var row = cells[i];
var r = rowIndex + i;
for (var j = 0; j < row.length; j++) {
var cell = row[j];
var c = colIndex + j;
if (cell === null) {
continue;
}
if (typeof cell === "object") {
if (cell.v !== undefined) {
sheet.setValue(r, c, cell.v);
}
if (cell.s !== undefined) {
sheet.setStyle(r, c, cell.s)
}
} else if (cell[0] === '=') {
sheet.setFormula(r, c, cell);
} else {
sheet.setValue(r, c, cell);
}
}
}
}
const setColumnsWidth=(sheet, columnsWidth)=> {
if (!columnsWidth) {
return;
}
for (var i = 0; i < columnsWidth.length; i++) {
sheet.setColumnWidth(i, columnsWidth[i]);
}
}
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => initSpread(spread)}>
<Worksheet>
</Worksheet>
</SpreadSheets>
</div>
</div>
);
}
import * as React from 'react';
import { SpreadSheets, Worksheet } from '@mescius/spread-sheets-react';
import GC from '@mescius/spread-sheets';
import '@mescius/spread-sheets-resources-ko';
GC.Spread.Common.CultureManager.culture("ko-kr");
import './styles.css';
const Component = React.Component;
export class App extends Component {
render() {
return (
<div class="sample-tutorial">
<div class="sample-spreadsheets">
<SpreadSheets workbookInitialized={spread => this.initSpread(spread)}>
<Worksheet>
</Worksheet>
</SpreadSheets>
</div>
</div>
);
}
initSpread(workbook) {
var data = [
{
sheetName: 'VSTACK',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", null, "AA", "BB", "CC"],
["D", "E", "F", null, "DD", "EE", "FF"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A7)"],
[{ v: "Result", s: "title" }],
["=VSTACK(A2:C3,E2:G3)"]
],
columnsWidth: [69]
},
{
sheetName: 'HSTACK',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", null, "AA", "BB", "CC"],
["D", "E", "F", null, "DD", "EE", "FF"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A7)"],
[{ v: "Result", s: "title" }],
["=HSTACK(A2:C3,E2:G3)"]
],
columnsWidth: [69]
},
{
sheetName: 'TOROW',
cells: [
[{ v: "Data", s: "title" }],
["Ben", "Peter", "Mary", "Sam"],
["John", "Hillary", "Jenny", "James"],
["Agnes", "Harry", "Felicity", "Joe"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A8)"],
[{ v: "Result", s: "title" }],
["=TOROW(A2:D4)"]
],
columnsWidth: [69]
},
{
sheetName: 'TOCOL',
cells: [
[{ v: "Data", s: "title" }],
["Ben", "Peter", "Mary", "Sam"],
["John", "Hillary", "Jenny", "James"],
["Agnes", "Harry", "Felicity", "Joe"],
[{ v: "Formula", s: "title" }],
["=FORMULATEXT(A8)"],
[{ v: "Result", s: "title" }],
["=TOCOL(A2:D4)"]
],
columnsWidth: [69]
},
{
sheetName: 'WRAPROWS',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", "D", "E", "F", "G"],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B5)"],
[{ v: "Result", s: "title" }, "=WRAPROWS(A2:G2,3)"],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, '=WRAPROWS(A2:G2,3,"x")'],
],
columnsWidth: [69]
},
{
sheetName: 'WRAPCOLS',
cells: [
[{ v: "Data", s: "title" }],
["A", "B", "C", "D", "E", "F", "G"],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B5)"],
[{ v: "Result", s: "title" }, "=WRAPCOLS(A2:G2,3)"],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, '=WRAPCOLS(A2:G2,3,"x")'],
],
columnsWidth: [69]
},
{
sheetName: 'TAKE',
cells: [
[{ v: "Data", s: "title" }],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B7)"],
[{ v: "Result", s: "title" }, "=TAKE(A2:C4,2)"],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B11)"],
[{ v: "Result", s: "title" }, '=TAKE(A2:C4,,2)'],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B16)"],
[{ v: "Result", s: "title" }, '=TAKE(A2:C4,-2)'],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B20)"],
[{ v: "Result", s: "title" }, '=TAKE(A2:C4,2,2)'],
],
columnsWidth: [69]
},
{
sheetName: 'DROP',
cells: [
[{ v: "Data", s: "title" }],
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B7)"],
[{ v: "Result", s: "title" }, "=DROP(A2:C4,2)"],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, '=DROP(A2:C4,,2)'],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B15)"],
[{ v: "Result", s: "title" }, '=DROP(A2:C4,-2)'],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B18)"],
[{ v: "Result", s: "title" }, '=DROP(A2:C4,2,2)'],
],
columnsWidth: [69]
},
{
sheetName: 'CHOOSEROWS',
cells: [
[{ v: "Data", s: "title" }],
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10],
[11, 12],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B10)"],
[{ v: "Result", s: "title" }, "=CHOOSEROWS(A2:B7,1,3,5,1)"],
[],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B16)"],
[{ v: "Result", s: "title" }, '=CHOOSEROWS(A2:B7,-1,-2)'],
],
columnsWidth: [69]
},
{
sheetName: 'CHOOSECOLS',
cells: [
[{ v: "Data", s: "title" }],
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B9)"],
[{ v: "Result", s: "title" }, "=CHOOSECOLS(A2:E6,1,3,5,1)"],
[],
[],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B16)"],
[{ v: "Result", s: "title" }, '=CHOOSECOLS(A2:E6,-1,-2)'],
],
columnsWidth: [69]
},
{
sheetName: 'EXPAND',
cells: [
[{ v: "Data", s: "title" }],
[1, 2],
[3, 4],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B6)"],
[{ v: "Result", s: "title" }, "=EXPAND(A2:B3,3,3)"],
[],
[],
[],
[{ v: "Formula", s: "title" }, "=FORMULATEXT(B11)"],
[{ v: "Result", s: "title" }, '=EXPAND(A2:B3,3,3, "-")'],
],
columnsWidth: [69]
},
];
workbook.options.allowDynamicArray = true;
workbook.suspendPaint();
workbook.suspendCalcService();
this.initStyles(workbook);
workbook.setSheetCount(data.length);
for (var i = 0; i < data.length; i++) {
var sheetData = data[i];
var sheet = workbook.sheets[i];
sheet.name(sheetData.sheetName);
this.setCells(sheet, sheetData.cells, 0, 0);
this.setColumnsWidth(sheet, sheetData.columnsWidth);
}
workbook.resumeCalcService();
workbook.resumePaint();
}
initStyles(workbook) {
var style = new GC.Spread.Sheets.Style();
style.name = 'title';
style.font = 'normal bold 16px Segoe UI';
style.foreColor = "#172b4d";
workbook.addNamedStyle(style);
}
setCells(sheet, cells, rowIndex, colIndex) {
for (var i = 0; i < cells.length; i++) {
var row = cells[i];
var r = rowIndex + i;
for (var j = 0; j < row.length; j++) {
var cell = row[j];
var c = colIndex + j;
if (cell === null) {
continue;
}
if (typeof cell === "object") {
if (cell.v !== undefined) {
sheet.setValue(r, c, cell.v);
}
if (cell.s !== undefined) {
sheet.setStyle(r, c, cell.s)
}
} else if (cell[0] === '=') {
sheet.setFormula(r, c, cell);
} else {
sheet.setValue(r, c, cell);
}
}
}
}
setColumnsWidth(sheet, columnsWidth) {
if (!columnsWidth) {
return;
}
for (var i = 0; i < columnsWidth.length; i++) {
sheet.setColumnWidth(i, columnsWidth[i]);
}
}
}
<!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$/ko/react/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/ko/react/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('$DEMOROOT$/ko/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: 100%;
height: 100%;
overflow: hidden;
float: left;
}
input {
padding: 4px 6px;
}
.options-row {
font-size: 14px;
padding: 5px;
margin-top: 10px;
}
label {
display: block;
margin-bottom: 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
width:216px;
}
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',
'@mescius/spread-sheets-resources-ko': 'npm:@mescius/spread-sheets-resources-ko/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);