Skip to content
Mog is in active development. The GitHub repo, SDK packages, and community channels are not yet available. Follow for launch updates

ws.pivots

WorksheetPivots

Sub-API for pivot table operations on a worksheet.

36 methods

add

Promise<PivotTableConfig>
add(config: PivotCreateConfig): Promise<PivotTableConfig>;
ParameterTypeRequired
configPivotCreateConfigrequired

Create a new pivot table on this worksheet. Accepts either: - **Simple config**: `{ name, dataSource: "Sheet1!A1:D100", rowFields: ["Region"], ... }` Fields are auto-detected from source headers, placements are generated from field arrays. - **Full config**: `{ name, sourceSheetName, sourceRange, fields, placements, filters, ... }` Direct wire format — no conversion needed. @param config - Pivot table configuration @returns The created pivot table configuration (with generated id, timestamps)

addWithSheet

Promise<{ sheetId: string; config: PivotTableConfig }>
addWithSheet(
    sheetName: string,
    config: PivotCreateConfig,
  ): Promise<{ sheetId: string; config: PivotTableConfig }>;
ParameterTypeRequired
sheetNamestringrequired
configPivotCreateConfigrequired

Atomically create a new sheet AND a pivot table on it. Both operations happen in a single transaction for undo atomicity. Accepts the same simple or full config formats as `add()`. @param sheetName - Name for the new sheet @param config - Pivot table configuration @returns The new sheet ID and the created pivot config

remove

Promise<PivotRemoveReceipt>
remove(name: string): Promise<PivotRemoveReceipt>;
ParameterTypeRequired
namestringrequired

Remove a pivot table by name. @param name - Pivot table name

rename

Promise<void>
rename(pivotId: string, newName: string): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
newNamestringrequired

Rename a pivot table. @param pivotId - Pivot table ID @param newName - New name for the pivot table

list

Promise<PivotTableInfo[]>
list(): Promise<PivotTableInfo[]>;

List all pivot tables on this worksheet. @returns Array of pivot table summary information

get

Promise<PivotTableHandle | null>
get(name: string): Promise<PivotTableHandle | null>;
ParameterTypeRequired
namestringrequired

Get a pivot table handle by name. @param name - Pivot table name @returns A handle for the pivot table, or null if not found

addField

Promise<void>
addField(
    pivotId: string,
    fieldId: string,
    area: PivotFieldArea,
    options?: {
      position?: number;
      aggregateFunction?: AggregateFunction;
      sortOrder?: SortOrder;
      displayName?: string;
      showValuesAs?: ShowValuesAsConfig;
    },
  ): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
areaPivotFieldArearequired
options{ position?: number; aggregateFunction?: AggregateFunction; sortOrder?: SortOrder; displayName?: string; showValuesAs?: ShowValuesAsConfig; }optional

Add a field to a pivot table area. @param pivotId - Pivot table ID @param fieldId - Field ID to add @param area - Target area (row, column, value, or filter) @param options - Optional configuration (position, aggregateFunction, sortOrder, displayName)

removeField

Promise<void>
removeField(pivotId: string, fieldId: string, area: PivotFieldArea): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
areaPivotFieldArearequired

Remove a field from a pivot table area. @param pivotId - Pivot table ID @param fieldId - Field ID to remove @param area - Area to remove the field from

moveField

Promise<void>
moveField(
    pivotId: string,
    fieldId: string,
    fromArea: PivotFieldArea,
    toArea: PivotFieldArea,
    toPosition: number,
  ): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
fromAreaPivotFieldArearequired
toAreaPivotFieldArearequired
toPositionnumberrequired

Move a field to a different area or position. @param pivotId - Pivot table ID @param fieldId - Field ID to move @param fromArea - Source area @param toArea - Target area @param toPosition - Target position within the area

setAggregateFunction

Promise<void>
setAggregateFunction(
    pivotId: string,
    fieldId: string,
    aggregateFunction: AggregateFunction,
  ): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
aggregateFunctionAggregateFunctionrequired

Set the aggregate function for a value field. @param pivotId - Pivot table ID @param fieldId - Field ID (must be in the 'value' area) @param aggregateFunction - New aggregation function

setShowValuesAs

Promise<void>
setShowValuesAs(
    pivotId: string,
    fieldId: string,
    showValuesAs: ShowValuesAsConfig | null,
  ): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
