Skip to content

Commit dd6dce6

Browse files
committed
fix id in update function
1 parent 17ce028 commit dd6dce6

File tree

2 files changed

+41
-22
lines changed

2 files changed

+41
-22
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sqlpage_spreadsheet",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "sqlpage spreadsheet component",
55
"source": "./src/spreadsheet_component.html",
66
"browserslist": "> 3%, last 2 versions, not dead",

src/spreadsheet.ts

Lines changed: 40 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,28 @@ const zod = import("zod");
3131

3232
const NUMBER_CELL_TYPE: typeof CellValueType.NUMBER = 2;
3333
const DEBUG = window?.location?.search?.includes("_debug_spreadsheet");
34+
35+
class CellIdMap {
36+
ids: string[] = [];
37+
static MAX_COLS = 1000000000;
38+
insert(row: number, col: number, id: string) {
39+
const idx = row * CellIdMap.MAX_COLS + col;
40+
this.ids[idx] = id;
41+
}
42+
get(row: number, col: number): string | undefined {
43+
const idx = row * CellIdMap.MAX_COLS + col;
44+
return this.ids[idx];
45+
}
46+
}
47+
3448
async function generateWorkSheet(
3549
dataArray: any[],
3650
props: Props,
37-
): Promise<Partial<IWorksheetData>> {
38-
const { cellData, rowCount, columnCount } = await buildCellData(dataArray);
51+
): Promise<{ worksheet: Partial<IWorksheetData>; cellIdMap: CellIdMap }> {
52+
const { cellData, rowCount, columnCount, cellIdMap } =
53+
await buildCellData(dataArray);
3954

40-
return {
55+
const worksheet: Partial<IWorksheetData> = {
4156
id: "sqlpage",
4257
name: props.sheet_name,
4358
defaultColumnWidth: props.column_width,
@@ -53,20 +68,25 @@ async function generateWorkSheet(
5368
columnCount,
5469
cellData,
5570
};
71+
72+
return { worksheet, cellIdMap };
5673
}
5774

5875
async function buildCellData(dataArray: any[]) {
5976
const cellData: IObjectMatrixPrimitiveType<ICellData> = {};
77+
const cellIdMap = new CellIdMap();
6078
let rowCount = 1000;
6179
let columnCount = 26;
6280
const schema = DataArraySchema(await zod);
6381

6482
for (const elem of dataArray) {
6583
const [colIdx, rowIdx, value, ...props] = schema.parse(elem);
6684
const cell: ICellData = { v: value };
67-
const style = props.length ? cellFromProps(props) : null;
68-
cell.s = style;
69-
if (style?.id) cell.custom = { id: style.id };
85+
if (props.length) {
86+
const { s, customId } = cellFromProps(props);
87+
cell.s = s;
88+
if (customId) cellIdMap.insert(rowIdx, colIdx, customId);
89+
}
7090
if (typeof value === "number") cell.t = NUMBER_CELL_TYPE;
7191
const row = cellData[rowIdx];
7292
if (row) row[colIdx] = cell;
@@ -75,7 +95,7 @@ async function buildCellData(dataArray: any[]) {
7595
columnCount = Math.max(columnCount, colIdx);
7696
}
7797

78-
return { cellData, rowCount, columnCount };
98+
return { cellData, rowCount, columnCount, cellIdMap };
7999
}
80100

81101
async function setupUniver(container: HTMLElement) {
@@ -117,7 +137,7 @@ async function handleUpdate(
117137
x: number,
118138
y: number,
119139
value: CellValue | null | undefined,
120-
custom: Record<string, unknown>,
140+
customId: string | undefined,
121141
errorModal: ReturnType<typeof setupErrorModal>,
122142
) {
123143
if (!update_link) return;
@@ -128,7 +148,7 @@ async function handleUpdate(
128148
formData.append("x", x.toString());
129149
formData.append("y", y.toString());
130150
if (value != null) formData.append("value", value.toString());
131-
if (typeof custom.id === "string") formData.append("id", custom.id);
151+
if (customId != null) formData.append("id", customId);
132152
const r = await fetch(url, { method: "POST", body: formData });
133153
let resp_html = await r.text();
134154
if (r.status !== 200 && !resp_html) resp_html = r.statusText;
@@ -141,7 +161,8 @@ async function handleUpdate(
141161
const CSS_VARS = getComputedStyle(document.documentElement);
142162

143163
function cellFromProps(props: CellProps[]) {
144-
const s: IStyleData & { id?: string } = {};
164+
let customId: string | undefined = undefined;
165+
const s: IStyleData = {};
145166
for (let i = 0; i < props.length; i++) {
146167
const n = props[i];
147168
if (n === 1) s.bl = 1;
@@ -157,7 +178,7 @@ function cellFromProps(props: CellProps[]) {
157178
else if (n === 8) {
158179
const pattern = props[++i].toString();
159180
s.n = { pattern };
160-
} else if (n === 9) s.id = props[++i].toString();
181+
} else if (n === 9) customId = props[++i].toString();
161182
else if (n === 10) s.ff = props[++i].toString();
162183
else if (n === 11) s.fs = Number(props[++i]);
163184
else if (n === 12) s.ul = { s: 1 };
@@ -174,7 +195,7 @@ function cellFromProps(props: CellProps[]) {
174195
else if (n === 20) s.tb = WrapStrategy.WRAP;
175196
else if (n === 21) s.td = TextDirection.RIGHT_TO_LEFT;
176197
}
177-
return s;
198+
return { s, customId };
178199
}
179200

180201
async function renderSpreadsheet(
@@ -186,7 +207,7 @@ async function renderSpreadsheet(
186207
if (!(modal instanceof HTMLElement)) throw new Error("modal not found");
187208
const errorModal = setupErrorModal(modal);
188209

189-
const worksheet = await generateWorkSheet(data, props);
210+
const { worksheet, cellIdMap } = await generateWorkSheet(data, props);
190211

191212
const univerAPI = await setupUniver(container);
192213

@@ -211,6 +232,7 @@ async function renderSpreadsheet(
211232
params as ISetRangeValuesMutationParams,
212233
update_link,
213234
errorModal,
235+
cellIdMap,
214236
);
215237
}
216238
});
@@ -220,6 +242,7 @@ function handleSetRangeValues(
220242
params: ISetRangeValuesMutationParams,
221243
update_link: string,
222244
errorModal: ReturnType<typeof setupErrorModal>,
245+
cellIdMap: CellIdMap,
223246
) {
224247
const { cellValue } = params;
225248
if (!cellValue) return;
@@ -233,14 +256,10 @@ function handleSetRangeValues(
233256
if (value == null && cell.p) {
234257
value = cell.p.body?.dataStream?.trimEnd();
235258
}
236-
handleUpdate(
237-
update_link,
238-
Number.parseInt(col),
239-
Number.parseInt(row),
240-
value,
241-
cell.custom || {},
242-
errorModal,
243-
);
259+
const rowIdx = Number.parseInt(row);
260+
const colIdx = Number.parseInt(col);
261+
const customId = cellIdMap.get(rowIdx, colIdx);
262+
handleUpdate(update_link, colIdx, rowIdx, value, customId, errorModal);
244263
}
245264
}
246265
}

0 commit comments

Comments
 (0)