Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions docs/excel/excel-add-ins-pivottables.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
---
title: Work with PivotTables using the Excel JavaScript API
description: Use the Excel JavaScript API to create PivotTables and interact with their components.
ms.date: 03/04/2022
description: Learn how to create, configure, and filter PivotTables programmatically with the Excel JavaScript API.
ms.date: 09/22/2025
ms.localizationpriority: medium
---

# Work with PivotTables using the Excel JavaScript API

PivotTables streamline larger data sets. They allow the quick manipulation of grouped data. The Excel JavaScript API lets your add-in create PivotTables and interact with their components. This article describes how PivotTables are represented by the Office JavaScript API and provides code samples for key scenarios.
Use the PivotTable APIs to summarize data, group fields, filter, and apply calculations in a workbook.

If you are unfamiliar with the functionality of PivotTables, consider exploring them as an end user.
See [Create a PivotTable to analyze worksheet data](https://support.microsoft.com/office/ccd3c4a6-272f-4c97-afbb-d3f27407fcde#ID0EBBD=PivotTables) for a good primer on these tools.
## Key concepts

- Four hierarchy categories: rows, columns, data, and filters.
- Adding a hierarchy to a new category removes it from the old one.
- `PivotLayout` gives you the output ranges (for example, `getDataBodyRange`).
- Filtering options: code (PivotFilters) or UI (slicers).
- Add multiple hierarchies, then call `context.sync()` once for speed.

If you're unfamiliar with PivotTables as an end user, review [Create a PivotTable to analyze worksheet data](https://support.microsoft.com/office/ccd3c4a6-272f-4c97-afbb-d3f27407fcde#ID0EBBD=PivotTables).

> [!IMPORTANT]
> PivotTables created with OLAP are not currently supported. There is also no support for Power Pivot.
Expand All @@ -21,9 +28,9 @@ See [Create a PivotTable to analyze worksheet data](https://support.microsoft.co

The [PivotTable](/javascript/api/excel/excel.pivottable) is the central object for PivotTables in the Office JavaScript API.

- `Workbook.pivotTables` and `Worksheet.pivotTables` are [PivotTableCollections](/javascript/api/excel/excel.pivottablecollection) that contain the [PivotTables](/javascript/api/excel/excel.pivottable) in the workbook and worksheet, respectively.
- A [PivotTable](/javascript/api/excel/excel.pivottable) contains a [PivotHierarchyCollection](/javascript/api/excel/excel.pivothierarchycollection) that has multiple [PivotHierarchies](/javascript/api/excel/excel.pivothierarchy).
- These [PivotHierarchies](/javascript/api/excel/excel.pivothierarchy) can be added to specific hierarchy collections to define how the PivotTable pivots data (as explained in the [following section](#hierarchies)).
- `Workbook.pivotTables` and `Worksheet.pivotTables` are [PivotTableCollections](/javascript/api/excel/excel.pivottablecollection) that contain the [PivotTables](/javascript/api/excel/excel.pivottable) in the workbook and worksheet.
- A [PivotTable](/javascript/api/excel/excel.pivottable) has a [PivotHierarchyCollection](/javascript/api/excel/excel.pivothierarchycollection) with multiple [PivotHierarchies](/javascript/api/excel/excel.pivothierarchy).
- [PivotHierarchies](/javascript/api/excel/excel.pivothierarchy) can be added to specific hierarchy collections to define how the PivotTable pivots data.
- A [PivotHierarchy](/javascript/api/excel/excel.pivothierarchy) contains a [PivotFieldCollection](/javascript/api/excel/excel.pivotfieldcollection) that has exactly one [PivotField](/javascript/api/excel/excel.pivotfield). If the design expands to include OLAP PivotTables, this may change.
- A [PivotField](/javascript/api/excel/excel.pivotfield) can have one or more [PivotFilters](/javascript/api/excel/excel.pivotfilters) applied, as long as the field's [PivotHierarchy](/javascript/api/excel/excel.pivothierarchy) is assigned to a hierarchy category.
- A [PivotField](/javascript/api/excel/excel.pivotfield) contains a [PivotItemCollection](/javascript/api/excel/excel.pivotitemcollection) that has multiple [PivotItems](/javascript/api/excel/excel.pivotitem).
Expand All @@ -37,9 +44,9 @@ This fruit farm sales data will be used to make a PivotTable. Each column, such

### Hierarchies

PivotTables are organized based on four hierarchy categories: [row](/javascript/api/excel/excel.rowcolumnpivothierarchy), [column](/javascript/api/excel/excel.rowcolumnpivothierarchy), [data](/javascript/api/excel/excel.datapivothierarchy), and [filter](/javascript/api/excel/excel.filterpivothierarchy).
PivotTables have four hierarchy categories: [row](/javascript/api/excel/excel.rowcolumnpivothierarchy), [column](/javascript/api/excel/excel.rowcolumnpivothierarchy), [data](/javascript/api/excel/excel.datapivothierarchy), and [filter](/javascript/api/excel/excel.filterpivothierarchy).

The farm data shown earlier has five hierarchies: **Farms**, **Type**, **Classification**, **Crates Sold at Farm**, and **Crates Sold Wholesale**. Each hierarchy can only exist in one of the four categories. If **Type** is added to column hierarchies, it cannot also be in the row, data, or filter hierarchies. If **Type** is subsequently added to row hierarchies, it is removed from the column hierarchies. This behavior is the same whether hierarchy assignment is done through the Excel UI or the Excel JavaScript APIs.
Our farm data has five hierarchies: **Farms**, **Type**, **Classification**, **Crates Sold at Farm**, and **Crates Sold Wholesale**. Each hierarchy can only exist in one of the four categories. If **Type** is added to column hierarchies, it cannot also be in the row, data, or filter hierarchies. If **Type** is subsequently added to row hierarchies, it is removed from the column hierarchies. This behavior is the same whether hierarchy assignment is done through the Excel UI or the Excel JavaScript APIs.

Row and column hierarchies define how data will be grouped. For example, a row hierarchy of **Farms** will group together all the data sets from the same farm. The choice between row and column hierarchy defines the orientation of the PivotTable.

Expand All @@ -55,7 +62,7 @@ This PivotTable could be generated through the JavaScript API or through the Exc

## Create a PivotTable

PivotTables need a name, source, and destination. The source can be a range address or table name (passed as a `Range`, `string`, or `Table` type). The destination is a range address (given as either a `Range` or `string`).
Provide a name, source, and destination. The source can be a range address or table name (passed as a `Range`, `string`, or `Table` type). The destination is a range address (given as either a `Range` or `string`).
The following samples show various PivotTable creation techniques.

### Create a PivotTable with range addresses
Expand Down Expand Up @@ -112,9 +119,7 @@ await Excel.run(async (context) => {

## Add rows and columns to a PivotTable

Rows and columns pivot the data around those fields' values.

Adding the **Farm** column pivots all the sales around each farm. Adding the **Type** and **Classification** rows further breaks down the data based on what fruit was sold and whether it was organic or not.
Rows and columns define how data is grouped. Adding the **Farm** column groups all the sales around each farm. Adding the **Type** and **Classification** rows further breaks down the data based on what fruit was sold and whether it was organic or not.

![A PivotTable with a Farm column and Type and Classification rows.](../images/excel-pivots-table-rows-and-columns.png)

Expand Down Expand Up @@ -238,7 +243,7 @@ await Excel.run(async (context) => {

### Other PivotLayout functions

By default, PivotTables adjust row and column sizes as needed. This is done when the PivotTable is refreshed. `PivotLayout.autoFormat` specifies that behavior. Any row or column size changes made by your add-in persist when `autoFormat` is `false`. Additionally, the default settings of a PivotTable keep any custom formatting in the PivotTable (such as fills and font changes). Set `PivotLayout.preserveFormatting` to `false` to apply the default format when refreshed.
By default, PivotTables adjust row and column sizes as needed. This occurs when the PivotTable is refreshed. `PivotLayout.autoFormat` specifies that behavior. Any row or column size changes made by your add-in persist when `autoFormat` is `false`. Additionally, the default settings of a PivotTable keep any custom formatting in the PivotTable (such as fills and font changes). Set `PivotLayout.preserveFormatting` to `false` to apply the default format when refreshed.

A `PivotLayout` also controls header and total row settings, how empty data cells are displayed, and [alt text](https://support.microsoft.com/topic/44989b2a-903c-4d9a-b742-6a75b451c669) options. The [PivotLayout](/javascript/api/excel/excel.pivotlayout) reference provides a complete list of these features.

Expand Down Expand Up @@ -417,7 +422,7 @@ await Excel.run(async (context) => {

#### Create a slicer

You can create a slicer in a workbook or worksheet by using the `Workbook.slicers.add` method or `Worksheet.slicers.add` method. Doing so adds a slicer to the [SlicerCollection](/javascript/api/excel/excel.slicercollection) of the specified `Workbook` or `Worksheet` object. The `SlicerCollection.add` method has three parameters:
Create a slicer in a workbook or worksheet with the `slicers.add` method. This adds a slicer to the [SlicerCollection](/javascript/api/excel/excel.slicercollection) of the specified `Workbook` or `Worksheet` object. The `SlicerCollection.add` method has three parameters:

- `slicerSource`: The data source on which the new slicer is based. It can be a `PivotTable`, `Table`, or string representing the name or ID of a `PivotTable` or `Table`.
- `sourceField`: The field in the data source by which to filter. It can be a `PivotField`, `TableColumn`, or string representing the name or ID of a `PivotField` or `TableColumn`.
Expand Down Expand Up @@ -467,7 +472,7 @@ await Excel.run(async (context) => {

#### Style and format a slicer

You add-in can adjust a slicer's display settings through `Slicer` properties. The following code sample sets the style to **SlicerStyleLight6**, sets the text at the top of the slicer to **Fruit Types**, places the slicer at the position **(395, 15)** on the drawing layer, and sets the slicer's size to **135x150** pixels.
Adjust a slicer's display settings with `Slicer` properties. The following code sample sets the style to **SlicerStyleLight6**, sets the text at the top of the slicer to **Fruit Types**, places the slicer at the position **(395, 15)** on the drawing layer, and sets the slicer's size to **135x150** pixels.

```js
await Excel.run(async (context) => {
Expand Down
15 changes: 12 additions & 3 deletions docs/excel/excel-add-ins-ranges-remove-duplicates.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
---
title: Remove duplicates using the Excel JavaScript API
description: Learn how to use the Excel JavaScript API to remove duplicates.
ms.date: 02/17/2022
ms.date: 09/22/2025
ms.localizationpriority: medium
---

# Remove duplicates using the Excel JavaScript API

This article provides a code sample that removes duplicate entries in a range using the Excel JavaScript API. For the complete list of properties and methods that the `Range` object supports, see [Excel.Range class](/javascript/api/excel/excel.range).
This article shows how to remove duplicate rows within a range using the [Range.removeDuplicates](/javascript/api/excel/excel.range#excel-excel-range-removeduplicates-member(1)) method. For the full list of range members, see [Excel.Range class](/javascript/api/excel/excel.range).

## Key points

- It works only inside the supplied `range`.
- Column indexes are zero-based and relative to the range.
- Set `includeHeader: true` to skip the first row when checking duplicates.
- Empty cells count like any other value. Two empty cells can be duplicates.
- It compares stored cell values, not formula logic.
- It returns `RemoveDuplicatesResult` with `removed` and `uniqueRemaining` counts.

## Remove rows with duplicate entries

The [Range.removeDuplicates](/javascript/api/excel/excel.range#excel-excel-range-removeduplicates-member(1)) method removes rows with duplicate entries in the specified columns. The method goes through each row in the range from the lowest-valued index to the highest-valued index in the range (from top to bottom). A row is deleted if a value in its specified column or columns appeared earlier in the range. Rows in the range below the deleted row are shifted up. `removeDuplicates` does not affect the position of cells outside of the range.
The `removeDuplicates` method removes rows with duplicate entries in the specified columns. The method goes through each row in the range from the lowest-valued index to the highest-valued index in the range (from top to bottom). A row is deleted if a value in its specified column or columns appeared earlier in the range. Rows in the range below the deleted row are shifted up. `removeDuplicates` doesn't affect the position of cells outside of the range.

`removeDuplicates` takes in a `number[]` representing the column indices which are checked for duplicates. This array is zero-based and relative to the range, not the worksheet. The method also takes in a boolean parameter that specifies whether the first row is a header. When `true`, the top row is ignored when considering duplicates. The `removeDuplicates` method returns a `RemoveDuplicatesResult` object that specifies the number of rows removed and the number of unique rows remaining.

Expand Down
33 changes: 24 additions & 9 deletions docs/excel/excel-add-ins-ranges-unbounded.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
---
title: Read or write to an unbounded range using the Excel JavaScript API
description: Learn how to use the Excel JavaScript API to read or write to an unbounded range.
ms.date: 02/17/2022
description: Understand what unbounded ranges are, why cell-level operations return null or fail, and how to safely work with entire rows or columns by narrowing scope.
ms.date: 09/22/2025
ms.localizationpriority: medium
---

# Read or write to an unbounded range using the Excel JavaScript API

This article describes how to read and write to an unbounded range with the Excel JavaScript API. For the complete list of properties and methods that the `Range` object supports, see [Excel.Range class](/javascript/api/excel/excel.range).
Use these guidelines to understand how entire-column and entire-row addresses behave, and apply patterns that reduce errors and memory usage. For the complete list of properties and methods that the `Range` object supports, see [Excel.Range class](/javascript/api/excel/excel.range).

An unbounded range address is a range address that specifies either entire columns or entire rows. For example:
## Key points

- "Unbounded" means entire columns (like `A:F`) or entire rows (such as `2:2`).
- Cell-level properties (like `values`, `text`, `numberFormat`, or `formulas`) come back as `null` for unbounded reads.
- You can't set cell-level properties on an unbounded range. This returns an error.
- Narrow to the used cells first with `getUsedRange()`.
- Prefer explicit bounds (like `A1:F5000`) for faster calculation speeds and lower memory use.

The following are examples of unbounded ranges.

- Range addresses comprised of entire columns.
- `C:C`
Expand All @@ -20,18 +28,25 @@ An unbounded range address is a range address that specifies either entire colum

## Read an unbounded range

When the API makes a request to retrieve an unbounded range (for example, `getRange('C:C')`), the response will contain `null` values for cell-level properties such as `values`, `text`, `numberFormat`, and `formula`. Other properties of the range, such as `address` and `cellCount`, will contain valid values for the unbounded range.
When you request an unbounded range (for example, `getRange('C:C')`), the response returns `null` for cell-level properties like `values`, `text`, `numberFormat`, and `formula`. Other properties (`address`, `cellCount`) are still valid.

## Write to an unbounded range

You cannot set cell-level properties such as `values`, `numberFormat`, and `formula` on an unbounded range because the input request is too large. For example, the following code example is not valid because it attempts to specify `values` for an unbounded range. The API returns an error if you attempt to set cell-level properties for an unbounded range.
You can't set cell-level properties like `values`, `numberFormat`, or `formula` on an unbounded range because the request is too large. For example, the next code sample fails because it sets `values` for an unbounded range.

```js
// Note: This code sample attempts to specify `values` for an unbounded range, which is not a valid request. The sample will return an error.
let range = context.workbook.worksheets.getActiveWorksheet().getRange('A:B');
range.values = 'Due Date';
// Invalid: Attempting to write cell-level data to unbounded columns.
let range = context.workbook.worksheets.getActiveWorksheet().getRange("A:B");
range.values = [["Due Date"]]; // This throws an error.
```

## Next steps

- Learn strategies for [large bounded ranges](excel-add-ins-ranges-large.md).
- Combine multiple explicit ranges with [multiple ranges](excel-add-ins-multiple-ranges.md).
- Optimize performance with [resource limits guidance](../concepts/resource-limits-and-performance-optimization.md#excel-add-ins).
- Identify specific cells using [special cells](excel-add-ins-ranges-special-cells.md).

## See also

- [Excel JavaScript object model in Office Add-ins](excel-add-ins-core-concepts.md)
Expand Down
10 changes: 5 additions & 5 deletions docs/excel/excel-add-ins-troubleshooting.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
title: Troubleshooting Excel add-ins
description: Learn how to troubleshoot development errors in Excel add-ins.
title: Troubleshoot Excel add-ins
description: 'Diagnose and resolve common Excel add-in errors: workbook focus changes, API limitations, and known issues.'
ms.date: 09/22/2025
ms.topic: troubleshooting-error-codes
ms.localizationpriority: medium
---

# Troubleshooting Excel add-ins
# Troubleshoot Excel add-ins

This article discusses troubleshooting issues that are unique to Excel. Please use the feedback tool at the bottom of the page to suggest other issues that can be added to the article.

Expand Down Expand Up @@ -38,11 +38,11 @@ The following APIs are affected by this workbook switch.
> [!NOTE]
> This only applies to multiple Excel workbooks open on Windows or Mac.

## Coauthoring
## Coauthoring and merge conflicts

See [Coauthoring in Excel add-ins](co-authoring-in-excel-add-ins.md) for patterns to use with events in a coauthoring environment. The article also discusses potential merge conflicts when using certain APIs, such as [`TableRowCollection.add`](/javascript/api/excel/excel.tablerowcollection#excel-excel-tablerowcollection-add-member(1)).

## Known Issues
## Known issues

### Binding events return temporary `Binding` objects

Expand Down