showValuesAsShowValuesAsConfig | nullrequired

Set the "Show Values As" calculation for a value field. Only applies to fields in the 'value' area. Pass null to clear. @param pivotId - Pivot table ID @param fieldId - Field ID (must be in the 'value' area) @param showValuesAs - ShowValuesAs configuration, or null to clear

setSortOrder

Promise<void>
setSortOrder(pivotId: string, fieldId: string, sortOrder: SortOrder): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
sortOrderSortOrderrequired

Set the sort order for a row or column field. @param pivotId - Pivot table ID @param fieldId - Field ID (must be in 'row' or 'column' area) @param sortOrder - Sort order ('asc', 'desc', or 'none')

setFilter

Promise<void>
setFilter(pivotId: string, fieldId: string, filter: Omit<PivotFilter, 'fieldId'>): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
filterOmit<PivotFilter, 'fieldId'>required

Set (add or update) a filter on a field. @param pivotId - Pivot table ID @param fieldId - Field ID to filter @param filter - Filter configuration (without fieldId)

removeFilter

Promise<void>
removeFilter(pivotId: string, fieldId: string): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired

Remove a filter from a field. @param pivotId - Pivot table ID @param fieldId - Field ID whose filter should be removed

resetField

Promise<void>
resetField(pivotId: string, fieldId: string): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired

Reset a field placement to defaults (clear aggregateFunction, sortOrder, displayName, showValuesAs, etc.) and remove any associated filter. @param pivotId - Pivot table ID @param fieldId - Field ID to reset

setLayout

Promise<void>
setLayout(pivotId: string, layout: Partial<PivotTableLayout>): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
layoutPartial<PivotTableLayout>required

Set layout options for a pivot table. @param pivotId - Pivot table ID @param layout - Partial layout configuration to merge with existing

setStyle

Promise<void>
setStyle(pivotId: string, style: Partial<PivotTableStyle>): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
stylePartial<PivotTableStyle>required

Set style options for a pivot table. @param pivotId - Pivot table ID @param style - Partial style configuration to merge with existing

detectFields

Promise<any[]>
detectFields(
    sourceSheetId: string,
    range: { startRow: number; startCol: number; endRow: number; endCol: number },
  ): Promise<any[]>;
ParameterTypeRequired
sourceSheetIdstringrequired
range{ startRow: number; startCol: number; endRow: number; endCol: number }required

Detect fields from source data for pivot table creation. @param sourceSheetId - Sheet ID containing the source data @param range - Source data range @returns Array of detected pivot fields

compute

Promise<any>
compute(pivotId: string, forceRefresh?: boolean): Promise<any>;
ParameterTypeRequired
pivotIdstringrequired
forceRefreshbooleanoptional

Compute a pivot table result (uses cache if available). @param pivotId - Pivot table ID @param forceRefresh - Force recomputation ignoring cache @returns Computed pivot table result

queryPivot

Promise<PivotQueryResult | null>
queryPivot(
    pivotName: string,
    filters?: Record<string, CellValue | CellValue[]>,
  ): Promise<PivotQueryResult | null>;
ParameterTypeRequired
pivotNamestringrequired
filtersRecord<string, CellValue | CellValue[]>optional

Query a pivot table by name, returning flat records optionally filtered by dimension values. Eliminates the need to manually traverse hierarchical PivotTableResult trees. @param pivotName - Pivot table name @param filters - Optional dimension filters: field name → value or array of values to include @returns Flat query result, or null if pivot not found or not computable

refresh

Promise<PivotRefreshReceipt>
refresh(pivotId: string): Promise<PivotRefreshReceipt>;
ParameterTypeRequired
pivotIdstringrequired

Refresh a pivot table (recompute without cache). @param pivotId - Pivot table ID

refreshAll

Promise<void>
refreshAll(): Promise<void>;

Refresh all pivot tables on this worksheet.

getDrillDownData

Promise<any[][]>
getDrillDownData(pivotId: string, rowKey: string, columnKey: string): Promise<any[][]>;
ParameterTypeRequired
pivotIdstringrequired
rowKeystringrequired
columnKeystringrequired

Get drill-down data for a pivot table cell. @param pivotId - Pivot table ID @param rowKey - Row key from the pivot result @param columnKey - Column key from the pivot result @returns Source data rows that contribute to this cell

addCalculatedField

