Skip to content

[Excel](data types) New APIs for linked entities #5091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
218 changes: 218 additions & 0 deletions docs/excel/excel-data-types-add-properties-to-basic-cell-values.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
---
title: Add properties to basic cell values
description: Add properties to basic cell values.
ms.topic: how-to #Required; leave this attribute/value as-is
ms.date: 04/14/2025
ms.localizationpriority: medium
---

# Add properties to basic cell values
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this article be tagged as preview? Should any of the other content in this PR (some of the entity card content?) be tagged as preview?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not available to all, then yes it should be tagged preview.


Add properties to basic cell values in Excel to associate additional information with the values. Similar to entity values, you can add properties to the **string**, **double**, and **Boolean** basic types. Each property is a key/value pair. The following example shows the number 104.67 (double) that represents a bill with added fields named **Drinks**, **Food**, **Tax**, and **Tip**.

:::image type="content" source="../images/data-type-basic-fields.png" alt-text="Screen shot of the drinks, food, tax, and tip fields shown for the selected cell value.":::

If the user chooses to show the data type card, they'll see the values for the fields.

:::image type="content" source="../images/data-type-basic-data-type-card.png" alt-text="Data type card showing values for drinks, food, tax, and tip properties":::

Cell value properties can also be used in formulas.

:::image type="content" source="../images/data-type-basic-dot-syntax.png" alt-text="Show user typing 'a1.' and Excel showing a menu with drinks, food, tax, and tip options.":::

## Create a cell value with properties

To create a cell value and add properties to it, use `valuesAsJson` to assign properties. The following code sample shows how to create a new number in cell **A1**. It adds the **Food**, **Drinks**, and additional properties describing a bill in a restaurant. It assigns a JSON description of the properties to `Range.valuesAsJson`.

```javascript
async function createNumberProperties() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const range = sheet.getRange("A1");
range.valuesAsJson = [
[
{
type: Excel.CellValueType.double,
basicType: Excel.RangeValueType.double,
basicValue: 104.67,
properties: {
Food: {
type: Excel.CellValueType.string,
basicType: Excel.RangeValueType.string,
basicValue: "Sandwich and fries"
},
Drinks: {
type: Excel.CellValueType.string,
basicType: Excel.RangeValueType.string,
basicValue: "Soda"
},
Tax: {
type: Excel.CellValueType.double,
basicType: Excel.RangeValueType.double,
basicValue: 5.5
},
Tip: {
type: Excel.CellValueType.double,
basicType: Excel.RangeValueType.double,
basicValue: 21
}
}
}
]
];
await context.sync();
});
}
```

> [!NOTE]
> Some cell values change based on a user's locale. The `valuesAsJsonLocal` property offers localization support and is available on all the same objects as `valuesAsJson`.

## Add properties to an existing value

To add properties to an existing value, first get the value from the cell using `valuesAsJson` , then add a properties JSON object to it. The following example shows how to get the number value from cell **A1** and assign a property named **Precision** to it. Note that you should check the type of the value to ensure it is a **string**, **double**, or **Boolean** basic type.

```javascript
async function addPropertyToNumber() {
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getActiveWorksheet();
let range = sheet.getRange("A1");
range.load("valuesAsJson");
await context.sync();
let cellValue = range.valuesAsJson[0][0] as any;

// Only apply this property to a double.
if (cellValue.basicType === "Double") {
cellValue.properties = {
Precision: {
type: Excel.CellValueType.double,
basicValue: 4
}
};
range.valuesAsJson = [[cellValue]];
await context.sync();
}
});
}
```

## Differences from entity values

Adding properties to **string**, **Boolean**, and **double** basic types is similar to adding properties to entity values. However there are differences.

- Basic types have a non-error fallback so that calculations can operate on them. For example, consider the formula **=SUM(A1:A3)** where **A1** is **1** (a double with properties), **A2** is **2**, and **A3** is **3**. The sum will return the correct result of **6**. The formula would not work if **A1** was an entity value.
- When the value of a basic type is used in a calculation, the properties are discarded. In the previous example of **=SUM(A1:A3)** where A1 is a double with properties, the result of **6** will not have any properties.
- If no icon is specified for a basic type, the cell does not show any icon. If an entity value does not specify an icon, it will still show a default icon in the cell value.

## Formatted number values

You can apply number formatting to values of type **CellValueType.double**. Use the **numberFormat** property in the JSON schema to specify a number format. The following code sample shows the complete schema of a number value formatted as currency. The formatted number value in the code sample displays as **$24.00** in the Excel UI.

```javascript
// This is an example of the complete JSON of a formatted number value with a property.
// In this case, the number is formatted as currency.
async function createCurrencyValue() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const range = sheet.getRange("A1");
range.valuesAsJson = [
[
{
type: Excel.CellValueType.double,
basicType: Excel.RangeValueType.double,
basicValue: 24,
numberFormat: "$0.00",
properties: {
Name: {
type: Excel.CellValueType.string,
basicValue: "dollar"
}
}
}
]
];
await context.sync();
});
}
```

