Skip to content

Commit 1027687

Browse files
authored
Merge pull request #674 from OfficeDev/main
[admin] Publish
2 parents 0201981 + 04f8b0d commit 1027687

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

docs/resources/samples/table-samples.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Table samples
33
description: A collection of samples showing how to interact with Excel tables.
4-
ms.date: 09/08/2023
4+
ms.date: 10/30/2023
55
ms.localizationpriority: medium
66
---
77

@@ -52,6 +52,44 @@ function main(workbook: ExcelScript.Workbook) {
5252
> workbook.addWorksheet().getRange("A1").copyFrom(table.getRange());
5353
> ```
5454
55+
### Filter out one value
56+
57+
The previous sample filters a table based on a list of included values. To exclude a particular value from the table, you need to provide the list of every other value in the column. This sample uses a function `columnToSet` to convert a column into a set of unique values. That set then has the excluded value ("Station-1") removed.
58+
59+
```TypeScript
60+
function main(workbook: ExcelScript.Workbook) {
61+
// Get the table in the workbook named "StationTable".
62+
const table = workbook.getTable("StationTable");
63+
64+
// Get the "Station" table column for the filter.
65+
const stationColumn = table.getColumnByName("Station");
66+
67+
// Get a list of unique values in the station column.
68+
const stationSet = columnToSet(stationColumn);
69+
70+
// Apply a filter to the table that will only show rows
71+
// that don't have a value of "Station-1" in the "Station" column.
72+
stationColumn.getFilter().applyValuesFilter(stationSet.filter((value) => {
73+
return value !== "Station-1";
74+
}));
75+
}
76+
77+
/**
78+
* Convert a column into a set so it only contains unique values.
79+
*/
80+
function columnToSet(column: ExcelScript.TableColumn): string[] {
81+
const range = column.getRangeBetweenHeaderAndTotal().getValues() as string[][];
82+
const columnSet: string[] = [];
83+
range.forEach((value) => {
84+
if (!columnSet.includes(value[0])) {
85+
columnSet.push(value[0]);
86+
}
87+
});
88+
89+
return columnSet;
90+
}
91+
```
92+
5593
## Remove table column filters
5694

5795
This sample removes the filters from a table column, based on the active cell location. The script detects if the cell is part of a table, determines the table column, and clears any filters that are applied on it.

0 commit comments

Comments
 (0)