Promise<void>
addCalculatedField(pivotId: string, field: CalculatedField): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldCalculatedFieldrequired

Add a calculated field to a pivot table. @param pivotId - Pivot table ID @param field - Calculated field definition (fieldId, name, formula)

removeCalculatedField

Promise<void>
removeCalculatedField(pivotId: string, fieldId: string): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired

Remove a calculated field from a pivot table. @param pivotId - Pivot table ID @param fieldId - Calculated field ID to remove

updateCalculatedField

Promise<void>
updateCalculatedField(
    pivotId: string,
    fieldId: string,
    updates: Partial<Pick<CalculatedField, 'name' | 'formula'>>,
  ): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
updatesPartial<Pick<CalculatedField, 'name' | 'formula'>>required

Update a calculated field on a pivot table. @param pivotId - Pivot table ID @param fieldId - Calculated field ID to update @param updates - Partial updates (name and/or formula)

getRange

Promise<CellRange | null>
getRange(pivotId: string): Promise<CellRange | null>;
ParameterTypeRequired
pivotIdstringrequired

Get the full range occupied by the rendered pivot table. @param pivotId - Pivot table ID @returns CellRange covering the entire pivot table, or null if not computed

getDataBodyRange

Promise<CellRange | null>
getDataBodyRange(pivotId: string): Promise<CellRange | null>;
ParameterTypeRequired
pivotIdstringrequired

Get the range of the data body (values only, excluding headers and totals row labels). @param pivotId - Pivot table ID @returns CellRange covering the data body, or null if not computed

getColumnLabelRange

Promise<CellRange | null>
getColumnLabelRange(pivotId: string): Promise<CellRange | null>;
ParameterTypeRequired
pivotIdstringrequired

Get the range of column label headers. @param pivotId - Pivot table ID @returns CellRange covering the column headers, or null if not computed

getRowLabelRange

Promise<CellRange | null>
getRowLabelRange(pivotId: string): Promise<CellRange | null>;
ParameterTypeRequired
pivotIdstringrequired

Get the range of row label headers. @param pivotId - Pivot table ID @returns CellRange covering the row headers, or null if not computed

getFilterAxisRange

Promise<CellRange | null>
getFilterAxisRange(pivotId: string): Promise<CellRange | null>;
ParameterTypeRequired
pivotIdstringrequired

Get the range of the filter area (page fields above the pivot table). @param pivotId - Pivot table ID @returns CellRange covering filter dropdowns, or null if no filter fields

getAllPivotItems

Promise<PivotFieldItems[]>
getAllPivotItems(pivotId: string): Promise<PivotFieldItems[]>;
ParameterTypeRequired
pivotIdstringrequired

Get pivot items for all placed fields (excluding value fields). Each item includes its display value, expansion state, and visibility. @param pivotId - Pivot table ID @returns Items grouped by field

setPivotItemVisibility

Promise<void>
setPivotItemVisibility(
    pivotId: string,
    fieldId: string,
    visibleItems: Record<string, boolean>,
  ): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
fieldIdstringrequired
visibleItemsRecord<string, boolean>required

Set visibility of specific items in a field. Updates the field's filter include/exclude list. @param pivotId - Pivot table ID @param fieldId - Field ID @param visibleItems - Map of item value (as string) to visibility boolean

toggleExpanded

Promise<boolean>
toggleExpanded(pivotId: string, headerKey: string, isRow: boolean): Promise<boolean>;
ParameterTypeRequired
pivotIdstringrequired
headerKeystringrequired
isRowbooleanrequired

Toggle expansion state for a header. @param pivotId - Pivot table ID @param headerKey - Header key to toggle @param isRow - Whether this is a row header (true) or column header (false) @returns The new expansion state (true = expanded, false = collapsed)

setAllExpanded

Promise<void>
setAllExpanded(pivotId: string, expanded: boolean): Promise<void>;
ParameterTypeRequired
pivotIdstringrequired
expandedbooleanrequired

Set expansion state for all headers. @param pivotId - Pivot table ID @param expanded - Whether all headers should be expanded (true) or collapsed (false)

getExpansionState

Promise<PivotExpansionState>
getExpansionState(pivotId: string): Promise<PivotExpansionState>;
ParameterTypeRequired
pivotIdstringrequired

Get the current expansion state for a pivot table. @param pivotId - Pivot table ID @returns Expansion state with expandedRows and expandedColumns maps