mirror of
https://github.com/gristlabs/grist-core.git
synced 2026-03-02 04:09:24 +00:00
(core) Suggest correct table when converting to RefList
Summary: RecordSets now have new encoding and rendering analogous to Records: `['r', 'Table', [1, 2, 3]]` and `Table[[1, 2, 3]]`. Test Plan: Added to nbrowser/TypeChange.ts. Reviewers: dsagal Reviewed By: dsagal Differential Revision: https://phab.getgrist.com/D2987
This commit is contained in:
@@ -43,7 +43,7 @@ export function addColTypeSuffix(type: string, column: ColumnRec, docModel: DocM
|
||||
|
||||
/**
|
||||
* Looks through the data of the given column to find the first value of the form
|
||||
* [R, <tableId>, <rowId>] (a Reference value returned from a formula), and returns the tableId
|
||||
* [R|r, <tableId>, <rowId>] (a Reference(List) value returned from a formula), and returns the tableId
|
||||
* from that.
|
||||
*/
|
||||
function getRefTableIdFromData(docModel: DocModel, column: ColumnRec): string|null {
|
||||
@@ -51,12 +51,21 @@ function getRefTableIdFromData(docModel: DocModel, column: ColumnRec): string|nu
|
||||
const columnData = tableData && tableData.getColValues(column.colId());
|
||||
if (columnData) {
|
||||
for (const value of columnData) {
|
||||
if (gristTypes.isObject(value) && value[0] === 'R') {
|
||||
if (gristTypes.isReferencing(value)) {
|
||||
return value[1];
|
||||
} else if (gristTypes.isList(value)) {
|
||||
const item = value[1];
|
||||
if (gristTypes.isReference(item)) {
|
||||
return item[1];
|
||||
}
|
||||
} else if (typeof value === 'string') {
|
||||
// If it looks like a formatted Ref value (e.g. "Table1[123]"), and the tableId is valid,
|
||||
// If it looks like a formatted Ref(List) value, e.g:
|
||||
// - Table1[123]
|
||||
// - [Table1[1], Table1[2], Table1[3]]
|
||||
// - Table1[[1, 2, 3]]
|
||||
// and the tableId is valid,
|
||||
// use it. (This helps if a Ref-returning formula column got converted to Text first.)
|
||||
const match = value.match(/^(\w+)\[\d+\]/);
|
||||
const match = value.match(/^\[?(\w+)\[/);
|
||||
if (match && docModel.docData.getTable(match[1])) {
|
||||
return match[1];
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ export const enum GristObjCode {
|
||||
Skip = 'S',
|
||||
Censored = 'C',
|
||||
Reference = 'R',
|
||||
ReferenceList = 'r',
|
||||
Exception = 'E',
|
||||
Pending = 'P',
|
||||
Unmarshallable = 'U',
|
||||
@@ -142,10 +143,34 @@ export function isCensored(value: CellValue): value is [GristObjCode.Censored] {
|
||||
/**
|
||||
* Returns whether a value (as received in a DocAction) represents a list.
|
||||
*/
|
||||
export function isList(value: CellValue): value is [GristObjCode.List, ...unknown[]] {
|
||||
export function isList(value: CellValue): value is [GristObjCode.List, ...CellValue[]] {
|
||||
return Array.isArray(value) && value[0] === GristObjCode.List;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a value (as received in a DocAction) represents a reference to a record.
|
||||
*/
|
||||
export function isReference(value: CellValue): value is [GristObjCode.Reference, string, number] {
|
||||
return Array.isArray(value) && value[0] === GristObjCode.Reference;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a value (as received in a DocAction) represents a reference list (RecordSet).
|
||||
*/
|
||||
export function isReferenceList(value: CellValue): value is [GristObjCode.ReferenceList, string, number[]] {
|
||||
return Array.isArray(value) && value[0] === GristObjCode.ReferenceList;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a value (as received in a DocAction) represents a reference or reference list.
|
||||
*/
|
||||
export function isReferencing(value: CellValue):
|
||||
value is [GristObjCode.ReferenceList|GristObjCode.Reference, string, number[]|number]
|
||||
{
|
||||
return Array.isArray(value) &&
|
||||
(value[0] === GristObjCode.ReferenceList || value[0] === GristObjCode.Reference);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether a value (as received in a DocAction) represents a list or is null,
|
||||
* which is a valid value for list types in grist.
|
||||
|
||||
@@ -50,6 +50,18 @@ export class Reference {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A ReferenceList represents a reference to a number of rows in a table. It is simply a pair of a string tableId
|
||||
* and a numeric array rowIds.
|
||||
*/
|
||||
export class ReferenceList {
|
||||
constructor(public tableId: string, public rowIds: number[]) {}
|
||||
|
||||
public toString(): string {
|
||||
return `${this.tableId}[[${this.rowIds}]]`;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A RaisedException represents a formula error. It includes the exception name, message, and
|
||||
* optional details.
|
||||
@@ -140,6 +152,8 @@ export function encodeObject(value: unknown): CellValue {
|
||||
return null;
|
||||
} else if (value instanceof Reference) {
|
||||
return ['R', value.tableId, value.rowId];
|
||||
} else if (value instanceof ReferenceList) {
|
||||
return ['r', value.tableId, value.rowIds];
|
||||
} else if (value instanceof Date) {
|
||||
const timestamp = value.valueOf() / 1000;
|
||||
if ('timezone' in value) {
|
||||
@@ -185,6 +199,7 @@ export function decodeObject(value: CellValue): unknown {
|
||||
case 'L': return (args as CellValue[]).map(decodeObject);
|
||||
case 'O': return mapValues(args[0] as {[key: string]: CellValue}, decodeObject, {sort: true});
|
||||
case 'P': return new PendingValue();
|
||||
case 'r': return new ReferenceList(String(args[0]), args[1]);
|
||||
case 'R': return new Reference(String(args[0]), args[1]);
|
||||
case 'S': return new SkipValue();
|
||||
case 'C': return new CensoredValue();
|
||||
|
||||
Reference in New Issue
Block a user