Skip to content

Commit 04a02e8

Browse files
authored
[excel] (Range, PivotTable) Add FilterPivotHierarchy and ConditionalFormat samples (#325)
* Add FitlerPivotHierarchy samples * PR feedback
1 parent 2c0d75a commit 04a02e8

File tree

6 files changed

+268
-10
lines changed

6 files changed

+268
-10
lines changed

docs/docs-ref-autogen/excel/excelscript/excelscript.conditionalrangefill.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,39 @@ uid: 'ExcelScript!ExcelScript.ConditionalRangeFill:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.ConditionalRangeFill
66
summary: Represents the background of a conditional range object.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script applies cell value conditional formatting to a range.
15+
* Any value less than 60 will have the cell's fill color changed and the font made italic.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the range to format.
19+
const selectedRange = workbook.getSelectedRange();
20+
21+
// Add cell value conditional formatting.
22+
const cellValueConditionalFormatting =
23+
selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
24+
25+
// Create the condition, in this case when the cell value is less than 60.
26+
const rule: ExcelScript.ConditionalCellValueRule = {
27+
formula1: "60",
28+
operator: ExcelScript.ConditionalCellValueOperator.lessThan
29+
};
30+
cellValueConditionalFormatting.setRule(rule);
31+
32+
// Set the format to apply when the condition is met.
33+
const format: ExcelScript.ConditionalRangeFormat = cellValueConditionalFormatting.getFormat();
34+
const fill: ExcelScript.ConditionalRangeFill = format.getFill();
35+
const font: ExcelScript.ConditionalRangeFont = format.getFont();
36+
fill.setColor("yellow");
37+
font.setItalic(true);
38+
}
39+
```
840
isPreview: false
941
isDeprecated: false
1042
type: interface

docs/docs-ref-autogen/excel/excelscript/excelscript.conditionalrangefont.yml

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,39 @@ uid: 'ExcelScript!ExcelScript.ConditionalRangeFont:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.ConditionalRangeFont
66
summary: 'This object represents the font attributes (font style, color, etc.) for an object.'
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script applies cell value conditional formatting to a range.
15+
* Any value less than 60 will have the cell's fill color changed and the font made italic.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the range to format.
19+
const selectedRange = workbook.getSelectedRange();
20+
21+
// Add cell value conditional formatting.
22+
const cellValueConditionalFormatting =
23+
selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
24+
25+
// Create the condition, in this case when the cell value is less than 60.
26+
const rule: ExcelScript.ConditionalCellValueRule = {
27+
formula1: "60",
28+
operator: ExcelScript.ConditionalCellValueOperator.lessThan
29+
};
30+
cellValueConditionalFormatting.setRule(rule);
31+
32+
// Set the format to apply when the condition is met.
33+
const format: ExcelScript.ConditionalRangeFormat = cellValueConditionalFormatting.getFormat();
34+
const fill: ExcelScript.ConditionalRangeFill = format.getFill();
35+
const font: ExcelScript.ConditionalRangeFont = format.getFont();
36+
fill.setColor("yellow");
37+
font.setItalic(true);
38+
}
39+
```
840
isPreview: false
941
isDeprecated: false
1042
type: interface

