Skip to content

Commit 29128c4

Browse files
davidchesnutlindalu-MSFTAlexJerabekalison-mkElizabethSamuel-MSFT
authored
[Excel](data types) New APIs for linked entities (#5091)
* new article on how to add properties to basic cell types * Apply suggestions from code review Co-authored-by: Linda Cannon <[email protected]> Co-authored-by: Alex Jerabek <[email protected]> Co-authored-by: Alison McKay <[email protected]> * fix formatting * refactoring and fixes. * Additional new API information for data types * fix broken links * fix broken links * add linked entity types to TOC * fix typo * add preview * Apply suggestions from code review Co-authored-by: Alison McKay <[email protected]> * updates from code review * fix resolution on images * Apply suggestions from code review Co-authored-by: Alison McKay <[email protected]> * Update docs/excel/excel-data-types-add-properties-to-basic-cell-values.md Co-authored-by: Elizabeth Samuel <[email protected]> * Apply suggestions from code review Co-authored-by: Alison McKay <[email protected]> * Apply suggestions from code review Co-authored-by: Alison McKay <[email protected]> * updates from feedback * minor edits * Apply suggestions from code review Co-authored-by: Alex Jerabek <[email protected]> * Apply suggestions from code review Co-authored-by: Alex Jerabek <[email protected]> Co-authored-by: Alison McKay <[email protected]> * updates from review feeback * fix formatting and review feedback * clean up error text * minor fix * fix code labels * minor edit fix * minor edit. --------- Co-authored-by: Linda Cannon <[email protected]> Co-authored-by: Alex Jerabek <[email protected]> Co-authored-by: Alison McKay <[email protected]> Co-authored-by: Elizabeth Samuel <[email protected]>
1 parent 8e68fa1 commit 29128c4

16 files changed

+1032
-72
lines changed
Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
---
2+
title: Add properties to basic cell values
3+
description: Add properties to basic cell values.
4+
ms.topic: how-to #Required; leave this attribute/value as-is
5+
ms.date: 05/12/2025
6+
ms.localizationpriority: medium
7+
---
8+
9+
# Add properties to basic cell values
10+
11+
Add properties to basic cell values in Excel to associate additional information with the values. Similar to [entity values](excel-data-types-linked-entity-cell-values.md), 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 14.67 (a double) that represents a bill with added fields named **Drinks**, **Food**, **Tax**, and **Tip**.
12+
13+
:::image type="content" source="../images/data-type-basic-fields.png" alt-text="Screenshot of the drinks, food, tax, and tip fields shown for the selected cell value.":::
14+
15+
If the user chooses to show the data type card, they'll see the values for the fields.
16+
17+
:::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.":::
18+
19+
Cell value properties can also be used in formulas.
20+
21+
:::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.":::
22+
23+
## Create a cell value with properties
24+
25+
To create a cell value and add properties to it, use `Range.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 `valuesAsJson`.
26+
27+
```typescript
28+
async function createNumberProperties() {
29+
await Excel.run(async (context) => {
30+
const sheet = context.workbook.worksheets.getActiveWorksheet();
31+
const range = sheet.getRange("A1");
32+
range.valuesAsJson = [
33+
[
34+
{
35+
type: Excel.CellValueType.double,
36+
basicType: Excel.RangeValueType.double,
37+
basicValue: 14.67,
38+
properties: {
39+
Food: {
40+
type: Excel.CellValueType.string,
41+
basicType: Excel.RangeValueType.string,
42+
basicValue: "Sandwich and fries"
43+
},
44+
Drinks: {
45+
type: Excel.CellValueType.string,
46+
basicType: Excel.RangeValueType.string,
47+
basicValue: "Soda"
48+
},
49+
Tax: {
50+
type: Excel.CellValueType.double,
51+
basicType: Excel.RangeValueType.double,
52+
basicValue: 5.5
53+
},
54+
Tip: {
55+
type: Excel.CellValueType.double,
56+
basicType: Excel.RangeValueType.double,
57+
basicValue: 21
58+
}
59+
}
60+
}
61+
]
62+
];
63+
await context.sync();
64+
});
65+
}
66+
```
67+
68+
> [!NOTE]
69+
> 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`.
70+
71+
## Add properties to an existing value
72+
73+
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's a **string**, **double**, or **Boolean** basic type.
74+
75+
```typescript
76+
async function addPropertyToNumber() {
77+
await Excel.run(async (context) => {
78+
let sheet = context.workbook.worksheets.getActiveWorksheet();
79+
let range = sheet.getRange("A1");
80+
range.load("valuesAsJson");
81+
await context.sync();
82+
let cellValue = range.valuesAsJson[0][0] as any;
83+
84+
// Only apply this property to a double.
85+
if (cellValue.basicType === "Double") {
86+
cellValue.properties = {
87+
Precision: {
88+
type: Excel.CellValueType.double,
89+
basicValue: 4
90+
}
91+
};
92+
range.valuesAsJson = [[cellValue]];
93+
await context.sync();
94+
}
95+
});
96+
}
97+
```
98+
99+
## Differences from entity values
100+
101+
Adding properties to **string**, **Boolean**, and **double** basic types is similar to adding properties to entity values. However there are differences.
102+
103+
- 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**, **A2** is **2**, and **A3** is **3**. **A1** is a double with properties, while **A2** and **A3** don't have properties. The sum returns the correct result of **6**. The formula wouldn't work if **A1** was an entity value.
104+
- When the value of a basic type is used in a calculation, the properties are excluded in the result. In the previous example of **=SUM(A1:A3)** where A1 is a double with properties, the result of **6** does not have any properties.
105+
- If no icon is specified for a basic type, the cell doesn't show any icon. But if an entity value doesn't specify an icon, it shows a default icon in the cell value.
106+
107+
## Formatted number values
108+
109+
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.
110+
111+
```typescript
112+
// This is an example of the complete JSON of a formatted number value with a property.
113+
// In this case, the number is formatted as currency.
114+
async function createCurrencyValue() {
115+
await Excel.run(async (context) => {
116+
const sheet = context.workbook.worksheets.getActiveWorksheet();
117+
const range = sheet.getRange("A1");
118+
range.valuesAsJson = [
119+
[
120+
{
121+
type: Excel.CellValueType.double,
122+
basicType: Excel.RangeValueType.double,
123+
basicValue: 24,
124+
numberFormat: "$0.00",
125+
properties: {
126+
Name: {
127+
type: Excel.CellValueType.string,
128+
basicValue: "dollar"
129+
}
130+
}
131+
}
132+
]
133+
];
134+
await context.sync();
135+
});
136+
}
137+
```
138+
139+
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 overrides the number’s format.
140+
141+
## Card layout
142+
143+
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.
144+
145+
For more information, see [Use cards with cell value data types](excel-data-types-entity-card.md).
146+
147+
## Nested data types
148+
149+
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.
150+
151+
```typescript
152+
async function createNumberWithNestedEntity() {
153+
await Excel.run(async (context) => {
154+
const sheet = context.workbook.worksheets.getActiveWorksheet();
155+
const range = sheet.getRange("A1");
156+
range.valuesAsJson = [
157+
[
158+
{
159+
type: Excel.CellValueType.double,
160+
basicType: Excel.RangeValueType.double,
161+
layouts: {
162+
compact: {
163+
icon: "Battery10"
164+
}
165+
},
166+
basicValue: 0.7,
167+
numberFormat: "00%",
168+
properties: {
169+
Computer: {
170+
type: Excel.CellValueType.entity,
171+
text: "Laptop",
172+
properties: {
173+
"Power Consumption": {
174+
type: Excel.CellValueType.double,
175+
basicType: Excel.RangeValueType.double,
176+
basicValue: 0.25,
177+
numberFormat: "00%",
178+
layouts: {
179+
compact: {
180+
icon: "Power"
181+
}
182+
},
183+
properties: {
184+
plan: {
185+
type: Excel.CellValueType.string,
186+
basicType: Excel.RangeValueType.string,
187+
basicValue: "Balanced"
188+
}
189+
}
190+
},
191+
Charging: {
192+
type: Excel.CellValueType.boolean,
193+
basicType: Excel.RangeValueType.boolean,
194+
basicValue: true
195+
}
196+
}
197+
}
198+
}
199+
}
200+
]
201+
];
202+
await context.sync();
203+
});
204+
}
205+
```
206+
207+
The following image shows the number value and the data type card for the nested laptop entity.
208+
209+
:::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 consumption property values.":::
210+
211+
## Compatibility
212+
213+
On previous versions of Excel that don't support the data types feature, users see a warning of **Unavailable Data Type**. The value still displays in the cell and functions 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.
214+
215+
On Excel versions older than Office 2016, the value is shown in the cell with no error and is indistinguishable from a basic value.
216+
217+
## See also
218+
219+
- [Excel JavaScript API data types core concepts](excel-data-types-concepts.md)

docs/excel/excel-data-types-concepts.md

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Excel JavaScript API data types core concepts
33
description: Learn the core concepts for using Excel data types in your Office Add-in.
4-
ms.date: 10/14/2022
4+
ms.date: 04/14/2025
55
ms.topic: overview
66
ms.custom: scenarios:getting-started
77
ms.localizationpriority: high
@@ -37,8 +37,10 @@ The `valuesAsJson` property returns a [CellValue](/javascript/api/excel/excel.ce
3737
- [EmptyCellValue](/javascript/api/excel/excel.emptycellvalue)
3838
- [EntityCellValue](/javascript/api/excel/excel.entitycellvalue)
3939
- [ErrorCellValue](/javascript/api/excel/excel.errorcellvalue)
40-
- [FormattedNumberCellValue](/javascript/api/excel/excel.formattednumbercellvalue)
40+
- [ExternalCodeServiceObjectCellValue](/javascript/api/excel/excel.externalcodeserviceobjectcellvalue)
41+
- [FunctionCellValue](/javascript/api/excel/excel.functioncellvalue)
4142
- [LinkedEntityCellValue](/javascript/api/excel/excel.linkedentitycellvalue)
43+
- [LocalImageCellValue](/javascript/api/excel/excel.localimagecellvalue)
4244
- [ReferenceCellValue](/javascript/api/excel/excel.referencecellvalue)
4345
- [StringCellValue](/javascript/api/excel/excel.stringcellvalue)
4446
- [ValueTypeNotAvailableCellValue](/javascript/api/excel/excel.valuetypenotavailablecellvalue)
@@ -58,23 +60,33 @@ The following sections show JSON code samples for the formatted number value, en
5860

5961
## Formatted number values
6062

61-
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.
63+
The [DoubleCellValue](/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.
6264

6365
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.
6466

6567
```TypeScript
6668
// This is an example of the complete JSON of a formatted number value.
6769
// In this case, the number is formatted as a date.
68-
const myDate: Excel.FormattedNumberCellValue = {
69-
type: Excel.CellValueType.formattedNumber,
70+
const myDate: Excel.DoubleCellValue = {
71+
type: Excel.CellValueType.double,
7072
basicValue: 32889.0,
7173
basicType: Excel.RangeValueType.double, // A read-only property. Used as a fallback in incompatible scenarios.
7274
numberFormat: "m/d/yyyy"
7375
};
7476
```
7577

78+
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 overrides the number’s format.
79+
7680
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.
7781

82+
## Basic cell values
83+
84+
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 (a double) that represents a bill with added fields named **Drinks**, **Food**, **Tax**, and **Tip**.
85+
86+
:::image type="content" source="../images/data-type-basic-fields.png" alt-text="Screenshot of the drinks, food, tax, and tip fields shown for the selected cell value.":::
87+
88+
For more information, see [Add properties to basic cell values](excel-data-types-add-properties-to-basic-cell-values.md).
89+
7890
## Entity values
7991

8092
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.
@@ -102,13 +114,18 @@ const myEntity: Excel.EntityCellValue = {
102114
};
103115
```
104116

105-
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).
106-
107117
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.
108118

109-
### Linked entities
119+
### Linked entity cell values
120+
121+
Linked entity cell values, or [LinkedEntityCellValue](/javascript/api/excel/excel.linkedentitycellvalue) objects, are 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.
122+
123+
Linked entity cell values are linked to an external data source. They provide the following advantages over regular entity values:
124+
125+
- Linked entity cell values can nest, and nested linked entity cell values aren't retrieved until referenced, either by the user or by the worksheet. This helps reduce file size and improve workbook performance.
126+
- Excel uses a cache to allow different cells to reference the same linked entity cell value seamlessly. This also improves workbook performance.
110127

111-
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.
128+
For more information, see [Create linked entity cell values](excel-data-types-linked-entity-cell-values.md).
112129

113130
## Web image values
114131

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

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

0 commit comments

Comments
 (0)