Rows and Columns
Row heights, column widths, inserted and deleted rows and hidden columns are all changed through the rows and columns collections of a worksheet. Both collections are bound to their worksheet, so you change the structure and size of a sheet without relying on the selected sheet.
Each size you set names its unit: points, CSS pixels or characters.
Size, Insert and Hide Rows and Columns
The following code sizes a row and two columns of the first worksheet, inserts and removes two rows and then hides and shows two columns. editor is the Spreadsheet of the browser editor:
const sheet = editor.workbook.sheets.at(0);
sheet.rows.get(1).setHeight({ points: 24 });
sheet.columns.get('A').setWidth({ pixels: 180 });
sheet.columns.at(1).setWidth({ characters: 16 });
sheet.rows.insert(4, 2); // Insert two rows before visible row 5.
sheet.rows.remove(4, 2);
sheet.columns.setHidden(3, 2, true); // Hide columns D and E.
sheet.columns.setHidden(3, 2, false);
The code works as follows:
Worksheet.rowsandWorksheet.columnsreturn the rows and the columns of the worksheet, each as aDimensionCollection.get()uses visible labels: one-based row numbers or column letters, such as row1and column'A'.at()and the structural indices are zero-based, soat(1)is column B. Both return aDimension.setHeightsets the height of row 1 to 24 points.setWidthsets column A to 180 CSS pixels and column B to 16 characters.insertinserts two rows before the zero-based index 4, andremovedeletes the two rows at that index again.setHiddenhides two columns from the zero-based index 3 withtrueand shows them again withfalse.
Choose the Units of a Size
| Dimension | Accepted units |
|---|---|
| Row height | Points or CSS pixels |
| Column width | Characters or CSS pixels |
Choose the units explicitly. Character widths depend on the font metrics of the document. Do not treat them as a constant pixel conversion.
size reports the effective CSS-pixel size of a row or column. reset() removes the explicit dimension override.
Fit Rows and Columns to Their Content
In an editor, editor.ui.autoFitRows() and autoFitColumns() measure the current selection after the required fonts are available. For a deliberate subset, pass supported inclusive zero-based bounds as the first and last arguments.
AutoFit is a layout operation, not a promise that a long unbreakable value fits a narrow viewport.
What Structure Changes Affect
Insert and delete operations update supported references and document objects. They can reject invalid bounds, worksheet limits or protected edits.
- Test realistic templates. Check merged cells, table calculated columns, names, drawing anchors and dependent formulas in realistic templates.
- Reacquire handles. Reacquire handles whose addresses should change after structural edits: a range handle retains its stored address.
Hide Rows and Columns
Hiding keeps the data and the stored sizes. It is distinct from AutoFilter and from deleting rows.
Warning
Hidden Data. Hidden data may still be present in downloadable files and must not be used as an authorization mechanism.
Next Steps
- Cells and Ranges: write values, text and formulas into the cells of the rows and columns.
- Font Handling: deploy the fonts whose metrics decide character widths, wrapping and row heights.
- DimensionCollection: look up every member of the
rowsandcolumnscollections in the TypeScript reference.