* Updates cell values for records. * @param records Specify the records be modified. * @return * * #### Description * Throws an error if the user does not have permission to update the given cell values in the record, or or recordId does not exist, * or when the written value type
(records: { id: string, valuesMap: { [key: string]: any } }[])
| 492 | * ``` |
| 493 | */ |
| 494 | async setRecords(records: { id: string, valuesMap: { [key: string]: any } }[]) { |
| 495 | const recordIds: string[] = []; |
| 496 | const recordsValues: { [key: string]: any }[] = []; |
| 497 | for (const record of records) { |
| 498 | recordIds.push(record.id); |
| 499 | recordsValues.push(record.valuesMap); |
| 500 | } |
| 501 | const state = this.wCtx.widgetStore.getState() as any as IReduxState; |
| 502 | const transformedRecordsValues = this.transformRecordValues(recordsValues); |
| 503 | this.checkRecordsValues(transformedRecordsValues); |
| 504 | |
| 505 | const data = records.reduce<ISetRecordOptions[]>((pre, record) => { |
| 506 | const recordId = record.id; |
| 507 | const recordData = Selectors.getRecord(state, recordId, this.datasheetId); |
| 508 | if (!recordData) { |
| 509 | throw new Error(`RecordId: ${recordId} is not exist in datasheet`); |
| 510 | } |
| 511 | |
| 512 | this.transformRecordValues([record.valuesMap]).forEach(valueMap => { |
| 513 | Object.entries(valueMap).forEach(([fieldId, cellValue])=> { |
| 514 | pre.push({ |
| 515 | recordId, |
| 516 | fieldId, |
| 517 | value: cellValue, |
| 518 | }); |
| 519 | }); |
| 520 | }); |
| 521 | return pre; |
| 522 | }, []); |
| 523 | |
| 524 | const result: ICollaCommandExecuteResult<any> = await cmdExecute({ |
| 525 | cmd: CollaCommandName.SetRecords, |
| 526 | datasheetId: this.datasheetId, |
| 527 | data, |
| 528 | }, this.wCtx.id); |
| 529 | if (result.result === ExecuteResult.Fail) { |
| 530 | throw new Error(result.reason); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Delete the given record. |
no test coverage detected