Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import type { WorkbookSheetMetadata } from '@macro-inc/spreadsheet/workbook-metadata';
import type { SpreadsheetCell, SpreadsheetCells } from './spreadsheet-document';
export type CalculationCells = Record<
string,
Pick<SpreadsheetCell, 'value' | 'format' | 'decimals' | 'numberFormat'>
>;
/** The calculation-relevant part of a cell, or undefined for a blank one.
* Formatting an otherwise empty cell must not trigger calculation. Keep
* number formats, though: a blank source cell can display an array spill. */
export function calculationInput(
cell: SpreadsheetCell | undefined
): CalculationCells[string] | undefined {
if (!cell) return;
const format = cell.format ?? 'general';
const decimals = cell.decimals ?? -1;
const numberFormat = cell.numberFormat;
if (
cell.value === '' &&
format === 'general' &&
decimals === -1 &&
!numberFormat
)
return;
return {
value: cell.value,
format,
decimals,
...(numberFormat && { numberFormat }),
};
}
/** Keep appearance edits out of the calculation dependency graph. */
export function calculationInputs(
cells: SpreadsheetCells,
previous: CalculationCells
): CalculationCells {
const next: CalculationCells = {};
let unchanged = true;
for (const [address, cell] of Object.entries(cells)) {
const format = cell.format ?? 'general';
const decimals = cell.decimals ?? -1;
// Formatting an otherwise empty cell must not trigger calculation. Keep
// number formats, though: a blank source cell can display an array spill.
const numberFormat = cell.numberFormat;
if (
cell.value === '' &&
format === 'general' &&
decimals === -1 &&
!numberFormat
)
continue;
next[address] = {
value: cell.value,
format,
decimals,
...(numberFormat && { numberFormat }),
};
const prior = previous[address];
if (
!prior ||
prior.value !== cell.value ||
prior.format !== format ||
prior.decimals !== decimals ||
prior.numberFormat !== numberFormat
) {
unchanged = false;
}
}
return unchanged && Object.keys(next).length === Object.keys(previous).length
? previous
: next;
}
export type CalculationWorkbookInput = {
id: string;
name: string;
cells: SpreadsheetCells;
rowCount: number;
metadata?: WorkbookSheetMetadata;
};
/** Preserve each sheet's calculation identity across formatting and layout edits. */
export function workbookCalculationInputs(
sheets: CalculationWorkbookInput[],
previous: CalculationWorkbookInput[]
): CalculationWorkbookInput[] {
const previousById = new Map(previous.map((sheet) => [sheet.id, sheet]));
const next = sheets.map((sheet) => {
const prior = previousById.get(sheet.id);
const cells = calculationInputs(sheet.cells, prior?.cells ?? {});
return prior &&
prior.name === sheet.name &&
prior.rowCount === sheet.rowCount &&
JSON.stringify(prior.metadata?.definedNames) ===
JSON.stringify(sheet.metadata?.definedNames) &&
JSON.stringify(prior.metadata?.hiddenRows) ===
JSON.stringify(sheet.metadata?.hiddenRows) &&
JSON.stringify(prior.metadata?.conditionalFormats) ===
JSON.stringify(sheet.metadata?.conditionalFormats) &&
JSON.stringify(prior.metadata?.tables) ===
JSON.stringify(sheet.metadata?.tables) &&
cells === prior.cells
? prior
: { ...sheet, cells };
});
return next.length === previous.length &&
next.every((sheet, index) => sheet === previous[index])
? previous
: next;
}