Skip to contentSheetwrite

Data

Sort the cells, or just the view

Sort 40 orders in place, as Excel does, or for the view only. Then check what the formulas on the Watch sheet read.

Engine @sheetwrite/formulas

Orders
40
Formulas
46
Try it
  1. Row 1 holds …
  2. Cells in their original rows
  3. Revenue in row 1 … · largest order …

How the sort treats the workbook

  • Values, formulas, styles and notes move with their row. A formula that reads its own row, like =D1*E1, keeps reading it.
  • Formulas outside the range keep their references, so they read the new order.
  • Text sorts case-insensitively in Excel's order, and blank cells are last in both directions.
  • Collaborators receive an in-place sort as one transaction. A view sort is saved with the sheet and exports to Excel as its sort state.
Sorting the cells versus sorting only the viewSorting the cells moves values, formulas, styles and notes with their row; a formula that reads its own row keeps reading it, formulas outside the range read the new order, and collaborators receive the sort as one transaction. Sorting only the view leaves the cells in place; the view sort is saved with the sheet and exports to Excel as its sort state.Sort the cellsEach row moves with all it holds;other formulas read the new orderSort the view onlyThe cells stay where they are.Saved and exported with the sheet
Sorting the cells versus sorting only the viewSorting the cells moves values, formulas, styles and notes with their row; a formula that reads its own row keeps reading it, formulas outside the range read the new order, and collaborators receive the sort as one transaction. Sorting only the view leaves the cells in place; the view sort is saved with the sheet and exports to Excel as its sort state.Sort the cellsEach row moves with all it holds;other formulas read the new orderSort the view onlyThe cells stay where they are.Saved and exported with the sheet

The code behind it

sort.ts
import "@sheetwrite/core/styles.css";
import { createGrid, initSheetwrite, type Workbook } from "@sheetwrite/core";
// An "orders" sheet: a header row and 39 orders in columns A to F
declare const workbook: Workbook;
// The grid fills its host, so the host needs a height
const host = document.querySelector<HTMLElement>("#grid");
if (host === null) throw new Error("Missing #grid host");
host.style.height = "480px";
const grid = createGrid(host, { workbook });
const orders = { sheet: "orders", start: { row: 0, col: 0 }, end: { row: 39, col: 5 } };
const REGION = 1;
const REVENUE = 5;
// Sort the cells in place: one undo step, one transaction for collaborators.
grid.sortRange(orders, [
{ col: REGION, ascending: true },
{ col: REVENUE, ascending: false },
]);
// Sort the view only: the grid shows the new order, the cells keep their rows.
grid.setViewSort([{ col: REVENUE, ascending: false }]);
// The Sort panel (Data › Custom Sort; Excel's Data › Sort): sort levels, a
// header row and "Sort view only".
// When the page removes the host: grid.destroy();