-
Notifications
You must be signed in to change notification settings - Fork 262
[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
davidchesnut
wants to merge
10
commits into
main
Choose a base branch
from
davech-c
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f2dec41
new article on how to add properties to basic cell types
davidchesnut f7eccba
Apply suggestions from code review
davidchesnut c472eba
fix formatting
davidchesnut 4146a1f
refactoring and fixes.
davidchesnut 2c68d7f
Additional new API information for data types
davidchesnut b637b24
fix broken links
davidchesnut b403895
Merge branch 'main' into davech-c
davidchesnut 7bd616a
fix broken links
davidchesnut 3c7230c
add linked entity types to TOC
davidchesnut 69870ac
fix typo
davidchesnut File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
218 changes: 218 additions & 0 deletions
218
docs/excel/excel-data-types-add-properties-to-basic-cell-values.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.