The number formatting is considered the default format. If the user, or other code, applies formatting to a cell containing a formatted number, the applied format will override the number’s format.

## Card layout

Cell values with properties have a default data type card that the user can view. You can provide a custom card layout to use instead of the default card layout to improve the user experience when viewing properties. To do this, add the **layouts** property to the JSON description.

For more information, see [Use cards with cell value data types](excel-data-types-entity-card.md).

## Nested data types

You can nest data types in a cell value, such as additional entity values, as well as **strings**, **doubles**, and **Booleans**. The following code sample shows how to create a cell value that represents the charge status on a computer battery. It contains a nested entity value that describes the computer properties for power consumption and charging status. The computer entity value also contains a nested string value that describes the computer’s power plan.

```javascript
async function createNumberWithNestedEntity() {
await Excel.run(async (context) => {
const sheet = context.workbook.worksheets.getActiveWorksheet();
const range = sheet.getRange("A1");
range.valuesAsJson = [
[
{
type: Excel.CellValueType.double,
basicType: Excel.RangeValueType.double,
layouts: {
compact: {
icon: "Battery10"
}
},
basicValue: 0.7,
numberFormat: "00%",
properties: {
Computer: {
type: Excel.CellValueType.entity,
text: "Laptop",
properties: {
"Power Consumption": {
type: Excel.CellValueType.double,
basicType: Excel.RangeValueType.double,
basicValue: 0.25,
numberFormat: "00%",
layouts: {
compact: {
icon: "Power"
}
},
properties: {
plan: {
type: Excel.CellValueType.string,
basicType: Excel.RangeValueType.string,
basicValue: "Balanced"
}
}
},
Charging: {
type: Excel.CellValueType.boolean,
basicType: Excel.RangeValueType.boolean,
basicValue: true
}
}
}
}
}
]
];
await context.sync();
});
}
```

The following image shows the number value and the data type card for the nested laptop entity.

:::image type="content" source="../images/data-type-basic-nested-entities.png" alt-text="Cell value in Excel showing battery charge at 70% and the data type card showing the nested laptop entity with charging and power consuption property values.":::

## Compatibility

On previous versions of Excel that do not support the data types feature, users will see a warning of **Unavailable Data Type**. The value will still show in the cell and function as expected with formulas and other Excel features. If the value is a formatted number, calculations use the **basicValue** in place of the formatted number.
On Excel versions older than Office 2016, the value will be shown in the cell with no error and will be indistinguishable from a basic value.

## Related content

