-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathexcel-scripts.yaml
7054 lines (6348 loc) · 270 KB
/
excel-scripts.yaml
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'ExcelScript.AggregationFunction:enum':
- |-
/**
* This script changes how the data in a PivotTable is aggregated.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the first PivotTable in the workbook.
const pivotTable = workbook.getPivotTables()[0];
// Set the first data hierarchy to summarize with an average value, instead of a sum.
const dataHierarchy = pivotTable.getDataHierarchies()[0];
dataHierarchy.setSummarizeBy(ExcelScript.AggregationFunction.average);
}
'ExcelScript.AllowEditRangeOptions:interface':
- |-
/**
* This script adds a password-protected, editable range
* to an otherwise protected worksheet.
*/
function main(workbook: ExcelScript.Workbook, password: string) {
// Get the protection object for the "Data" worksheet.
const dataSheet = workbook.getWorksheet("Data");
const sheetProtection = dataSheet.getProtection();
// Set the password needed to edit the range to be the user provided string.
const editRangeProperties : ExcelScript.AllowEditRangeOptions = {
password: password
};
// Set range "D2:D6" to be editable if the password is provided.
sheetProtection.addAllowEditRange("Notes Section", "D2:D6", editRangeProperties);
// Protect the worksheet.
sheetProtection.protect();
}
'ExcelScript.Application#calculate:member(1)':
- |-
/**
* This script fully recalculates the entire workbook.
* This code is useful when automatic recalculation is turned off
* but later parts of the script rely on updated values.
*/
function main(workbook: ExcelScript.Workbook, workbookURL: string) {
const application = workbook.getApplication();
application.calculate(ExcelScript.CalculationType.fullRebuild);
}
'ExcelScript.Application#getCalculationState:member(1)':
- |-
/**
* This script uses the fill color of the first cell to indicate the current
* calculation state of the workbook.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the first cell in the first worksheet.
const cell = workbook.getWorksheets()[0].getCell(0,0);
// Get that cell's fill object.
const cellFill = cell.getFormat().getFill();
// Set the cell fill based on the calculation state.
const calcState = workbook.getApplication().getCalculationState();
switch (calcState) {
case ExcelScript.CalculationState.pending:
cellFill.setColor("Red");
break;
case ExcelScript.CalculationState.calculating:
cellFill.setColor("Yellow");
break;
case ExcelScript.CalculationState.done:
cellFill.setColor("Green");
break;
}
}
'ExcelScript.ArrowheadLength:enum':
- |-
/**
* This script adds a line that goes from cell B2 to cell F4 on the current worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
const sheet = workbook.getActiveWorksheet();
// Get the ranges for the two cells.
const b2Range = sheet.getRange("B2");
const f4Range = sheet.getRange("F4");
// Add a straight line that connects the top-left corners of both cells.
const newShape = sheet.addLine(
b2Range.getLeft(),
b2Range.getTop(),
f4Range.getLeft(),
f4Range.getTop(),
ExcelScript.ConnectorType.straight);
// Add a long, open arrowhead to the end of the line, such that it points at F4.
const line = newShape.getLine();
line.setEndArrowheadStyle(ExcelScript.ArrowheadStyle.open);
line.setEndArrowheadLength(ExcelScript.ArrowheadLength.long);
}
'ExcelScript.ArrowheadStyle:enum':
- |-
/**
* This script adds a line that goes from cell B2 to cell F4 on the current worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
const sheet = workbook.getActiveWorksheet();
// Get the ranges for the two cells.
const b2Range = sheet.getRange("B2");
const f4Range = sheet.getRange("F4");
// Add a straight line that connects the top-left corners of both cells.
const newShape = sheet.addLine(
b2Range.getLeft(),
b2Range.getTop(),
f4Range.getLeft(),
f4Range.getTop(),
ExcelScript.ConnectorType.straight);
// Add an open arrowhead to the end of the line, such that it points at F4.
const line = newShape.getLine();
line.setEndArrowheadStyle(ExcelScript.ArrowheadStyle.open);
}
'ExcelScript.ArrowheadWidth:enum':
- |-
/**
* This script adds a line that goes from cell B2 to cell F4 on the current worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
const sheet = workbook.getActiveWorksheet();
// Get the ranges for the two cells.
const b2Range = sheet.getRange("B2");
const f4Range = sheet.getRange("F4");
// Add a straight line that connects the top-left corners of both cells.
const newShape = sheet.addLine(
b2Range.getLeft(),
b2Range.getTop(),
f4Range.getLeft(),
f4Range.getTop(),
ExcelScript.ConnectorType.straight);
// Add a wide, triangular arrowhead to the end of the line, such that it points at F4.
const line = newShape.getLine();
line.setEndArrowheadStyle(ExcelScript.ArrowheadStyle.triangle);
line.setEndArrowheadWidth(ExcelScript.ArrowheadWidth.wide);
}
'ExcelScript.AutoFillType:enum':
- |-
/**
* This script uses the autofill feature to complete a table with days of the month.
* See https://support.microsoft.com/office/74e31bdd-d993-45da-aa82-35a236c5b5db
* for examples of autofill scenarios.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current, active worksheet.
let currentWorksheet = workbook.getActiveWorksheet();
// Get the data range that shows the pattern.
let dataRange = currentWorksheet.getRange("C2:C3");
// Autofill the connected range. C2:C3 are filled in with dates. C4:C54 are blank.
dataRange.autoFill("C2:C54", ExcelScript.AutoFillType.fillDays);
}
'ExcelScript.AutoFilter:interface':
- |-
/**
* This script creates an autoFilter on the worksheet that filters out rows based on column values.
* The autoFilter filters to only include rows that have a value in column C in the lowest 10 values
* (of column C values).
*/
function main(workbook: ExcelScript.Workbook) {
// Get the autoFilter of the first table in the current worksheet.
const currentSheet = workbook.getActiveWorksheet();
const dataRange = currentSheet.getUsedRange();
const autoFilter = currentSheet.getAutoFilter();
// Add a filter that will only show the rows with the lowest 10 values in column C
// (index 2, assuming the used range spans from at least A:C).
autoFilter.apply(dataRange, 2, {
criterion1: "10",
filterOn: ExcelScript.FilterOn.bottomItems
});
}
'ExcelScript.AutoFilter#apply:member(1)':
- |-
/**
* This script applies a filter to a table so that
* only rows with values in column 1 that start with "L" are shown.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the autoFilter of the first table in the current worksheet.
const table = workbook.getActiveWorksheet().getTables()[0];
const autoFilter = table.getAutoFilter();
// Filter to only include values that start with "L".
const filterCriteria: ExcelScript.FilterCriteria = {
filterOn: ExcelScript.FilterOn.custom,
criterion1: "L*"
};
// Apply the filter to column 1 (zero-based).
autoFilter.apply(table.getRange(), 1, filterCriteria);
}
'ExcelScript.AutoFilter#clearCriteria:member(1)':
- |-
/**
* This script clears any applied criteria from the worksheet's autoFilter.
*/
function main(workbook: ExcelScript.Workbook) {
const currentSheet = workbook.getActiveWorksheet();
// Clear all the criteria currently applied to the autoFilter.
currentSheet.getAutoFilter().clearCriteria();
}
'ExcelScript.BasicDataValidation:interface':
- |-
/**
* This script creates a data validation rule for the range B1:B5.
* All values in that range must be a positive number.
* Attempts to enter other values are blocked and an error message appears.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range B1:B5 in the active worksheet.
const currentSheet = workbook.getActiveWorksheet();
const positiveNumberOnlyCells = currentSheet.getRange("B1:B5");
// Create a data validation rule to only allow positive numbers.
const positiveNumberValidation: ExcelScript.BasicDataValidation = {
formula1: "0",
operator: ExcelScript.DataValidationOperator.greaterThan
};
const positiveNumberOnlyRule: ExcelScript.DataValidationRule = {
wholeNumber: positiveNumberValidation
};
// Set the rule on the range.
const rangeDataValidation = positiveNumberOnlyCells.getDataValidation();
rangeDataValidation.setRule(positiveNumberOnlyRule);
// Create an alert to appear when data other than positive numbers are entered.
const positiveNumberOnlyAlert: ExcelScript.DataValidationErrorAlert = {
message: "Positive numbers only",
showAlert: true,
style: ExcelScript.DataValidationAlertStyle.stop,
title: "Invalid data"
};
rangeDataValidation.setErrorAlert(positiveNumberOnlyAlert);
}
'ExcelScript.BorderIndex:enum':
- |-
/**
* This script creates a border around a range.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the RangeFormat object for the range "B2:G10".
const currentSheet = workbook.getActiveWorksheet();
const rangeForBorder = currentSheet.getRange("B2:G10");
const format = rangeForBorder.getFormat();
// Get a RangeBorder object for each edge of the range and set the border properties.
let edgeTop = format.getRangeBorder(ExcelScript.BorderIndex.edgeTop);
edgeTop.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeTop.setWeight(ExcelScript.BorderWeight.thick);
let edgeBottom = format.getRangeBorder(ExcelScript.BorderIndex.edgeBottom);
edgeBottom.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeBottom.setWeight(ExcelScript.BorderWeight.thick);
let edgeLeft = format.getRangeBorder(ExcelScript.BorderIndex.edgeLeft);
edgeLeft.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeLeft.setWeight(ExcelScript.BorderWeight.thick);
let edgeRight = format.getRangeBorder(ExcelScript.BorderIndex.edgeRight);
edgeRight.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeRight.setWeight(ExcelScript.BorderWeight.thick);
}
'ExcelScript.BorderLineStyle:enum':
- |-
/**
* This script creates a border around a range.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the RangeFormat object for the range "B2:G10".
const currentSheet = workbook.getActiveWorksheet();
const rangeForBorder = currentSheet.getRange("B2:G10");
const format = rangeForBorder.getFormat();
// Get a RangeBorder object for each edge of the range and set the border properties.
let edgeTop = format.getRangeBorder(ExcelScript.BorderIndex.edgeTop);
edgeTop.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeTop.setWeight(ExcelScript.BorderWeight.thick);
let edgeBottom = format.getRangeBorder(ExcelScript.BorderIndex.edgeBottom);
edgeBottom.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeBottom.setWeight(ExcelScript.BorderWeight.thick);
let edgeLeft = format.getRangeBorder(ExcelScript.BorderIndex.edgeLeft);
edgeLeft.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeLeft.setWeight(ExcelScript.BorderWeight.thick);
let edgeRight = format.getRangeBorder(ExcelScript.BorderIndex.edgeRight);
edgeRight.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeRight.setWeight(ExcelScript.BorderWeight.thick);
}
'ExcelScript.BorderWeight:enum':
- |-
/**
* This script creates a border around a range.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the RangeFormat object for the range "B2:G10".
const currentSheet = workbook.getActiveWorksheet();
const rangeForBorder = currentSheet.getRange("B2:G10");
const format = rangeForBorder.getFormat();
// Get a RangeBorder object for each edge of the range and set the border properties.
let edgeTop = format.getRangeBorder(ExcelScript.BorderIndex.edgeTop);
edgeTop.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeTop.setWeight(ExcelScript.BorderWeight.thick);
let edgeBottom = format.getRangeBorder(ExcelScript.BorderIndex.edgeBottom);
edgeBottom.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeBottom.setWeight(ExcelScript.BorderWeight.thick);
let edgeLeft = format.getRangeBorder(ExcelScript.BorderIndex.edgeLeft);
edgeLeft.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeLeft.setWeight(ExcelScript.BorderWeight.thick);
let edgeRight = format.getRangeBorder(ExcelScript.BorderIndex.edgeRight);
edgeRight.setStyle(ExcelScript.BorderLineStyle.dashDot);
edgeRight.setWeight(ExcelScript.BorderWeight.thick);
}
'ExcelScript.BuiltInStyle:enum':
- |-
/**
* This script sets the cell style to "warning text"
* on every cell with a formula error.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the used range in the current worksheet.
const sheet = workbook.getActiveWorksheet();
const usedRange = sheet.getUsedRange();
// Get any cells that are displaying errors.
const errorCells = usedRange.getSpecialCells(
ExcelScript.SpecialCellType.formulas,
ExcelScript.SpecialCellValueType.errors
);
// Check if there are error cells before proceeding.
if (errorCells) {
// Use the built-in warning text style on the error cells.
errorCells.setPredefinedCellStyle(
ExcelScript.BuiltInStyle.warningText.toString()
);
} else {
console.log("No formula errors in the worksheet.");
}
}
'ExcelScript.CalculationMode:enum':
- |-
/**
* This script recalculates the used range of a specific worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Only recalculate if the calculation mode is not set to automatic.
if (workbook.getApplication().getCalculationMode() !== ExcelScript.CalculationMode.automatic) {
// Get the used range from a worksheet named "Monthly Report".
const sheet = workbook.getWorksheet("Monthly Report");
const range = sheet.getUsedRange();
console.log(`Calculating ${range.getAddress()}`);
// Force all the used cells in that worksheet to calculate.
sheet.getUsedRange().calculate();
}
}
'ExcelScript.CalculationState:enum':
- |-
/**
* This script uses the fill color of the first cell to indicate the current
* calculation state of the workbook.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the first cell in the first worksheet.
const cell = workbook.getWorksheets()[0].getCell(0,0);
// Get that cell's fill object.
const cellFill = cell.getFormat().getFill();
// Set the cell fill based on the calculation state.
const calcState = workbook.getApplication().getCalculationState();
switch (calcState) {
case ExcelScript.CalculationState.pending:
cellFill.setColor("Red");
break;
case ExcelScript.CalculationState.calculating:
cellFill.setColor("Yellow");
break;
case ExcelScript.CalculationState.done:
cellFill.setColor("Green");
break;
}
}
'ExcelScript.CalculationType:enum':
- |-
/**
* This script fully recalculates the entire workbook.
* This code is useful when automatic recalculation is turned off
* but later parts of the script rely on updated values.
*/
function main(workbook: ExcelScript.Workbook, workbookURL: string) {
const application = workbook.getApplication();
application.calculate(ExcelScript.CalculationType.fullRebuild);
}
'ExcelScript.CellValueConditionalFormat:interface':
- |-
/**
* This script applies conditional formatting to a range.
* That formatting is conditional upon the cell's numerical value.
* Any value between 50 and 75 will have the cell fill color changed and the font made italic.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range to format.
const sheet = workbook.getActiveWorksheet();
const ratingColumn = sheet.getRange("D2:D20");
// Add cell value conditional formatting.
const cellValueConditionalFormatting : ExcelScript.CellValueConditionalFormat
= ratingColumn.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
// Create the condition, in this case when the cell value is between 50 and 75.
let rule: ExcelScript.ConditionalCellValueRule = {
formula1: "50",
formula2: "75",
operator: ExcelScript.ConditionalCellValueOperator.between
};
cellValueConditionalFormatting.setRule(rule);
// Set the format to apply when the condition is met.
let format = cellValueConditionalFormatting.getFormat();
format.getFill().setColor("yellow");
format.getFont().setItalic(true);
}
'ExcelScript.Chart#addChartSeries:member(1)':
- |-
/**
* This sample produces a line chart with two series.
* The chart assumes data in A1:E5 that looks like this:
* Product Qtr1 Qtr2 Qtr3 Qtr4
* Frames 5000 7000 6544 4377
* Saddles 400 323 276 651
*/
function main(workbook: ExcelScript.Workbook) {
// Establish the ranges to use.
const sheet = workbook.getActiveWorksheet();
const headerRange = sheet.getRange("A1:E1");
const firstSeriesRange = sheet.getRange("A2:E2");
const secondSeriesRange = sheet.getRange("A3:E3");
// Create the chart.
const lineChart = sheet.addChart(ExcelScript.ChartType.line, headerRange);
// Add the first chart series.
const firstSeries = lineChart.addChartSeries();
firstSeries.setXAxisValues(headerRange);
firstSeries.setValues(firstSeriesRange);
// Add the second chart series.
const secondSeries = lineChart.addChartSeries();
secondSeries.setXAxisValues(headerRange);
secondSeries.setValues(secondSeriesRange);
}
'ExcelScript.Chart#getImage:member(1)':
- |-
/**
* This script returns an image of the first chart in the first worksheet.
* That image is 600x400 pixels and the chart will be
* stretched to fill those dimensions.
* The returned image can be used in a Power Automate flow.
*/
function main(workbook: ExcelScript.Workbook): string {
// Get the first chart in the first worksheet.
const firstSheet = workbook.getFirstWorksheet();
const firstChart = firstSheet.getCharts()[0];
// Get an image of the chart as a base64-encoded string.
const base64String = firstChart.getImage(
600, /* Width */
400, /* Height */
ExcelScript.ImageFittingMode.fill /* Fill to match the dimensions. */
);
return base64String;
}
'ExcelScript.Chart#getPlotBy:member(1)':
- |-
/**
* This sample performs the "Switch Row/Column" action on a chart named "ColumnClusteredChart".
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnClusteredChart".
let columnClusteredChart = selectedSheet.getChart("ColumnClusteredChart");
// Switch the row and column for the chart's data source.
if (columnClusteredChart.getPlotBy() === ExcelScript.ChartPlotBy.columns) {
// If the chart is grouped by columns, switch it to rows.
columnClusteredChart.setPlotBy(ExcelScript.ChartPlotBy.rows);
} else {
// If the chart is grouped by rows, switch it to columns.
columnClusteredChart.setPlotBy(ExcelScript.ChartPlotBy.columns);
}
}
'ExcelScript.Chart#getSeries:member(1)':
- |-
/**
* This sample sets the overlap of the columns in a chart named "ColumnClusteredChart".
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnClusteredChart".
let chart = selectedSheet.getChart("ColumnClusteredChart");
// Set the overlap of every column of each series within a category.
let seriesList = chart.getSeries();
seriesList.forEach((series) => {
// An overlap of 25 means the columns have 25% of their length overlapping with the adjacent columns in the same category.
series.setOverlap(25);
});
}
'ExcelScript.Chart#setName:member(1)':
- |-
/**
* This sample creates a column-clustered chart based on the current worksheet's data.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get the data range.
let range = selectedSheet.getUsedRange();
// Insert a chart using the data on the current worksheet.
let chart = selectedSheet.addChart(ExcelScript.ChartType.columnClustered, range);
// Name the chart for easy access in other scripts.
chart.setName("ColumnChart");
}
'ExcelScript.Chart#setPlotBy:member(1)':
- |-
/**
* This sample performs the "Switch Row/Column" action on a chart named "ColumnClusteredChart".
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnClusteredChart".
let columnClusteredChart = selectedSheet.getChart("ColumnClusteredChart");
// Switch the row and column for the chart's data source.
if (columnClusteredChart.getPlotBy() === ExcelScript.ChartPlotBy.columns) {
// If the chart is grouped by columns, switch it to rows.
columnClusteredChart.setPlotBy(ExcelScript.ChartPlotBy.rows);
} else {
// If the chart is grouped by rows, switch it to columns.
columnClusteredChart.setPlotBy(ExcelScript.ChartPlotBy.columns);
}
}
'ExcelScript.Chart#setPosition:member(1)':
- |-
/**
* This sample moves an existing chart to a specific place on the worksheet.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnChart".
let chart = selectedSheet.getChart("ColumnChart");
// Place the chart over the range "F1:L13".
chart.setPosition("F1", "L13");
}
'ExcelScript.ChartErrorBarsType:enum':
- |-
/**
* This script adds error bars for the standard error of each chart series point.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range for the chart.
const worksheet = workbook.getWorksheet("Sample");
const dataRange = worksheet.getRange("A1:B15");
// Create a line chart.
const chart = worksheet.addChart(ExcelScript.ChartType.line, dataRange);
// For each series, add error bars for the standard error on each point.
const allSeries = chart.getSeries();
allSeries.forEach((series) => {
series.getYErrorBars().setType(ExcelScript.ChartErrorBarsType.stError);
series.getYErrorBars().setVisible(true);
});
}
'ExcelScript.ChartPlotBy:enum':
- |-
/**
* This sample performs the "Switch Row/Column" action on a chart named "ColumnClusteredChart".
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnClusteredChart".
let columnClusteredChart = selectedSheet.getChart("ColumnClusteredChart");
// Switch the row and column for the chart's data source.
if (columnClusteredChart.getPlotBy() === ExcelScript.ChartPlotBy.columns) {
// If the chart is grouped by columns, switch it to rows.
columnClusteredChart.setPlotBy(ExcelScript.ChartPlotBy.rows);
} else {
// If the chart is grouped by rows, switch it to columns.
columnClusteredChart.setPlotBy(ExcelScript.ChartPlotBy.columns);
}
}
'ExcelScript.ChartSeries:interface':
- |-
/**
* This sample produces a line chart with two series.
* The chart assumes data in A1:E5 that looks like this:
* Product Qtr1 Qtr2 Qtr3 Qtr4
* Frames 5000 7000 6544 4377
* Saddles 400 323 276 651
*/
function main(workbook: ExcelScript.Workbook) {
// Establish the ranges to use.
const sheet = workbook.getActiveWorksheet();
const headerRange = sheet.getRange("A1:E1");
const firstSeriesRange = sheet.getRange("A2:E2");
const secondSeriesRange = sheet.getRange("A3:E3");
// Create the chart.
const lineChart = sheet.addChart(ExcelScript.ChartType.line, headerRange);
// Add the first chart series.
const firstSeries = lineChart.addChartSeries();
firstSeries.setXAxisValues(headerRange);
firstSeries.setValues(firstSeriesRange);
// Add the second chart series.
const secondSeries = lineChart.addChartSeries();
secondSeries.setXAxisValues(headerRange);
secondSeries.setValues(secondSeriesRange);
}
'ExcelScript.ChartSeries#getName:member(1)':
- |-
/**
* This sample logs the names of each of the chart series in a chart named "ColumnClusteredChart".
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnClusteredChart".
let chart = selectedSheet.getChart("ColumnClusteredChart");
// Log the name of each chart series in the chart.
let seriesList = chart.getSeries();
seriesList.forEach((series) => {
console.log(series.getName());
});
}
'ExcelScript.ChartSeries#getYErrorBars:member(1)':
- |-
/**
* This script adds error bars for the standard error of each chart series point.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range for the chart.
const worksheet = workbook.getWorksheet("Sample");
const dataRange = worksheet.getRange("A1:B15");
// Create a line chart.
const chart = worksheet.addChart(ExcelScript.ChartType.line, dataRange);
// For each series, add error bars for the standard error on each point
const allSeries = chart.getSeries();
allSeries.forEach((series) => {
series.getYErrorBars().setType(ExcelScript.ChartErrorBarsType.stError);
series.getYErrorBars().setVisible(true);
});
}
'ExcelScript.ChartSeries#setOverlap:member(1)':
- |-
/**
* This sample sets the overlap of the columns in a chart named "ColumnClusteredChart".
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get an existing chart named "ColumnClusteredChart".
let chart = selectedSheet.getChart("ColumnClusteredChart");
// Set the overlap of every column of each series within a category.
let seriesList = chart.getSeries();
seriesList.forEach((series) => {
// An overlap of 25 means the columns have 25% of their length overlapping with the adjacent columns in the same category.
series.setOverlap(25);
});
}
'ExcelScript.ChartSeriesBy:enum':
- |-
/**
* This script creates a clustered-column chart using an existing table.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the first table on the current worksheet.
const sheet = workbook.getActiveWorksheet();
const table = sheet.getTables()[0];
// Add a clustered-column chart that clusters the data based on the columns in the table.
const chart = sheet.addChart(
ExcelScript.ChartType.columnClustered,
table.getRange(),
ExcelScript.ChartSeriesBy.columns
);
}
'ExcelScript.ChartType:enum':
- |-
/**
* This sample creates a column-clustered chart based on the current worksheet's data.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the current worksheet.
let selectedSheet = workbook.getActiveWorksheet();
// Get the data range.
let range = selectedSheet.getUsedRange();
// Insert a chart using the data on the current worksheet.
let chart = selectedSheet.addChart(ExcelScript.ChartType.columnClustered, range);
// Name the chart for easy access in other scripts.
chart.setName("ColumnChart");
}
'ExcelScript.ClearApplyTo:enum':
- |-
/**
* This script removes any extra formatting that's been applied to a table.
* This leaves only the base table style effects.
* Any formatting outside of the table will be left as is.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the first table on the current worksheet.
let worksheet = workbook.getActiveWorksheet();
let table = worksheet.getTables()[0];
// Get the range used by the table.
let range = table.getRange();
// Clear all the formatting that is not applied by the table and the table style.
range.clear(ExcelScript.ClearApplyTo.formats);
}
'ExcelScript.CommentMention:interface':
- |-
/**
* This sample creates a comment that mentions a specific person.
* That person will get a notification and link to the workbook.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the first cell in the current worksheet.
const currentSheet = workbook.getActiveWorksheet();
const cell = currentSheet.getCell(0,0);
/* Create a CommentMention object for the comment.
*
* A CommentMention's properties are:
* `name`: The name of the person being mentioned.
* `id`: The zero-based index of this mention in the comment.
* `email`: The email address of the person being mentioned.
*/
const mention: ExcelScript.CommentMention = {
name: "Alex",
id: 0,
email: "[email protected]"
};
/* Create comment content that uses the mention.
* The `<at id="0">` syntax embeds the mention with ID 0 in the comment text.
* The name is displayed in the comment,
* while an email is sent to the given address.
*/
const content: ExcelScript.CommentRichContent = {
richContent: '<at id="0">' + mention.name + "</at> - Hello!",
mentions: [mention]
};
// Add the comment.
currentSheet.addComment(cell, content, ExcelScript.ContentType.mention);
}
'ExcelScript.CommentRichContent#richContent:member':
- |-
/**
* This sample finds overdue work items in a table and
* lets their owners know with a comment that uses an @mention.
*
* This assumes the worksheet has a table with the columns:
* "Work Item", "Project", "Owner", "Due Date"
*/
function main(workbook: ExcelScript.Workbook) {
let currentSheet = workbook.getActiveWorksheet();
// Get the "Owner" column range and values.
let table = currentSheet.getTables()[0];
let ownerColumnRange = table.getColumn("Owner").getRangeBetweenHeaderAndTotal();
let ownerColumnValues = ownerColumnRange.getValues();
// Get the "Due Date" column range and values.
let dueDateColumnRange = table.getColumn("Due Date").getRangeBetweenHeaderAndTotal();
let dueDateColumnValues = dueDateColumnRange.getValues();
// Look for overdue work items.
for (let row = 0; row < dueDateColumnValues.length; row++) {
/* Convert the Excel date into a JavaScript date.
* This is necessary because Excel and JavaScript store
* their dates as different numerical values.
*/
let dueDate = new Date(Math.round((dueDateColumnValues[row][0] as number - 25569) * 86400 * 1000));
// Check if the current date is greater than the due date.
if (Date.now() > dueDate.valueOf()) {
/* Create a CommentMention object for the comment,
* based on the work item's owner.
*
* A CommentMention's properties are:
* `name`: The name of the person being mentioned.
* `id`: The index of this mention in the comment.
* `email`: The email address of the person being mentioned.
* In this sample, "Owner: is also the user name for the email.
*/
let mention = {
name: ownerColumnValues[row][0].toString(),
id: 0,
email: ownerColumnValues[row][0] + "@contoso.com"
};
/* Create the comment.
* The `<at id="0">` syntax embeds the mention in the comment text.
* The name is displayed in the comment,
* while an email is sent to the given address.
*
* The addComment parameters are:
* `cellAddress`: The location of the comment.
* `content`: The text of the comment and any embedded mentions.
* `contentType`: The type of comment ("Mention" or "Plain").
*/
currentSheet.addComment(
dueDateColumnRange.getCell(row, 0),
{
richContent: '<at id="0">' + mention.name + "</at> - Your work item is overdue.",
mentions: [mention]
},
ExcelScript.ContentType.mention
);
}
}
}
'ExcelScript.ConditionalCellValueOperator:enum':
- |-
/**
* This script applies conditional formatting to a range.
* That formatting is conditional upon the cell's numerical value.
* Any value between 50 and 75 will have the cell fill color changed and the font made italic.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range to format.
const sheet = workbook.getActiveWorksheet();
const ratingColumn = sheet.getRange("D2:D20");
// Add cell value conditional formatting.
const cellValueConditionalFormatting =
ratingColumn.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
// Set the format to apply when the condition is met.
let format = cellValueConditionalFormatting.getFormat();
format.getFill().setColor("yellow");
format.getFont().setItalic(true);
// Create the condition, in this case when the cell value is between 50 and 75.
let rule: ExcelScript.ConditionalCellValueRule = {
formula1: "50",
formula2: "75",
operator: ExcelScript.ConditionalCellValueOperator.between
};
cellValueConditionalFormatting.setRule(rule);
}
'ExcelScript.ConditionalCellValueRule:interface':
- |-
/**
* This script applies conditional formatting to a range.
* That formatting is conditional upon the cell's numerical value.
* Any value between 50 and 75 will have the cell fill color changed and the font made italic.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range to format.
const sheet = workbook.getActiveWorksheet();
const ratingColumn = sheet.getRange("D2:D20");
// Add cell value conditional formatting.
const cellValueConditionalFormatting =
ratingColumn.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
// Create the condition, in this case when the cell value is between 50 and 75.
let rule: ExcelScript.ConditionalCellValueRule = {
formula1: "50",
formula2: "75",
operator: ExcelScript.ConditionalCellValueOperator.between
};
cellValueConditionalFormatting.setRule(rule);
// Set the format to apply when the condition is met.
let format = cellValueConditionalFormatting.getFormat();
format.getFill().setColor("yellow");
format.getFont().setItalic(true);
}
'ExcelScript.ConditionalDataBarRule:interface':
- |-
/**
* This script creates data bar conditional formatting on the selected range.
* The scale of the data bar goes from 0 to 1000.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the selected range.
const selected = workbook.getSelectedRange();
// Create new conditional formatting on the range.
const format = selected.addConditionalFormat(ExcelScript.ConditionalFormatType.dataBar);
const dataBarFormat: ExcelScript.DataBarConditionalFormat = format.getDataBar();
// Set the lower bound of the data bar formatting to be 0.
const lowerBound: ExcelScript.ConditionalDataBarRule = {
type: ExcelScript.ConditionalFormatRuleType.number,
formula: "0"
};
dataBarFormat.setLowerBoundRule(lowerBound);
// Set the upper bound of the data bar formatting to be 1000.
const upperBound: ExcelScript.ConditionalDataBarRule = {
type: ExcelScript.ConditionalFormatRuleType.number,
formula: "1000"
};
dataBarFormat.setUpperBoundRule(upperBound);
}
'ExcelScript.ConditionalFormat#getCellValue:member(1)':
- |-
/**
* This script applies conditional formatting to a range.
* That formatting is conditional upon the cell's numerical value.
* Any value between 50 and 75 will have the cell fill color changed and the font made italic.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the range to format.
const sheet = workbook.getActiveWorksheet();
const ratingColumn = sheet.getRange("D2:D20");
// Add cell value conditional formatting.
const cellValueConditionalFormatting =
ratingColumn.addConditionalFormat(ExcelScript.ConditionalFormatType.cellValue).getCellValue();
// Create the condition, in this case when the cell value is between 50 and 75.
let rule: ExcelScript.ConditionalCellValueRule = {
formula1: "50",
formula2: "75",
operator: ExcelScript.ConditionalCellValueOperator.between
};
cellValueConditionalFormatting.setRule(rule);
// Set the format to apply when the condition is met.
let format = cellValueConditionalFormatting.getFormat();
format.getFill().setColor("yellow");
format.getFont().setItalic(true);
}
'ExcelScript.ConditionalFormat#getColorScale:member(1)':
- |-
/**
* This script applies a red, white, and blue color scale to the selected range.
*/
function main(workbook: ExcelScript.Workbook) {