类: FFilter
This interface class provides methods to modify the filter settings of a worksheet.
方法
getColumnFilterCriteria()
getColumnFilterCriteria(column): any
Get the filter criteria of a column.
参数
参数 | 类型 | 描述 |
---|---|---|
column | number | The column index. |
返回
any
The filter criteria of the column.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Set some values of the range C1:F10
const fRange = fWorksheet.getRange('C1:F10');
fRange.setValues([
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9],
[7, 8, 9, 10],
[8, 9, 10, 11],
[9, 10, 11, 12],
[10, 11, 12, 13],
]);
// Create a filter on the range C1:F10
let fFilter = fRange.createFilter();
// If the filter already exists, remove it and create a new one
if (!fFilter) {
fRange.getFilter().remove();
fFilter = fRange.createFilter();
}
// Set the filter criteria of the column C, filter out the rows that are not 1, 5, 9
const column = fWorksheet.getRange('C:C').getColumn();
fFilter.setColumnFilterCriteria(column, {
colId: 0,
filters: {
filters: ['1', '5', '9'],
},
});
// Print the filter criteria of the column C and D
console.log(fFilter.getColumnFilterCriteria(column)); // { colId: 0, filters: { filters: ['1', '5', '9'] } }
console.log(fFilter.getColumnFilterCriteria(column + 1)); // undefined
getFilteredOutRows()
getFilteredOutRows(): number[]
Get the filtered out rows by this filter.
返回
number
[]
Filtered out rows by this filter.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Set some values of the range C1:F10
const fRange = fWorksheet.getRange('C1:F10');
fRange.setValues([
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9],
[7, 8, 9, 10],
[8, 9, 10, 11],
[9, 10, 11, 12],
[10, 11, 12, 13],
]);
// Create a filter on the range C1:F10
let fFilter = fRange.createFilter();
// If the filter already exists, remove it and create a new one
if (!fFilter) {
fRange.getFilter().remove();
fFilter = fRange.createFilter();
}
// Set the filter criteria of the column C, filter out the rows that are not 1, 5, 9
const column = fWorksheet.getRange('C:C').getColumn();
fFilter.setColumnFilterCriteria(column, {
colId: 0,
filters: {
filters: ['1', '5', '9'],
},
});
// Get the filtered out rows
console.log(fFilter.getFilteredOutRows()); // [1, 2, 3, 5, 6, 7, 9]
getRange()
getRange(): FRange
Get the range of the filter.
返回
The range of the filter.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fFilter = fWorksheet.getFilter();
console.log(fFilter?.getRange().getA1Notation());
remove()
remove(): boolean
Remove the filter from the worksheet.
返回
boolean
True if the filter is removed successfully; otherwise, false.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
const fRange = fWorksheet.getRange('A1:D14');
let fFilter = fRange.createFilter();
// If the worksheet already has a filter, remove it and create a new filter.
if (!fFilter) {
fWorksheet.getFilter().remove();
fFilter = fRange.createFilter();
}
console.log(fFilter);
removeColumnFilterCriteria()
removeColumnFilterCriteria(column): FFilter
Clear the filter criteria of a column.
参数
参数 | 类型 | 描述 |
---|---|---|
column | number | The column index. |
返回
FFilter
The FFilter instance for chaining.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Set some values of the range C1:F10
const fRange = fWorksheet.getRange('C1:F10');
fRange.setValues([
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9],
[7, 8, 9, 10],
[8, 9, 10, 11],
[9, 10, 11, 12],
[10, 11, 12, 13],
]);
// Create a filter on the range C1:F10
let fFilter = fRange.createFilter();
// If the filter already exists, remove it and create a new one
if (!fFilter) {
fRange.getFilter().remove();
fFilter = fRange.createFilter();
}
// Set the filter criteria of the column C, filter out the rows that are not 1, 5, 9
const column = fWorksheet.getRange('C:C').getColumn();
fFilter.setColumnFilterCriteria(column, {
colId: 0,
filters: {
filters: ['1', '5', '9'],
},
});
// Clear the filter criteria of the column C after 3 seconds
setTimeout(() => {
fFilter.removeColumnFilterCriteria(column);
}, 3000);
removeFilterCriteria()
removeFilterCriteria(): FFilter
Remove the filter criteria of all columns.
返回
FFilter
The FFilter instance for chaining.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Set some values of the range C1:F10
const fRange = fWorksheet.getRange('C1:F10');
fRange.setValues([
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9],
[7, 8, 9, 10],
[8, 9, 10, 11],
[9, 10, 11, 12],
[10, 11, 12, 13],
]);
// Create a filter on the range C1:F10
let fFilter = fRange.createFilter();
// If the filter already exists, remove it and create a new one
if (!fFilter) {
fRange.getFilter().remove();
fFilter = fRange.createFilter();
}
// Set the filter criteria of the column C, filter out the rows that are not 1, 5, 9
const column = fWorksheet.getRange('C:C').getColumn();
fFilter.setColumnFilterCriteria(column, {
colId: 0,
filters: {
filters: ['1', '5', '9'],
},
});
// Clear the filter criteria of all columns after 3 seconds
setTimeout(() => {
fFilter.removeFilterCriteria();
}, 3000);
setColumnFilterCriteria()
setColumnFilterCriteria(column, criteria): FFilter
Set the filter criteria of a column.
参数
参数 | 类型 | 描述 |
---|---|---|
column | number | The column index. |
criteria | ISetSheetsFilterCriteriaCommandParams | The new filter criteria. |
返回
FFilter
The FFilter instance for chaining.
示例
const fWorkbook = univerAPI.getActiveWorkbook();
const fWorksheet = fWorkbook.getActiveSheet();
// Set some values of the range C1:F10
const fRange = fWorksheet.getRange('C1:F10');
fRange.setValues([
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9],
[7, 8, 9, 10],
[8, 9, 10, 11],
[9, 10, 11, 12],
[10, 11, 12, 13],
]);
// Create a filter on the range C1:F10
let fFilter = fRange.createFilter();
// If the filter already exists, remove it and create a new one
if (!fFilter) {
fRange.getFilter().remove();
fFilter = fRange.createFilter();
}
// Set the filter criteria of the column C, filter out the rows that are not 1, 5, 9
const column = fWorksheet.getRange('C:C').getColumn();
fFilter.setColumnFilterCriteria(column, {
colId: 0,
filters: {
filters: ['1', '5', '9'],
},
});