TX Sheet Control

AnalyticsCalculation Interface

Interface

A bounded, deterministic arithmetic or ordered-window calculation on grouped query results. Window operations follow query.sort order. Division by zero and missing numeric operands produce null.

Package: @txtextcontrol/tx-sheet-control-server. This is a settings/result contract, not a runtime constructor.

Members

PropertyDescription
aliasConfigures alias on AnalyticsCalculation; apply the complete settings object through its owning dashboard.
operationConfigures operation on AnalyticsCalculation; apply the complete settings object through its owning dashboard.
leftConfigures left on AnalyticsCalculation; apply the complete settings object through its owning dashboard.
rightConfigures right on AnalyticsCalculation; apply the complete settings object through its owning dashboard.
windowMoving-average trailing window length, 1..1000; defaults to 3.

Examples

Configure a complete sales dashboard using this contract. Pass the workbook from your existing editor or server control.

async function configureSalesDashboard(book) {
    const sheet = book.sheets.at(0);
    sheet.range('A1:G5').setValues([
        ['Region', 'Country', 'Customer', 'Revenue', 'Profit', 'Date', 'Units'],
        ['Europe', 'Germany', 'Studio', 42000, 12000, '2026-01-15', 12],
        ['Europe', 'France', 'Labs', 38000, 9000, '2026-02-15', 15],
        ['Americas', 'Canada', 'Partners', 31000, 8000, '2026-01-15', 9],
        ['Americas', 'United States', 'Design', 49000, 15000, '2026-02-15', 18]
    ]);
    book.analytics.setSource({ id: 'sales', name: 'Sales', sheetId: sheet.id, range: 'A1:G5', hasHeaderRow: true });
    const dashboard = book.dashboards.add('Sales Performance');
    dashboard.update({ sections: [{ id: 'summary', title: 'Executive Summary' }] });
    dashboard.addWidget({
        id: 'revenue', type: 'column', title: 'Regional Revenue',
        placement: { x: 0, y: 0, width: 8, height: 6 },
        query: { sourceId: 'sales', dimensions: ['Region'], measures: [
            { field: 'Revenue', aggregation: 'SUM', alias: 'Revenue' },
            { field: 'Profit', aggregation: 'SUM', alias: 'Profit' }
        ] }
    });
    const settings = {
        "alias": "Margin",
        "operation": "divide",
        "left": "Profit",
        "right": "Revenue",
        "window": 3
    };
    dashboard.updateWidget('revenue', { query: { ...dashboard.snapshot.widgets[0].query, calculations: [settings] }, chart: { yField: 'Margin' } });
    const { writeFile: saveDashboard } = await import('node:fs/promises');
    await saveDashboard('sales-dashboard.svg', await dashboard.renderSvg());
}