数组操作函数

SpreadJS包括一些函数,旨在帮助你更容易地在工作表中操作数组。

为了使工作表中的数组操作更容易,SpreadJS有11个数组操作函数的集合。 合并数组 合并数据是很有挑战性的,特别是当它们的来源是灵活的大小。通过VSTACK和HSTACK,你可以轻松地组合动态数组,垂直或水平地堆叠你的数据。  VSTACK-垂直堆叠数组 HSTACK--水平堆叠数组 塑造数组 改变数据的 "形状 "是很有挑战性的,尤其是从数组到列表,反之亦然。如果你发现自己有一个二维数组,而你想把它转换成一个简单的列表,可以使用TOROW和TOCOL把一个二维数组转换成单行或单列的数据。 使用WRAPROWS和WRAPCOLS函数,可以做相反的事情:一旦达到你选择的宽度/高度限制,通过将数据 "包裹 "到下一行(就像本文档中的文本),创建一个指定宽度或高度的二维数组。  TOROW - 以一行的形式返回数组 TOCOL - 将数组返回为一列 WRAPROWS - 将一个行数组包裹成一个二维数组 WRAPCOLS - 将一个列数包裹成一个二维数组 调整数组的大小 TAKE和DROP函数使你能够通过指定从数组开始或结束时保留或删除的行数来减少你的数组。 使用CHOOSEROWS或CHOOSECOLS,你可以通过索引从数组中挑选特定的行或列。 EXPAND允许你将一个数组扩大到你所选择的大小--你只需要提供新的尺寸和一个值来填充额外的空间。 TAKE - 从数组开始或结束时返回行或列 DROP - 从数组开始或结束时删除行或列 CHOOSEROWS - 从数组中返回指定的行 CHOOSECOLS - 从一个数组中返回指定的列 EXPAND - 将一个数组扩展到指定的尺寸
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(_getElementById("ss")); spread.options.allowDynamicArray = true; initStyles(spread); initSpread(spread); }; 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] }, ]; function initSpread(workbook) { workbook.suspendPaint(); workbook.suspendCalcService(); 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(); } function 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); } function 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); } } } } function setColumnsWidth(sheet, columnsWidth) { if (!columnsWidth) { return; } for (var i = 0; i < columnsWidth.length; i++) { sheet.setColumnWidth(i, columnsWidth[i]); } } function _getElementById(id) { return document.getElementById(id); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="spreadjs culture" content="zh-cn" /> <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-resources-zh/dist/gc.spread.sheets.resources.zh.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> </body> </html>
input[type="text"] { width: 200px; margin-right: 20px; } label { display: inline-block; width: 110px; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: 100%; height: 100%; overflow: hidden; float: left; } label { display: block; margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; width:216px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } code { border: 1px solid #000; }