- [Excel JavaScript API data types core concepts](excel-data-types-concepts.md)
37 changes: 28 additions & 9 deletions docs/excel/excel-data-types-concepts.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: Excel JavaScript API data types core concepts
description: Learn the core concepts for using Excel data types in your Office Add-in.
ms.date: 10/14/2022
ms.date: 04/14/2025
ms.topic: overview
ms.custom: scenarios:getting-started
ms.localizationpriority: high
Expand Down Expand Up @@ -37,8 +37,10 @@ The `valuesAsJson` property returns a [CellValue](/javascript/api/excel/excel.ce
- [EmptyCellValue](/javascript/api/excel/excel.emptycellvalue)
- [EntityCellValue](/javascript/api/excel/excel.entitycellvalue)
- [ErrorCellValue](/javascript/api/excel/excel.errorcellvalue)
- [FormattedNumberCellValue](/javascript/api/excel/excel.formattednumbercellvalue)
- [ExternalCodeServiceObjectCellValue](/javascript/api/excel/excel.externalcodeserviceobjectcellvalue)
- [FunctionCellValue](/javascript/api/excel/excel.functioncellvalue)
- [LinkedEntityCellValue](/javascript/api/excel/excel.linkedentitycellvalue)
- [LocalImageCellValue](/javascript/api/excel/excel.localimagecellvalue)
- [ReferenceCellValue](/javascript/api/excel/excel.referencecellvalue)
- [StringCellValue](/javascript/api/excel/excel.stringcellvalue)
- [ValueTypeNotAvailableCellValue](/javascript/api/excel/excel.valuetypenotavailablecellvalue)
Expand All @@ -58,23 +60,33 @@ The following sections show JSON code samples for the formatted number value, en

## Formatted number values

The [FormattedNumberCellValue](/javascript/api/excel/excel.formattednumbercellvalue) object enables Excel add-ins to define a `numberFormat` property for a value. Once assigned, this number format travels through calculations with the value and can be returned by functions.
The [DoubleCellCellValue](/javascript/api/excel/excel.doublecellvalue) object enables Excel add-ins to define a `numberFormat` property for a value. Once assigned, this number format travels through calculations with the value and can be returned by functions.

The following JSON code sample shows the complete schema of a formatted number value. The `myDate` formatted number value in the code sample displays as **1/16/1990** in the Excel UI. If the minimum compatibility requirements for the data types feature aren't met, calculations use the `basicValue` in place of the formatted number.

```TypeScript
// This is an example of the complete JSON of a formatted number value.
// In this case, the number is formatted as a date.
const myDate: Excel.FormattedNumberCellValue = {
type: Excel.CellValueType.formattedNumber,
const myDate: Excel.DoubleCellValue = {
type: Excel.CellValueType.double,
basicValue: 32889.0,
basicType: Excel.RangeValueType.double, // A read-only property. Used as a fallback in incompatible scenarios.
numberFormat: "m/d/yyyy"
};
```

The number formatting is considered the default format. If the user, or other code, applies formatting to a cell containing a formatted number, the applied format will override the number’s format.

Begin experimenting with formatted number values by opening [Script Lab](../overview/explore-with-script-lab.md) and checking out the [Data types: Formatted numbers](https://github.com/OfficeDev/office-js-snippets/blob/prod/samples/excel/20-data-types/data-types-formatted-number.yaml) snippet in our **Samples** library.

## Basic cell values

Add properties to basic cell values in Excel to associate additional information with the values. Similar to entity values, you can add properties to the **string**, **double**, and **Boolean** basic types. Each property is a key/value pair. The following example shows the number 104.67 (double) that represents a bill with added fields named **Drinks**, **Food**, **Tax**, and **Tip**.

:::image type="content" source="../images/data-type-basic-fields.png" alt-text="Screen shot of the drinks, food, tax, and tip fields shown for the selected cell value.":::

For more information, see [Add properties to basic cell values](excel-data-types-add-properties-to-basic-cell-values.md).

## Entity values

An entity value is a container for data types, similar to an object in object-oriented programming. Entities also support arrays as properties of an entity value. The [EntityCellValue](/javascript/api/excel/excel.entitycellvalue) object allows add-ins to define properties such as `type`, `text`, and `properties`. The `properties` property enables the entity value to define and contain additional data types.
Expand Down Expand Up @@ -102,13 +114,18 @@ const myEntity: Excel.EntityCellValue = {
};
```

Entity values also offer a `layouts` property that creates a card for the entity. The card displays as a modal window in the Excel UI and can display additional information contained within the entity value, beyond what's visible in the cell. To learn more, see [Use cards with entity value data types](excel-data-types-entity-card.md).

To explore entity data types, start by going to [Script Lab](../overview/explore-with-script-lab.md) in Excel and opening the [Data types: Create entity cards from data in a table](https://github.com/OfficeDev/office-js-snippets/blob/prod/samples/excel/20-data-types/data-types-entity-values.yaml) snippet in our **Samples** library. The [Data types: Entity values with references](https://github.com/OfficeDev/office-js-snippets/blob/prod/samples/excel/20-data-types/data-types-references.yaml) and [Data types: Entity value attribution properties](https://github.com/OfficeDev/office-js-snippets/blob/prod/samples/excel/20-data-types/data-types-entity-attribution.yaml) snippets offer a deeper look at entity features.

### Linked entities
### Linked entity cell values

Linked entity cell values, or [LinkedEntityCellValue](/javascript/api/excel/excel.linkedentitycellvalue) objects, integrated data types from external data sources and can display the data as an entity card. They enable you to scale your data types to represent large data sets without downloading all the data into the workbook. The [Stocks and Geography data domains](https://support.microsoft.com/office/excel-data-types-stocks-and-geography-61a33056-9935-484f-8ac8-f1a89e210877) available via the Excel UI provide linked entity cell values.

Linked entity cell values are linked to an external data source. They provide the following advantages over regular entity values:

- Linked entity cell values can nest, and nested linked entity cell values are not retrieved until referenced, either by the user, or by the worksheet. This helps reduce file size and improve workbook performance.
- Excel uses a cache to allow different cells to reference the same linked entity cell value seamlessly. This also improves workbook performance.

Linked entity values, or [LinkedEntityCellValue](/javascript/api/excel/excel.linkedentitycellvalue) objects, are a type of entity value. These objects integrate data provided by an external service and can display this data as an [entity card](excel-data-types-entity-card.md), like regular entity values. The [Stocks and Geography data types](https://support.microsoft.com/office/excel-data-types-stocks-and-geography-61a33056-9935-484f-8ac8-f1a89e210877) available via the Excel UI are linked entity values.
For more information, see [Create linked entity cell values](excel-data-types-linked-entity-cell-values.md).

## Web image values

Expand Down Expand Up @@ -164,6 +181,8 @@ Use the [Create and explore data types in Excel](https://github.com/OfficeDev/Of
## See also

- [Overview of data types in Excel add-ins](excel-data-types-overview.md)
- [Create linked entity cell values](excel-data-types-linked-entity-cell-values.md)
- [Add properties to basic cell values](excel-data-types-add-properties-to-basic-cell-values.md)
- [Use cards with entity value data types](excel-data-types-entity-card.md)
- [Create and explore data types in Excel](https://github.com/OfficeDev/Office-Add-in-samples/tree/main/Samples/excel-data-types-explorer)
- [Custom functions and data types](custom-functions-data-types-concepts.md)
Expand Down
Loading