您好,我们公式设置前后加了这部分代码,效率依然很低,请问是什么原因呢
- initFormulas = activeSheet => {
- const { formulas } = this.props;
- if (formulas && formulas.size) {
- if (!this.needReFormulas(activeSheet, formulas)) {
- return;
- }
- this.props.handleBindInteraction({ functionName: 'handleArtificialFormula', functionParams: false });
- // console.log('===initFormula===');
- // this.props.handleBindInteraction({ functionName: 'handleLoading', functionParams: { message: '检测到公式,正在写入~', type: 'start' } });
- try {
- const spread = this.getSpread();
- const activeSheet = spread.getActiveSheet();
- activeSheet.suspendPaint();
- spread.suspendCalcService(false);
- const errors = new Map();
- formulas.forEach((value, key) => {
- let error = false;
- if (!isValuedIndex(key.rowIndex) || !isValuedIndex(key.colIndex)) {
- return;
- }
- const formulaType = key.formulaType ? key.formulaType : 'cell'; // 增加公式类型判断
- const rowCount = formulaType === 'column' ? this.dataLength : 1;
- const conditionFormula = key.conditionFormula; //是否为条件公式
- // activeSheet.setFormula(4, 1, "IF(A1>10, A1*2, A1*3)"); 三元模式
- if (isValuedContent(value)) {
- // 直接为基础类型,且可以展示
- activeSheet.setValue(key.rowIndex, key.colIndex, value);
- } else if (value && value.symbol && value.items) {
- if (Array.isArray(value.items)) {
- let hasError = false;
- const locations = value.items.map(item => {
- const location = this.index2A1(item, rowCount);
- if (location.error) {
- // console.log('==initFormula-error==', location);
- hasError = true;
- } else {
- return location;
- }
- });
- if (hasError) {
- // 存在异常,取消公式设置
- return;
- }
- const cFormula = this.handleFormulaSymbol(locations, value.symbol);
- if (cFormula.error) {
- // console.log('==initFormula-error1==', cFormula);
- return;
- }
- // console.log('====setFormula==', key.rowIndex, key.colIndex, formulaType, '===', `=${cFormula}`);
- if (formulaType === 'cell') {
- activeSheet.setFormula(key.rowIndex, key.colIndex, `=${cFormula}`);
- } else {
- activeSheet.setArrayFormula(0, key.colIndex, this.dataLength, 1, `=${cFormula}`);
- }
- if (conditionFormula === true) {
- activeSheet.setCellType(key.rowIndex, key.colIndex, new MyCellType(key, key?.rule));
- }
- } else {
- error = true;
- }
- } else if (value && isValuedIndex(value.rowIndex) && isValuedIndex(value.colIndex)) {
- const cFormula = this.index2A1(value, rowCount);
- if (cFormula.error) {
- // console.log('==initFormula-error2==', cFormula);
- return;
- }
- // console.log('====setFormula==', key.rowIndex, key.colIndex, formulaType, '===', `=${cFormula}`);
- if (formulaType === 'cell') {
- activeSheet.setFormula(key.rowIndex, key.colIndex, `=${cFormula}`);
- } else {
- activeSheet.setArrayFormula(0, key.colIndex, this.dataLength, 1, `=${cFormula}`);
- }
- if (conditionFormula === true) {
- activeSheet.setCellType(key.rowIndex, key.colIndex, new MyCellType());
- }
- } else if (isValuedContent(value.constValue)) {
- activeSheet.setValue(key.rowIndex, key.colIndex, value.constValue);
- } else {
- error = true;
- }
- if (error) {
- errors.set(key, value);
- }
- });
- // foreach end
- spread.resumeCalcService(true);
- activeSheet.resumePaint();
- this.props.handleModelInteraction({ functionName: 'setNeedReFormula', functionParams: false });
- this.props.handleBindInteraction({ functionName: 'handleFormatModelData', functionParams: true });
- // this.props.handleBindInteraction({ functionName: 'handleLoading', functionParams: { message: '公式写入完毕~', type: 'end' } });
- } catch (e) {
- console.log('======initFormula error=====', e);
- this.props.handleBindInteraction({ functionName: 'handleLoading', functionParams: { message: cb.lang.templateByUuid('UID:P_DGYL-FE_1AD6753E05480027', '公式写入出现异常!'), type: 'end' } });
- }
- // activeSheet.options.isProtected = true;
- }
- };
复制代码 |