Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 11 additions & 14 deletions packages/o-spreadsheet-engine/src/helpers/cells/cell_evaluation.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { isEvaluationError, toString } from "../../functions/helpers";
import {
BooleanCell,
Cell,
CellValue,
CellValueType,
EmptyCell,
Expand Down Expand Up @@ -35,7 +34,7 @@ export function evaluateLiteral(
? literalCell.content
: literalCell.parsedValue;
const functionResult = { value, format: localeFormat.format, origin: position };
return createEvaluatedCell(functionResult, localeFormat.locale);
return createEvaluatedCell(functionResult, localeFormat);
}

export function parseLiteral(content: string, locale: Locale): CellValue {
Expand Down Expand Up @@ -66,28 +65,27 @@ export function parseLiteral(content: string, locale: Locale): CellValue {

export function createEvaluatedCell(
functionResult: FunctionResultObject,
locale: Locale = DEFAULT_LOCALE,
cell?: Cell,
locale: LocaleFormat = { format: undefined, locale: DEFAULT_LOCALE },
origin?: CellPosition
): EvaluatedCell {
const link = detectLink(functionResult.value);
if (!link) {
const evaluateCell = _createEvaluatedCell(functionResult, locale, cell);
const evaluateCell = _createEvaluatedCell(functionResult, locale);
return addOrigin(evaluateCell, functionResult.origin ?? origin);
}
const value = parseLiteral(link.label, locale);
const format =
const value = parseLiteral(link.label, locale.locale);
const format2 =
functionResult.format ||
(typeof value === "number"
? detectDateFormat(link.label, locale) || detectNumberFormat(link.label)
? detectDateFormat(link.label, locale.locale) || detectNumberFormat(link.label)
: undefined);
const linkPayload = {
value,
format,
format2,
};
return addOrigin(
{
..._createEvaluatedCell(linkPayload, locale, cell),
..._createEvaluatedCell(linkPayload, locale),
link,
},
functionResult.origin ?? origin
Expand All @@ -96,13 +94,12 @@ export function createEvaluatedCell(

function _createEvaluatedCell(
functionResult: FunctionResultObject,
locale: Locale,
cell?: Cell
localeFormat: LocaleFormat
): EvaluatedCell {
let { value, format, message } = functionResult;
format = cell?.format || format;
format = localeFormat.format || format;

const formattedValue = formatValue(value, { format, locale });
const formattedValue = formatValue(value, { format, locale: localeFormat.locale });
if (isEvaluationError(value)) {
return errorCell(value, message);
}
Expand Down
42 changes: 3 additions & 39 deletions packages/o-spreadsheet-engine/src/plugins/core/cell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import { compile, compileTokens } from "../../formulas/compiler";
import { Token } from "../../formulas/tokenizer";
import { isEvaluationError, toString } from "../../functions/helpers";
import { PositionMap } from "../../helpers/cells/position_map";
import {
getItemId,
groupItemIdsByZones,
iterateItemIdsPositions,
} from "../../helpers/data_normalization";
import { iterateItemIdsPositions } from "../../helpers/data_normalization";
import { concat, deepEquals, range, replaceNewLines } from "../../helpers/misc";

import { toCartesian, toXC } from "../../helpers/coordinates";
Expand All @@ -23,15 +19,10 @@ import {
PositionDependentCommand,
UpdateCellCommand,
} from "../../types/commands";
import { CellPosition, HeaderIndex, UID } from "../../types/misc";
import { HeaderIndex, UID } from "../../types/misc";

import { parseLiteral } from "../../helpers/cells/cell_evaluation";
import {
detectDateFormat,
detectNumberFormat,
isExcelCompatible,
isTextFormat,
} from "../../helpers/format/format";
import { detectDateFormat, detectNumberFormat, isTextFormat } from "../../helpers/format/format";
import { Format } from "../../types/format";
import {
AdaptSheetName,
Expand Down Expand Up @@ -294,30 +285,20 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {
}

export(data: WorkbookData) {
const formats: { [formatId: number]: string } = {};

for (const _sheet of data.sheets) {
const positionsByFormat: Record<number, CellPosition[]> = [];
const cells: { [key: string]: string } = {};
const positions = Object.keys(this.cells[_sheet.id] || {})
.map((cellId) => this.getters.getCellPosition(cellId))
.sort((a, b) => (a.col === b.col ? a.row - b.row : a.col - b.col));
for (const position of positions) {
const cell = this.getters.getCell(position)!;
const xc = toXC(position.col, position.row);
if (cell.format) {
const formatId = getItemId<Format>(cell.format, formats);
positionsByFormat[formatId] ??= [];
positionsByFormat[formatId].push(position);
}
if (cell.content) {
cells[xc] = cell.content;
}
}
_sheet.formats = groupItemIdsByZones(positionsByFormat);
_sheet.cells = cells;
}
data.formats = formats;
}

importCell(sheetId: UID, content?: string, format?: Format): Cell {
Expand All @@ -327,23 +308,6 @@ export class CellPlugin extends CorePlugin<CoreState> implements CoreState {

exportForExcel(data: ExcelWorkbookData) {
this.export(data);
const incompatibleIds: number[] = [];
for (const formatId in data.formats || []) {
if (!isExcelCompatible(data.formats[formatId])) {
incompatibleIds.push(Number(formatId));
delete data.formats[formatId];
}
}
if (incompatibleIds.length) {
for (const sheet of data.sheets) {
for (const zoneXc in sheet.formats) {
const formatId = sheet.formats[zoneXc];
if (formatId && incompatibleIds.includes(formatId)) {
delete sheet.formats[zoneXc];
}
}
}
}
}

// ---------------------------------------------------------------------------
Expand Down
8 changes: 6 additions & 2 deletions packages/o-spreadsheet-engine/src/plugins/core/settings.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { getDateTimeFormat, isValidLocale } from "../../helpers/locale";
import { CommandResult, CoreCommand } from "../../types/commands";
import { Format } from "../../types/format";
import { Format, LocaleFormat } from "../../types/format";
import { DEFAULT_LOCALE, Locale } from "../../types/locale";
import { WorkbookData } from "../../types/workbook_data";
import { CorePlugin } from "../core_plugin";

export class SettingsPlugin extends CorePlugin {
static getters = ["getLocale"] as const;
static getters = ["getLocale", "getLocaleFormat"] as const;
private locale: Locale = DEFAULT_LOCALE;

allowDispatch(cmd: CoreCommand) {
Expand All @@ -32,6 +32,10 @@ export class SettingsPlugin extends CorePlugin {
return this.locale;
}

getLocaleFormat(format?: Format): LocaleFormat {
return { locale: this.locale, format };
}

private changeCellsDateFormatWithLocale(oldLocale: Locale, newLocale: Locale) {
for (const sheetId of this.getters.getSheetIds()) {
for (const [cellId, cell] of Object.entries(this.getters.getCells(sheetId))) {
Expand Down
Loading