Insert a range of cells using the Excel JavaScript API
This article provides a code sample that inserts a range of cells with the Excel JavaScript API. For the complete list of properties and methods that the Range
object supports, see the Excel.Range class.
Note
The Excel JavaScript API doesn't have a "Cell" object or class. Instead, the Excel JavaScript API defines all Excel cells as Range
objects. An individual cell in the Excel UI translates to a Range
object with one cell in the Excel JavaScript API. A single Range
object can also contain multiple contiguous cells. See Work with cells using the Excel JavaScript API to learn more.
Insert a range of cells
The following code sample inserts a range of cells in location B4:E4 and shifts other cells down to provide space for the new cells.
await Excel.run(async (context) => {
let sheet = context.workbook.worksheets.getItem("Sample");
let range = sheet.getRange("B4:E4");
range.insert(Excel.InsertShiftDirection.down);
await context.sync();
});
Data before range is inserted
Data after range is inserted
See also
Office Add-ins