使用sortRange方法可以对指定区域进行排序,如下所示:
用户也可以使用上下文菜单排序项或过滤框中对数据进行排序。
不同数据的排序优先级为:布尔 > 字符串 > 数值,例如 True > 'A' > 8.
自定义排序
用户可以使用回调函数定义自定义的compare方法进行排序。
以下代码显示了如何使用localCompare进行排序。
根据单元格颜色排序
用户可以通过给定sort info使单元格根据单元格的颜色排序。这个功能支持纯色填充,图案填充和渐变填充。
以下代码显示了如何使用背景颜色进行排序。
根据字体颜色排序
用户可以通过给定sort info使单元格根据单元格的字体颜色排序. 这个功能仅支持纯色填充。
用户可以定义引发RangeSorting事件时使用的回调函数。
Group sort
你可以使用groupSort函数来设置是否将数据分组,GroupSort提供以下的方式:
flat:排序忽略分组
group:仅在组与组之间排序,组内不进行排序
child:只在组内排序
full:排序时移动组,并且对组内部进行排序
下面的代码展示了组排序选项。
用户可以定义在RangeSorting事件发生时要使用的groupSorting选项。
排序忽略隐藏部分
你能够设置当排序时是否要忽略被隐藏的值,
当ignoreHidden设置为true, spread会跳过并且不移动被隐藏的值。
当ignoreHidden设置为false, spread会比较并移动被隐藏的值。
当groupSort设置为group/child/full, SpreadJS会移动被隐藏的值并且移动行/列的可见性并隐藏值。
下面的代码会展示使用ignoreHidden选项.
用户可以定义RangeSorting事件发生时要使用ignoreHidden选项。
默认情况下,如果排序范围包括组,SpreadJS将使用group排序对数据进行排序。否则,它将使用flat排序并且忽略隐藏。
保持上次的排序状态
最后一次排序操作设置将由worksheet进行记录,您可以使用getSortState函数获取最后一次排序状态。
下面的代码展示了如何使用getSortState方法.
现在,您可以在不传递任何参数的情况下调用sortRange来触发重新排序,重新排序操作基于最后一次排序状态。
用户可以使用bind函数联合sortRange来实现自动排序。以下是一个简单的代码示例:
上述代码将在工作表中的值发生更改时自动调用,因此每当您在工作表中更改值时,将触发重新排序操作。
import { Component, NgModule, enableProdMode } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import { FormsModule } from '@angular/forms';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { SpreadSheetsModule } from '@grapecity-software/spread-sheets-angular';
import GC from '@grapecity-software/spread-sheets';
import './styles.css';
const spreadNS = GC.Spread.Sheets, SheetArea = spreadNS.SheetArea;
const CELL_COLOR_MAPPING = {
red: "#FF0000",
green: "#00B050",
blue: "#00B0F0",
gradient: {
degree: 90,
stops: [
{
color: "#ffffff",
position: 0,
},
{
color: "#5B9BD5",
position: 1,
}
]
},
pattern: {
patternColor: "",
type: 14,
backgroundColor: ""
}
}
const FONT_COLOR_MAPPING = {
red: "#FF0000",
blue: "#00B0F0",
purple: "#7030A0",
green: "#92D050",
null: ""
}
@Component({
selector: 'app-component',
templateUrl: 'src/app.component.html'
})
export class AppComponent {
spread: GC.Spread.Sheets.Workbook;
hostStyle = {
width: 'calc(100% - 280px)',
height: '100%',
overflow: 'hidden',
float: 'left'
};
constructor() {
}
initSpread($event: any) {
let spread = this.spread = $event.spread;
spread.suspendPaint();
spread.fromJSON(sjsData);
this.initSheet0(spread.getSheet(0));
this.initSheet1(spread.getSheet(1));
this.initSheet2(spread.getSheet(2));
this.initSheet3(spread.getSheet(3));
this.initSheet4(spread.getSheet(4));
this.initSheet5(spread.getSheet(5));
this.initSheet6(spread.getSheet(6));
this.initSheet7(spread.getSheet(7));
this.initSortStatePanel(spread);
spread.resumePaint();
}
initSheet0(sheet: GC.Spread.Sheets.Worksheet) {
var style = sheet.getStyle(4, 7);
style.cellButtons = [{
useButtonStyle: true,
caption: "Sort by last name",
width: 222,
command: function() {
sheet.sortRange(4, 0, 27, 5, true, [
{
index: 1,
ascending: true,
compareFunction: function(value1, value2) {
var str1 = value1.split(" ")[1], str2 = value2.split(" ")[1];
return str1.localeCompare(str2);
}
},
])
},
}];
sheet.setStyle(4, 7, style);
var grade = ["Freshmen", "Sophomore", "Junior", "Senior"];
var clothesSize = ["XX-Small", "X-Small", "Small", "Medium", "Large", "X-Large", "XX-Large"];
function compareList(obj1: Object, obj2: Object, list: any[]) {
var index1 = list.indexOf(obj1), index2 = list.indexOf(obj2);
if (index1 > index2) {
return 1;
} else if (index1 < index2) {
return -1;
} else {
return 0;
}
}
style = sheet.getStyle(5, 7);
style.cellButtons = [{
useButtonStyle: true,
caption: "Sort by Grade",
width: 222,
command: function() {
sheet.sortRange(4, 0, 27, 5, true, [
{
index: 2,
ascending: true,
compareFunction: function(value1, value2) {
return compareList(value1, value2, grade);
}
},
])
},
}];
sheet.setStyle(5, 7, style);
style = sheet.getStyle(6, 7);
style.cellButtons = [{
useButtonStyle: true,
caption: "Sort by T-Shirt Size",
width: 222,
command: function() {
sheet.sortRange(4, 0, 27, 5, true, [
{
index: 3,
ascending: true,
compareFunction: function(value1, value2) {
return compareList(value1, value2, clothesSize);
}
},
])
},
}];
sheet.setStyle(6, 7, style);
}
initSheet1(sheet: GC.Spread.Sheets.Worksheet) {
function sortDomain(value1: string, value2: string) {
var str1 = value1.substr(value1.lastIndexOf(".") + 1), str2 = value2.substr(value2.lastIndexOf(".") + 1);
return str1.localeCompare(str2);
}
function sortIP(ip1: string, ip2: string) {
var value1 = ip1.split("."), value2 = ip2.split(".");
for (var i = 0; i < 4; i++) {
var num1 = parseInt(value1[i]), num2 = parseInt(value2[i]);
if (num1 > num2) {
return 1;
} else if (num1 < num2) {
return -1;
}
}
return 0;
}
sheet.bind(GC.Spread.Sheets.Events.RangeSorting, function(e, info) {
if (info.col === 0) {
info.compareFunction = sortDomain;
} else if (info.col === 1) {
info.compareFunction = sortIP;
}
});
}
initSheet2(sheet: GC.Spread.Sheets.Worksheet) {
sheet.bind(GC.Spread.Sheets.Events.RangeSorting, function(e, info) {
info.groupSort = GC.Spread.Sheets.GroupSort.full;
});
}
initSheet3(sheet: GC.Spread.Sheets.Worksheet) {
sheet.outlineColumn.options({
columnIndex: 0,
showImage: false,
showIndicator: true,
showCheckBox: true,
maxLevel: 10
});
}
initSheet4(sheet: GC.Spread.Sheets.Worksheet) {
var style = sheet.getStyle(1, 4);
style.cellButtons = [
{
useButtonStyle: true,
caption: "ignoreHidden = true",
command: function() {
sheet.sortRange(2, 0, 15, 1, true, [
{
index: 0,
ascending: sheet.getValue(1, 3) === '1',
},
], { ignoreHidden: true });
},
}, {
useButtonStyle: true,
caption: "ignoreHidden = false",
command: function() {
sheet.sortRange(2, 0, 15, 1, true, [
{
index: 0,
ascending: sheet.getValue(1, 3) === '1',
},
], { ignoreHidden: false });
},
}, {
useButtonStyle: true,
caption: "groupSort = group",
command: function() {
sheet.sortRange(2, 0, 15, 1, true, [
{
index: 0,
ascending: sheet.getValue(1, 3) === '1',
},
], { groupSort: GC.Spread.Sheets.GroupSort.group });
},
}];
sheet.setStyle(1, 4, style);
}
initSheet5(sheet: GC.Spread.Sheets.Worksheet) {
sheet.setColumnWidth(4, 120);
var style = new GC.Spread.Sheets.Style();
style.cellButtons = [{
caption: "Sort By Cell Color",
useButtonStyle: true,
width: 120,
command: function(sheet: GC.Spread.Sheets.Worksheet) {
var value = sheet.getValue(15, 3);
var order = sheet.getValue(15, 4);
value = value ? value : "red";
order = order ? order : "top";
var color = CELL_COLOR_MAPPING[value];
sheet.sortRange(3, 2, 10, 1, true, [{
index: 2,
backColor: color,
order: order,
}])
}
}];
sheet.setStyle(16, 4, style);
}
initSheet6(sheet: GC.Spread.Sheets.Worksheet) {
sheet.setColumnWidth(4, 120);
var style = new GC.Spread.Sheets.Style();
style.cellButtons = [{
caption: "Sort By Font Color",
useButtonStyle: true,
width: 120,
command: function(sheet: GC.Spread.Sheets.Worksheet) {
var value = sheet.getValue(15, 3);
var order = sheet.getValue(15, 4);
value = value ? value : "red";
order = order ? order : "top";
var color = FONT_COLOR_MAPPING[value];
sheet.sortRange(3, 2, 10, 1, true, [{
index: 2,
fontColor: color,
order: order
}])
}
}];
sheet.setStyle(16, 4, style);
}
initSheet7(sheet: GC.Spread.Sheets.Worksheet) {
function inSortStateRange(sortState : any, row: number, col: number) {
if (row >= sortState.row && row < sortState.row + sortState.rowCount && col >= sortState.col && col < sortState.col + sortState.colCount) {
return true;
}
return false;
}
sheet.sortRange(2, 2, 10, 1, true, [{ index: 2, ascending: false, compareFunction: undefined }]);
sheet.setSelection(2, 2, 10, 1);
sheet.bind(GC.Spread.Sheets.Events.ValueChanged, function(e, info) {
let sortState = sheet.getSortState();
if (inSortStateRange(sortState, info.row, info.col)) {
sheet.sortRange();
}
});
}
initSortStatePanel(spread: GC.Spread.Sheets.Workbook) {
this._getElementById('get_SortState_Btn').addEventListener('click', function() {
let sheet = spread.getActiveSheet();
let sortState = sheet.getSortState();
if (!sortState) {
return;
}
let { row, col, rowCount, colCount, byRow, sortConditions } = sortState;
if (sortState) {
let sortStateStr = '';
sortStateStr += "row: " + row + ",\n";
sortStateStr += "col: " + col + ",\n";
sortStateStr += "rowCount: " + rowCount + ",\n";
sortStateStr += "colCount: " + colCount + ",\n";
sortStateStr += "byRow: " + byRow + ",\n";
sortStateStr += "sortCondition: " + JSON.stringify(sortConditions); +"}\n";
document.getElementById("showEventArgs").value = sortStateStr;
}
});
}
_getElementById(id:string) {
return document.getElementById(id);
}
}
@NgModule({
imports: [BrowserModule, SpreadSheetsModule, FormsModule],
declarations: [AppComponent],
exports: [AppComponent],
bootstrap: [AppComponent]
})
export class AppModule { }
enableProdMode();
// Bootstrap application with hash style navigation and global services.
platformBrowserDynamic().bootstrapModule(AppModule);
<!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/angular/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/spread/source/data/sorting.js" type="text/javascript"></script>
<!-- Polyfills -->
<script src="$DEMOROOT$/zh/angular/node_modules/core-js/client/shim.min.js"></script>
<script src="$DEMOROOT$/zh/angular/node_modules/zone.js/fesm2015/zone.min.js"></script>
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/angular/node_modules/systemjs/dist/system.js"></script>
<script src="systemjs.config.js"></script>
<script>
// workaround to load 'rxjs/operators' from the rxjs bundle
System.import('rxjs').then(function (m) {
System.import('@angular/compiler');
System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators));
System.import('$DEMOROOT$/zh/lib/angular/license.ts');
System.import('./src/app.component');
});
</script>
</head>
<body>
<app-component></app-component>
</body>
</html>
<div class="sample-tutorial groupAppearanceSs">
<gc-spread-sheets [hostStyle]="hostStyle" (workbookInitialized)="initSpread($event)">
<gc-worksheet></gc-worksheet>
<gc-worksheet></gc-worksheet>
<gc-worksheet></gc-worksheet>
<gc-worksheet></gc-worksheet>
<gc-worksheet></gc-worksheet>
</gc-spread-sheets>
<div class="options-container">
<div id="settingsDiv">
<br/>
<label>This text box shows sortState information about the last sort action.</label>
<br/>
<textarea id="showEventArgs" cols="85" rows="8" style="max-width: 98%"></textarea>
<div class="option-row">
<input type="button" id="get_SortState_Btn" value="Get Sort State"/>
</div>
</div>
</div>
</div>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
input {
display: inline-block;
}
input[type="text"] {
width: 200px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.groupAppearanceSs{
width: 100%;
height: 100%;
}
.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;
}
label {
display: block;
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
(function(global) {
System.config({
transpiler: 'ts',
typescriptOptions: {
tsconfig: true
},
meta: {
'typescript': {
"exports": "ts"
},
'*.css': { loader: 'css' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'core-js': 'npm:core-js/client/shim.min.js',
'zone': 'npm:zone.js/fesm2015/zone.min.js',
'rxjs': 'npm:rxjs/dist/bundles/rxjs.umd.min.js',
'@angular/core': 'npm:@angular/core/fesm2022',
'@angular/common': 'npm:@angular/common/fesm2022/common.mjs',
'@angular/compiler': 'npm:@angular/compiler/fesm2022/compiler.mjs',
'@angular/platform-browser': 'npm:@angular/platform-browser/fesm2022/platform-browser.mjs',
'@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/fesm2022/platform-browser-dynamic.mjs',
'@angular/common/http': 'npm:@angular/common/fesm2022/http.mjs',
'@angular/router': 'npm:@angular/router/fesm2022/router.mjs',
'@angular/forms': 'npm:@angular/forms/fesm2022/forms.mjs',
'jszip': 'npm:jszip/dist/jszip.min.js',
'typescript': 'npm:typescript/lib/typescript.js',
'ts': './plugin.js',
'tslib':'npm:tslib/tslib.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',
'@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-angular': 'npm:@grapecity-software/spread-sheets-angular/fesm2020/grapecity-software-spread-sheets-angular.mjs',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'ts'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
},
"node_modules/@angular": {
defaultExtension: 'mjs'
},
"@grapecity-software/spread-sheets-angular": {
defaultExtension: 'mjs'
},
'@angular/core': {
defaultExtension: 'mjs',
main: 'core.mjs'
}
}
});
})(this);