import { IStoreState, ICell, ID } from '../../index.data'; import { getCellIdByStr, getLabelByIj } from '../../store/formula_support'; import { getCellij } from '../../store/selectors'; import { FormulaManagerConfig } from '../index.data'; import * as R from 'rambda'; import { isExternalCell, getExternalLabelById } from '../../store/formula_support/externalCell'; // {{id111}}+1 => A1+1 // {{idAAA!11}}+1 => B!A1+1 export function idFormula2LabelFormula( state: IStoreState, s: string, config?: FormulaManagerConfig, ): string { return s.replace(/\{\{id[^\}]+\}\}/g, idStr => { const cellId = getCellIdByStr(state, idStr, config); if (!cellId) throw new Error(`idFormula2LabelFormula未找到cellId:${idStr}`); return id2Label(state, cellId, config); }); } // A1 + 1 => {{id111}}+1 // B!A1+1 => {{idAAA!11}}+1 export function labelFormula2IdFormula( state: IStoreState, s: string, config?: FormulaManagerConfig, ): string { return s.replace(/([a-zA-Z0-9]+![a-zA-Z]+[0-9]+)|([a-zA-Z]+[0-9]+)/g, labelStr => { const cellId = getCellIdByStr(state, labelStr, config); if (!cellId) throw new Error(`labelFormula2IdFormula未找到cellId:${labelStr}`); return '{{id' + cellId + '}}'; }); } // 1111 => A1 export function id2Label(state: IStoreState, id: ID, config?: FormulaManagerConfig): string { if (isExternalCell(id)) { if (!config) throw new Error('id2Label出错: 指定了外部单元格, 则必须设置 config'); return getExternalLabelById(id + '', config); } else { const { i, j } = getCellij(state, id); return getLabelByIj(i, j); } } // A1 => 111 // A!A1 => 333!111 export function label2Id(state: IStoreState, labelStr: ID, config?: FormulaManagerConfig): ID { const cellId = getCellIdByStr(state, labelStr + '', config); return cellId!; } /// "A1+A2" => [33,55], for highlight // 注意排除外部单元格, 因为无需高亮 export function getCellIdsFromPositionedContent( state: IStoreState, content: string, config?: FormulaManagerConfig, ): ID[] { const result = content.match(/([a-zA-Z0-9]+![a-zA-Z]+[0-9]+)|([a-zA-Z]+[0-9]+)/g); return result ? result .filter(it => !isExternalCell(it)) .map(it => label2Id(state, it, config)) .filter(it => (it + '').indexOf('!') < 0) : []; }