import { ICellNN, ID } from '../../index.data'; import { sliceBy } from 'valor-app-utils'; import { ERROR_REF } from '../../formula-parser/error'; import { FormulaManagerConfig } from '../../formula-manager/index.data'; import { getValue } from '../../utils'; // 独立此文件, 避免与selector/cell的递归依赖 export function isExternalCell(cellId: ID) { return (cellId + '').indexOf('!') >= 0; } // AAA!A1=>333!11 export function getExternalCellIdByLabel( sCell: string, options: { getSheetIdByCode: (code: string) => ID; getExternalCellIdByLabel: (sheetCode: string, cellLabel: string) => ID; }, ) { const sheetCode = sliceBy(sCell, { to: '!' }); const cellLabel = sliceBy(sCell, { from: '!' }).slice(1); const sheetId = options.getSheetIdByCode(sheetCode); const cellId = options.getExternalCellIdByLabel(sheetCode, cellLabel); if (!sheetId || !cellId) throw new Error(ERROR_REF); return `${sheetId}!${cellId}`; } // 333!11 => AAA!A1 export function getExternalLabelById( sCell: string, options: { getSheetCodeById: (id: ID) => string; getExternalCellLabelById: (sheetId: ID, cellID: ID) => string; }, ) { // const { sheetId, cellId } = splitExtenalCell(sCell + ''); const [sheetId, cellId] = (sCell + '').split('!'); const sheetCode = options.getSheetCodeById(sheetId); const cellLabel = options.getExternalCellLabelById(sheetId, cellId); if (!sheetId || !cellId) throw new Error(ERROR_REF); return `${sheetCode}!${cellLabel}`; } /* export function getExternalCellById(sCell: string, config: FormulaManagerConfig) { const { sheetId, cellId } = splitExtenalCell(sCell + ''); return config.getExternalCell(sheetId, cellId); } */ // 'sheet1!15' => ExternalCell export function getExternalCellById(cellId: string, config?: FormulaManagerConfig): ICellNN | null { if (isExternalCell(cellId)) { if (!config) { throw new Error('查找表间引用出错: 必须在初始化SpreadSheet时, 配置 formulaConfig 属性!'); } const [extSheetId, extCellId] = (cellId + '').split('!'); const cell = config.getExternalCell(extSheetId, extCellId); if (cell) { return Object.assign({}, cell, { value: getValue(cell.value, cell.dataType) }); } } return null; } // '111!11'=> {sheetId:111, cellId:11} // export function splitExtenalCell(extCellId: string) { // const sheetId = sliceBy(extCellId, { to: '!' }); // const cellId = sliceBy(extCellId, { from: '!' }).slice(1); // return { sheetId, cellId }; // }