@@ -31,13 +31,28 @@ const zod = import("zod");
31
31
32
32
const NUMBER_CELL_TYPE : typeof CellValueType . NUMBER = 2 ;
33
33
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
+
34
48
async function generateWorkSheet (
35
49
dataArray : any [ ] ,
36
50
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 ) ;
39
54
40
- return {
55
+ const worksheet : Partial < IWorksheetData > = {
41
56
id : "sqlpage" ,
42
57
name : props . sheet_name ,
43
58
defaultColumnWidth : props . column_width ,
@@ -53,20 +68,25 @@ async function generateWorkSheet(
53
68
columnCount,
54
69
cellData,
55
70
} ;
71
+
72
+ return { worksheet, cellIdMap } ;
56
73
}
57
74
58
75
async function buildCellData ( dataArray : any [ ] ) {
59
76
const cellData : IObjectMatrixPrimitiveType < ICellData > = { } ;
77
+ const cellIdMap = new CellIdMap ( ) ;
60
78
let rowCount = 1000 ;
61
79
let columnCount = 26 ;
62
80
const schema = DataArraySchema ( await zod ) ;
63
81
64
82
for ( const elem of dataArray ) {
65
83
const [ colIdx , rowIdx , value , ...props ] = schema . parse ( elem ) ;
66
84
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
+ }
70
90
if ( typeof value === "number" ) cell . t = NUMBER_CELL_TYPE ;
71
91
const row = cellData [ rowIdx ] ;
72
92
if ( row ) row [ colIdx ] = cell ;
@@ -75,7 +95,7 @@ async function buildCellData(dataArray: any[]) {
75
95
columnCount = Math . max ( columnCount , colIdx ) ;
76
96
}
77
97
78
- return { cellData, rowCount, columnCount } ;
98
+ return { cellData, rowCount, columnCount, cellIdMap } ;
79
99
}
80
100
81
101
async function setupUniver ( container : HTMLElement ) {
@@ -117,7 +137,7 @@ async function handleUpdate(
117
137
x : number ,
118
138
y : number ,
119
139
value : CellValue | null | undefined ,
120
- custom : Record < string , unknown > ,
140
+ customId : string | undefined ,
121
141
errorModal : ReturnType < typeof setupErrorModal > ,
122
142
) {
123
143
if ( ! update_link ) return ;
@@ -128,7 +148,7 @@ async function handleUpdate(
128
148
formData . append ( "x" , x . toString ( ) ) ;
129
149
formData . append ( "y" , y . toString ( ) ) ;
130
150
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 ) ;
132
152
const r = await fetch ( url , { method : "POST" , body : formData } ) ;
133
153
let resp_html = await r . text ( ) ;
134
154
if ( r . status !== 200 && ! resp_html ) resp_html = r . statusText ;
@@ -141,7 +161,8 @@ async function handleUpdate(
141
161
const CSS_VARS = getComputedStyle ( document . documentElement ) ;
142
162
143
163
function cellFromProps ( props : CellProps [ ] ) {
144
- const s : IStyleData & { id ?: string } = { } ;
164
+ let customId : string | undefined = undefined ;
165
+ const s : IStyleData = { } ;
145
166
for ( let i = 0 ; i < props . length ; i ++ ) {
146
167
const n = props [ i ] ;
147
168
if ( n === 1 ) s . bl = 1 ;
@@ -157,7 +178,7 @@ function cellFromProps(props: CellProps[]) {
157
178
else if ( n === 8 ) {
158
179
const pattern = props [ ++ i ] . toString ( ) ;
159
180
s . n = { pattern } ;
160
- } else if ( n === 9 ) s . id = props [ ++ i ] . toString ( ) ;
181
+ } else if ( n === 9 ) customId = props [ ++ i ] . toString ( ) ;
161
182
else if ( n === 10 ) s . ff = props [ ++ i ] . toString ( ) ;
162
183
else if ( n === 11 ) s . fs = Number ( props [ ++ i ] ) ;
163
184
else if ( n === 12 ) s . ul = { s : 1 } ;
@@ -174,7 +195,7 @@ function cellFromProps(props: CellProps[]) {
174
195
else if ( n === 20 ) s . tb = WrapStrategy . WRAP ;
175
196
else if ( n === 21 ) s . td = TextDirection . RIGHT_TO_LEFT ;
176
197
}
177
- return s ;
198
+ return { s , customId } ;
178
199
}
179
200
180
201
async function renderSpreadsheet (
@@ -186,7 +207,7 @@ async function renderSpreadsheet(
186
207
if ( ! ( modal instanceof HTMLElement ) ) throw new Error ( "modal not found" ) ;
187
208
const errorModal = setupErrorModal ( modal ) ;
188
209
189
- const worksheet = await generateWorkSheet ( data , props ) ;
210
+ const { worksheet, cellIdMap } = await generateWorkSheet ( data , props ) ;
190
211
191
212
const univerAPI = await setupUniver ( container ) ;
192
213
@@ -211,6 +232,7 @@ async function renderSpreadsheet(
211
232
params as ISetRangeValuesMutationParams ,
212
233
update_link ,
213
234
errorModal ,
235
+ cellIdMap ,
214
236
) ;
215
237
}
216
238
} ) ;
@@ -220,6 +242,7 @@ function handleSetRangeValues(
220
242
params : ISetRangeValuesMutationParams ,
221
243
update_link : string ,
222
244
errorModal : ReturnType < typeof setupErrorModal > ,
245
+ cellIdMap : CellIdMap ,
223
246
) {
224
247
const { cellValue } = params ;
225
248
if ( ! cellValue ) return ;
@@ -233,14 +256,10 @@ function handleSetRangeValues(
233
256
if ( value == null && cell . p ) {
234
257
value = cell . p . body ?. dataStream ?. trimEnd ( ) ;
235
258
}
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 ) ;
244
263
}
245
264
}
246
265
}
0 commit comments