(context: IFormulaContext)
| 46 | } |
| 47 | |
| 48 | function resolverWrapper(context: IFormulaContext): ResolverFunction { |
| 49 | /** |
| 50 | * The role of the resolver is, given a fieldId, get the value from the context according to the string |
| 51 | *params |
| 52 | * @value field ID |
| 53 | * @originValue: whether to return the value of the original cell |
| 54 | */ |
| 55 | return (fieldId: string) => { |
| 56 | /** |
| 57 | * Check for reserved keywords when formulas are run in the lookUp field. |
| 58 | * When the keyword is hit, return LookUpField's cellValueArray instead of cellValue |
| 59 | */ |
| 60 | const hostField = context.field; |
| 61 | const state = context.state; |
| 62 | // TODO: The first phase of column permissions, |
| 63 | // because the middle layer does not actively push data, |
| 64 | // and getFieldMap actively masks the data, so here we temporarily use the datasheet to get the fieldmap directly |
| 65 | const datasheet = getDatasheet(state, context.field.property.datasheetId)!; |
| 66 | const fieldMap = datasheet.snapshot.meta.fieldMap; |
| 67 | // const fieldMap = getFieldMap(state, context.field.property.datasheetId)!; |
| 68 | if (hostField.type === FieldType.LookUp && fieldId === ROLLUP_KEY_WORDS) { |
| 69 | const flatCellValue = Field.bindContext(hostField, state).getFlatCellValue(context.record.id, true); |
| 70 | return Field.bindContext(hostField, state).cellValueToArray(flatCellValue); |
| 71 | } |
| 72 | |
| 73 | const fieldPermissionMap = getFieldPermissionMap(state, datasheet.id); |
| 74 | const fieldRole = getFieldRoleByFieldId(fieldPermissionMap, fieldId); |
| 75 | |
| 76 | const field = fieldMap[fieldId]; |
| 77 | if (!field && !fieldRole) { |
| 78 | throw new Error( |
| 79 | t(Strings.view_field_search_not_found_tip, { |
| 80 | value: fieldId, |
| 81 | }), |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | const record = context.record; |
| 86 | const recordSnapshot = { meta: { fieldMap: fieldMap }, recordMap: { [record.id]: record } }; |
| 87 | const cellValue = getCellValue(state, recordSnapshot, record.id, fieldId, false, datasheet.id, true); |
| 88 | |
| 89 | // TODO what if field is undefined? |
| 90 | // String type fields need special treatment. Convert Segment|id type to pure string; |
| 91 | const fieldBasicValueType = Field.bindContext(field!, state).basicValueType; |
| 92 | // Currently "", [], false will be converted to null when getCellValue, |
| 93 | // in order to ensure the correct calculation result of the formula, the boolean type needs to be converted null => false |
| 94 | if (fieldBasicValueType === BasicValueType.Boolean) { |
| 95 | return Boolean(cellValue); |
| 96 | } |
| 97 | if (fieldBasicValueType === BasicValueType.String) { |
| 98 | // TODO what if field is undefined? |
| 99 | return Field.bindContext(field!, state).cellValueToString(cellValue as any); |
| 100 | } |
| 101 | if (fieldBasicValueType === BasicValueType.Array) { |
| 102 | // TODO what if field is undefined? |
| 103 | return (Field.bindContext(field!, state) as ArrayValueField).cellValueToArray(cellValue as any); |
| 104 | } |
| 105 | return cellValue; |
no test coverage detected