Skip to contentSheetwrite

Data

Saved queries that refresh into tables

Open a saved query in Queries & Connections, change one of its steps, and refresh: the output tables on the sheet follow. New orders reach them only through a refresh.

Package @sheetwrite/queries

Orders in the feed
2,000
Saved queries
3
Try it
  1. Opens it in Queries & Connections, or press Edit there
  2. Remove the Filter rows step, then Save…
  3. Changed cells light up on the sheet
  4. … orders in the feed

What a refresh does

  • The session loads sources only through your fetchSource callback, so your app applies its own credentials and network rules.
  • Undo and redo restore the written values without another source load: watch Source loads stay the same.
  • Large orders keeps orders of $1,500 or more. A refresh that would grow a table over other cells writes nothing.
  • XLSX export writes the saved queries as Power Query parts for Excel, and import reads them back, once the Power Query codec is loaded: the queries add-on loads it, or call loadPowerQueryCodec(). See Power Query in XLSX.
What a refresh doesA refresh loads the source only through your fetchSource callback, with your credentials and network rules, runs the saved steps, such as Large orders keeping orders of $1,500 or more, and writes the table. Undo and redo restore the written values without another source load. A refresh that would grow a table over other cells writes nothing.Your fetchSourceYour credentials and network rulesSourceThe CSV feedStepsLarge orders: keep $1,500 or moreTableWritten on the sheetBlockedA refresh that would grow overother cells writes nothing

Undo and redo restore the written values with no new source load.

The code behind it

queries.ts
import "@sheetwrite/core/styles.css";
import { createGrid, initSheetwrite, type Workbook } from "@sheetwrite/core";
import { queriesAddon, querySessionOf } from "@sheetwrite/queries";
// A workbook with an empty "report" sheet for the query's output table
declare const workbook: Workbook;
// The only path to source data: the host loads it, with its own credentials.
addons: [
fetchSource: async (request, { signal }) => {
const response = await fetch(`/sources/${request.sourceId}`, { signal });
return { kind: "bytes", bytes: new Uint8Array(await response.arrayBuffer()) };
},
}),
],
});
// 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 session = await querySessionOf(grid);
// The region list, from JSON such as [{ "code": "EU", "regionName": "EMEA" }].
// It has no output table: it only feeds the query below.
id: "regions",
name: "Regions",
source: {
kind: "json",
sourceId: "regions",
{ id: "code", name: "code", type: "text" },
{ id: "regionName", name: "regionName", type: "text" },
],
},
steps: [],
});
// Orders from a CSV feed: keep shipped ones, add each region's name from the
// region list, and total them by region name.
id: "region-totals",
name: "Region totals",
source: {
kind: "csv",
sourceId: "orders-feed",
{ id: "region", name: "Region", type: "text" },
{ id: "status", name: "Status", type: "text" },
{ id: "amount", name: "Amount", type: "number" },
],
},
steps: [
{ kind: "filter", predicate: { kind: "compare", column: "status", op: "eq", value: "Shipped" } },
{ kind: "join", input: "regions", keys: [["region", "code"]], join: "left" },
{
kind: "group",
keys: ["regionName"],
{ id: "revenue", name: "Revenue", op: "sum", column: "amount" },
{ id: "orders", name: "Orders", op: "countRows" },
],
},
{ kind: "rename", columns: [["regionName", "Region"]] },
],
output: {
table: "region-totals",
tableName: "RegionTotals",
anchor: { sheet: "report", row: 0, col: 0 },
},
});
// Every output table in one transaction, or none if a query fails.
// When the page removes the host: session.dispose(); grid.destroy();
Show all 79 linesShow the first lines