docs/docs-ref-autogen/excel/excelscript/excelscript.conditionalrangeformat.yml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,37 @@ uid: 'ExcelScript!ExcelScript.ConditionalRangeFormat:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.ConditionalRangeFormat
66
summary: 'A format object encapsulating the conditional formats range''s font, fill, borders, and other properties.'
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script applies cell value conditional formatting to a range.
15+
* Any value less than 60 will have the cell's fill color changed and the font made italic.
16+
*/
17+
function main(workbook: ExcelScript.Workbook) {
18+
// Get the range to format.
19+
const selectedRange = workbook.getSelectedRange();
20+
21+
// Add cell value conditional formatting.
22+
const cellValueConditionalFormatting =
23+
selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
24+
25+
// Create the condition, in this case when the cell value is less than 60.
26+
const rule: ExcelScript.ConditionalCellValueRule = {
27+
formula1: "60",
28+
operator: ExcelScript.ConditionalCellValueOperator.lessThan
29+
};
30+
cellValueConditionalFormatting.setRule(rule);
31+
32+
// Set the format to apply when the condition is met.
33+
const format: ExcelScript.ConditionalRangeFormat = cellValueConditionalFormatting.getFormat();
34+
format.getFill().setColor("yellow");
35+
format.getFont().setItalic(true);
36+
}
37+
```
838
isPreview: false
939
isDeprecated: false
1040
type: interface

docs/docs-ref-autogen/excel/excelscript/excelscript.filterpivothierarchy.yml

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,33 @@ uid: 'ExcelScript!ExcelScript.FilterPivotHierarchy:interface'
44
package: ExcelScript!
55
fullName: ExcelScript.FilterPivotHierarchy
66
summary: Represents the Excel FilterPivotHierarchy.
7-
remarks: ''
7+
remarks: |-
8+
9+
10+
#### Examples
11+
12+
```TypeScript
13+
/**
14+
* This script creates a PivotTable with a filter.
15+
*/
16+
function main(workbook: ExcelScript.Workbook) {
17+
// Get the two worksheets to use in PivotTable creation.
18+
const dataSheet = workbook.getWorksheet("Data");
19+
const pivotSheet = workbook.getWorksheet("Pivot");
20+
21+
// Create a new PivotTable.
22+
const newPivot = pivotSheet.addPivotTable(
23+
"My PivotTable",
24+
dataSheet.getUsedRange(),
25+
pivotSheet.getRange("A1"));
26+
27+
// Add a filter with the Quarter field.
28+
const filter: ExcelScript.FilterPivotHierarchy =
29+
newPivot.addFilterHierarchy(newPivot.getHierarchy("Quarter"));
30+
31+
// Add other hierarchies...
32+
}
33+
```
834
isPreview: false
935
isDeprecated: false
1036
type: interface
@@ -60,7 +86,25 @@ methods:
6086
content: 'getName(): string;'
6187
return:
6288
type: string
63-
description: ''
89+
description: |-
90+
91+
92+
#### Examples
93+
94+
```TypeScript
95+
/**
96+
* This script logs the names of all the filter hierarchies in a PivotTable.
97+
*/
98+
function main(workbook: ExcelScript.Workbook) {
99+
// Get the first PivotTable in the workbook.
100+
const pivotTable = workbook.getPivotTables()[0];
101+
102+
// For each pivot filter, log its name.
103+
pivotTable.getFilterHierarchies().forEach((filter: ExcelScript.FilterPivotHierarchy) => {
104+
console.log(filter.getName());
105+
});
106+
}
107+
```
64108
- name: getPivotField(name)
65109
uid: 'ExcelScript!ExcelScript.FilterPivotHierarchy#getPivotField:member(1)'
66110
package: ExcelScript!

docs/sample-scripts/excel-scripts.yaml

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1374,6 +1374,88 @@
13741374
};
13751375
presetFormat.setRule(duplicateRule);
13761376
}
1377+
'ExcelScript.ConditionalRangeFill:interface':
1378+
- |-
1379+
/**
1380+
* This script applies cell value conditional formatting to a range.
1381+
* Any value less than 60 will have the cell's fill color changed and the font made italic.
1382+
*/
1383+
function main(workbook: ExcelScript.Workbook) {
1384+
// Get the range to format.
1385+
const selectedRange = workbook.getSelectedRange();
1386+
1387+
// Add cell value conditional formatting.
1388+
const cellValueConditionalFormatting =
1389+
selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
1390+
1391+
// Create the condition, in this case when the cell value is less than 60.
1392+
const rule: ExcelScript.ConditionalCellValueRule = {
1393+
formula1: "60",
1394+
operator: ExcelScript.ConditionalCellValueOperator.lessThan
1395+
};
1396+
cellValueConditionalFormatting.setRule(rule);
1397+
1398+
// Set the format to apply when the condition is met.
1399+
const format: ExcelScript.ConditionalRangeFormat = cellValueConditionalFormatting.getFormat();
1400+
const fill: ExcelScript.ConditionalRangeFill = format.getFill();
1401+
const font: ExcelScript.ConditionalRangeFont = format.getFont();
1402+
fill.setColor("yellow");
1403+
font.setItalic(true);
1404+
}
1405+
'ExcelScript.ConditionalRangeFont:interface':
1406+
- |-
1407+
/**
1408+
* This script applies cell value conditional formatting to a range.
1409+
* Any value less than 60 will have the cell's fill color changed and the font made italic.
1410+
*/
1411+
function main(workbook: ExcelScript.Workbook) {
1412+
// Get the range to format.
1413+
const selectedRange = workbook.getSelectedRange();
1414+
1415+
// Add cell value conditional formatting.
1416+
const cellValueConditionalFormatting =
1417+
selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
1418+
1419+
// Create the condition, in this case when the cell value is less than 60.
1420+
const rule: ExcelScript.ConditionalCellValueRule = {
1421+
formula1: "60",
1422+
operator: ExcelScript.ConditionalCellValueOperator.lessThan
1423+
};
1424+
cellValueConditionalFormatting.setRule(rule);
1425+
1426+
// Set the format to apply when the condition is met.
1427+
const format: ExcelScript.ConditionalRangeFormat = cellValueConditionalFormatting.getFormat();
1428+
const fill: ExcelScript.ConditionalRangeFill = format.getFill();
1429+
const font: ExcelScript.ConditionalRangeFont = format.getFont();
1430+
fill.setColor("yellow");
1431+
font.setItalic(true);
1432+
}
1433+
'ExcelScript.ConditionalRangeFormat:interface':
1434+
- |-
1435+
/**
1436+
* This script applies cell value conditional formatting to a range.
1437+
* Any value less than 60 will have the cell's fill color changed and the font made italic.
1438+
*/
1439+
function main(workbook: ExcelScript.Workbook) {
1440+
// Get the range to format.
1441+
const selectedRange = workbook.getSelectedRange();
1442+
1443+
// Add cell value conditional formatting.
1444+
const cellValueConditionalFormatting =
1445+
selectedRange.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
1446+
1447+
// Create the condition, in this case when the cell value is less than 60.
1448+
const rule: ExcelScript.ConditionalCellValueRule = {
1449+
formula1: "60",
1450+
operator: ExcelScript.ConditionalCellValueOperator.lessThan
1451+
};
1452+
cellValueConditionalFormatting.setRule(rule);
1453+
1454+
// Set the format to apply when the condition is met.
1455+
const format: ExcelScript.ConditionalRangeFormat = cellValueConditionalFormatting.getFormat();
1456+
format.getFill().setColor("yellow");
1457+
format.getFont().setItalic(true);
1458+
}
13771459
'ExcelScript.ConditionalTextComparisonRule:interface':
13781460
- |-
13791461
/**
@@ -2345,6 +2427,44 @@
23452427
// greater than 0 and less than or equal to 60.
23462428
table.getColumnByName("Exam Score").getFilter().applyCustomFilter(">0", "<=60", ExcelScript.FilterOperator.and);
23472429
}
2430+
'ExcelScript.FilterPivotHierarchy:interface':
2431+
- |-
2432+
/**
2433+
* This script creates a PivotTable with a filter.
2434+
*/
2435+
function main(workbook: ExcelScript.Workbook) {
2436+
// Get the two worksheets to use in PivotTable creation.
2437+
const dataSheet = workbook.getWorksheet("Data");
2438+
const pivotSheet = workbook.getWorksheet("Pivot");
2439+
2440+
// Create a new PivotTable.
2441+
const newPivot = pivotSheet.addPivotTable(
2442+
"My PivotTable",
2443+
dataSheet.getUsedRange(),
2444+
pivotSheet.getRange("A1"));
2445+
2446+
// Add a filter with the Quarter field.
2447+
const filter: ExcelScript.FilterPivotHierarchy =
2448+
newPivot.addFilterHierarchy(newPivot.getHierarchy("Quarter"));
2449+
2450+
// Add other hierarchies...
2451+
}
2452+
'ExcelScript.FilterPivotHierarchy#getName:member(1)':
2453+
- |-
2454+
/**
2455+
* This script logs the names of all the filter hierarchies in a PivotTable.
2456+
*/
2457+
function main(workbook: ExcelScript.Workbook) {
2458+
// Get the first PivotTable in the workbook.
2459+
const pivotTable = workbook.getPivotTables()[0];
2460+
2461+
// For each pivot filter, log its name.
2462+
pivotTable.getFilterHierarchies().forEach((filter: ExcelScript.FilterPivotHierarchy) => {
2463+
console.log(filter.getName());
2464+
});
2465+
}
2466+
2467+
23482468
'ExcelScript.GeometricShapeType:enum':
23492469
- |-
23502470
/**

generate-docs/API Coverage Report.csv

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1265,11 +1265,11 @@ ExcelScript.ConditionalRangeBorderLineStyle,"dashDot",EnumField,Missing,false
12651265
ExcelScript.ConditionalRangeBorderLineStyle,"dashDotDot",EnumField,Missing,false
12661266
ExcelScript.ConditionalRangeBorderLineStyle,"dot",EnumField,Missing,false
12671267
ExcelScript.ConditionalRangeBorderLineStyle,"none",EnumField,Missing,false
1268-
ExcelScript.ConditionalRangeFill,N/A,Class,Fine,false
1268+
ExcelScript.ConditionalRangeFill,N/A,Class,Fine,true
12691269
ExcelScript.ConditionalRangeFill,"clear()",Method,Poor,false
12701270
ExcelScript.ConditionalRangeFill,"getColor()",Method,Poor,false
12711271
ExcelScript.ConditionalRangeFill,"setColor(color)",Method,Poor,false
1272-
ExcelScript.ConditionalRangeFont,N/A,Class,Fine,false
1272+
ExcelScript.ConditionalRangeFont,N/A,Class,Fine,true
12731273
ExcelScript.ConditionalRangeFont,"clear()",Method,Poor,false
12741274
ExcelScript.ConditionalRangeFont,"getBold()",Method,Poor,false
12751275
ExcelScript.ConditionalRangeFont,"getColor()",Method,Poor,false
@@ -1285,7 +1285,7 @@ ExcelScript.ConditionalRangeFontUnderlineStyle,N/A,Enum,Missing,false
12851285
ExcelScript.ConditionalRangeFontUnderlineStyle,"double",EnumField,Missing,false
12861286
ExcelScript.ConditionalRangeFontUnderlineStyle,"none",EnumField,Missing,false
12871287
ExcelScript.ConditionalRangeFontUnderlineStyle,"single",EnumField,Missing,false
1288-
ExcelScript.ConditionalRangeFormat,N/A,Class,Fine,false
1288+
ExcelScript.ConditionalRangeFormat,N/A,Class,Fine,true
12891289
ExcelScript.ConditionalRangeFormat,"getBorders()",Method,Poor,false
12901290
ExcelScript.ConditionalRangeFormat,"getConditionalRangeBorder(index)",Method,Poor,false
12911291
ExcelScript.ConditionalRangeFormat,"getConditionalRangeBorderBottom()",Method,Poor,false
@@ -1615,11 +1615,11 @@ ExcelScript.FilterOn,"values",EnumField,Missing,false
16151615
ExcelScript.FilterOperator,N/A,Enum,Missing,true
16161616
ExcelScript.FilterOperator,"and",EnumField,Missing,false
16171617
ExcelScript.FilterOperator,"or",EnumField,Missing,false
1618-
ExcelScript.FilterPivotHierarchy,N/A,Class,Poor,false
1618+
ExcelScript.FilterPivotHierarchy,N/A,Class,Poor,true
16191619
ExcelScript.FilterPivotHierarchy,"getEnableMultipleFilterItems()",Method,Poor,false
16201620
ExcelScript.FilterPivotHierarchy,"getFields()",Method,Poor,false
16211621
ExcelScript.FilterPivotHierarchy,"getId()",Method,Poor,false
1622-
ExcelScript.FilterPivotHierarchy,"getName()",Method,Poor,false
1622+
ExcelScript.FilterPivotHierarchy,"getName()",Method,Poor,true
16231623
ExcelScript.FilterPivotHierarchy,"getPivotField(name)",Method,Poor,false
16241624
ExcelScript.FilterPivotHierarchy,"getPosition()",Method,Poor,false
16251625
ExcelScript.FilterPivotHierarchy,"setEnableMultipleFilterItems(enableMultipleFilterItems)",Method,Poor,false

0 commit comments

Comments
 (0)