|
| 1 | +--- |
| 2 | +title: Add checkboxes using the Excel JavaScript API |
| 3 | +description: Learn how to add checkboxes using the Excel JavaScript API. |
| 4 | +ms.date: 04/08/2025 |
| 5 | +ms.localizationpriority: medium |
| 6 | +--- |
| 7 | + |
| 8 | +# Add checkboxes using the Excel JavaScript API |
| 9 | + |
| 10 | +This article provides code samples that add, edit, and remove checkboxes from a 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). |
| 11 | + |
| 12 | +To use checkboxes, make sure that your range contains Boolean values, like **TRUE** or **FALSE**. Only Boolean values can be replaced with checkboxes using the Excel JavaScript API. |
| 13 | + |
| 14 | +The following screenshot shows an example of checkboxes in a table. The table lists a variety of items, and the checkboxes indicate whether or not the items are types of fruit. |
| 15 | + |
| 16 | +:::image type="content" source="../images/excel-range-checkbox-table.png" alt-text="A table with checkboxes in the second column."::: |
| 17 | + |
| 18 | +> [!NOTE] |
| 19 | +> To experiment with the code snippets in this article in a complete sample, open [Script Lab](../overview/explore-with-script-lab.md) in Excel and select [Checkboxes](https://github.com/OfficeDev/office-js-snippets/blob/prod/samples/excel/42-range/range-cell-control.yaml) in our **Samples** library. |
| 20 | +
|
| 21 | +## Add checkboxes |
| 22 | + |
| 23 | +To add checkboxes to a range, use the [`Range.control`](/javascript/api/excel/excel.range#excel-excel-range-control-member) property to access the [`CellControl`](/javascript/api/excel/excel.cellcontrol) type, and set the `CellControlType` enum value to `checkbox`. Only Boolean values, like **TRUE** or **FALSE**, display as checkboxes in your range. The following code sample shows how to add checkboxes to the **Analysis** column of a table named **FruitTable**. |
| 24 | + |
| 25 | +```js |
| 26 | +await Excel.run(async (context) => { |
| 27 | + // This code sample shows how to add checkboxes to a table. |
| 28 | + const sheet = context.workbook.worksheets.getActiveWorksheet(); |
| 29 | + |
| 30 | + // Get the "Analysis" column in the table, without the header. |
| 31 | + const range = sheet.tables.getItem("FruitTable").columns.getItem("Analysis").getDataBodyRange(); |
| 32 | + |
| 33 | + // Change the Boolean values in the range to checkboxes. |
| 34 | + range.control = { |
| 35 | + type: Excel.CellControlType.checkbox |
| 36 | + }; |
| 37 | + await context.sync(); |
| 38 | +}); |
| 39 | +``` |
| 40 | + |
| 41 | +## Change the value of a checkbox |
| 42 | + |
| 43 | +To select or clear a checkbox with the Excel JavaScript API, change the Boolean value in that cell. Use `Range.values` to change the value of a cell. The following code sample shows how to set the value of a cell to **TRUE**. Note that if the cell doesn't already display a checkbox, then the code sample simply changes the Boolean value of the cell. |
| 44 | + |
| 45 | +```js |
| 46 | +await Excel.run(async (context) => { |
| 47 | + // This code sample shows how to change the value of cell B3. |
| 48 | + const sheet = context.workbook.worksheets.getActiveWorksheet(); |
| 49 | + const range = sheet.getRange("B3"); |
| 50 | + |
| 51 | + range.values = [["TRUE"]]; |
| 52 | + await context.sync(); |
| 53 | +}); |
| 54 | +``` |
| 55 | + |
| 56 | +## Remove checkboxes |
| 57 | + |
| 58 | +To remove checkboxes from a range and return the values to simple Booleans, use the [`Range.control`](/javascript/api/excel/excel.range#excel-excel-range-control-member) property to access the [`CellControl`](/javascript/api/excel/excel.cellcontrol) type, and set the `CellControlType` enum value to `empty`. The following code sample shows how to remove checkboxes from the **Analysis** column of a table named **FruitTable**. |
| 59 | + |
| 60 | +```js |
| 61 | +await Excel.run(async (context) => { |
| 62 | + // This code sample shows how to remove checkboxes from a table. |
| 63 | + const sheet = context.workbook.worksheets.getActiveWorksheet(); |
| 64 | + |
| 65 | + // Get the "Analysis" column in the table, without the header. |
| 66 | + const range = sheet.tables.getItem("FruitTable").columns.getItem("Analysis").getDataBodyRange(); |
| 67 | + |
| 68 | + // Change the checkboxes to Boolean values. |
| 69 | + range.control = { |
| 70 | + type: Excel.CellControlType.empty |
| 71 | + }; |
| 72 | + await context.sync(); |
| 73 | +}); |
| 74 | +``` |
| 75 | + |
| 76 | +> [!NOTE] |
| 77 | +> To remove all content from a range, use the [`Range.clearOrResetContents`](/javascript/api/excel/excel.range#excel-excel-range-clearorresetcontents-member(1)) method. |
| 78 | +
|
| 79 | +## See also |
| 80 | + |
| 81 | +- [Excel JavaScript object model in Office Add-ins](excel-add-ins-core-concepts.md) |
| 82 | +- [Work with cells using the Excel JavaScript API](excel-add-ins-cells.md) |
| 83 | +- [Insert a range using the Excel JavaScript API](excel-add-ins-ranges-insert.md) |
0 commit comments