| | |
| | | * @returns 返回所有参数的和,如果参数列表为空或参数类型非数值型,则返回null |
| | | */ |
| | | function SUM(...val){ |
| | | try { |
| | | let num = null; |
| | | if(val&&val.length>0){ |
| | | val.forEach(item=>{ |
| | |
| | | }) |
| | | } |
| | | return num; |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 计算传入参数中的最大值 |
| | |
| | | * @returns 返回最大值,如果参数列表为空或为null/undefined/'',则返回null |
| | | */ |
| | | function MAX(...val){ |
| | | try { |
| | | let max = null; |
| | | if(val&&val.length>0){ |
| | | val = val.filter(item=>item!=null&&item!=='') |
| | |
| | | } |
| | | } |
| | | return max; |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 计算传入参数中的最小值 |
| | |
| | | * @returns 返回传入参数中的最小值,如果传入参数为空或所有值均为null或空字符串,则返回null |
| | | */ |
| | | function MIN(...val){ |
| | | try { |
| | | let min = null; |
| | | if(val&&val.length>0){ |
| | | val = val.filter(item=>item!=null&&item!=='') |
| | |
| | | } |
| | | } |
| | | return min; |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 计算给定数值的平均值 |
| | |
| | | * @returns 返回平均值,如果数值列表为空或包含非数值项,则返回null |
| | | */ |
| | | function AVERAGE(...val){ |
| | | try { |
| | | let num = null; |
| | | let arr = []; |
| | | if(val&&val.length>0){ |
| | |
| | | }else{ |
| | | return null; |
| | | } |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 计算一个数的绝对值 |
| | |
| | | * @returns 返回该数值的绝对值 |
| | | */ |
| | | function ABS(val){ |
| | | try { |
| | | return Math.abs(val); |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 计算一组数字的中位数 |
| | |
| | | * @returns 如果参数中有有效的数字,则返回计算出的中位数;否则返回null |
| | | */ |
| | | function MEDIAN(...val){ |
| | | try { |
| | | let arr = []; |
| | | if(val&&val.length>0){ |
| | | arr = val.filter(item=>item!=null&&item!=='') |
| | |
| | | } |
| | | }else{ |
| | | return null; |
| | | } |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | * @returns 返回一个包含坐标信息的数组 |
| | | */ |
| | | function changeParameter(f){ |
| | | try { |
| | | let arr = getABCList(f) |
| | | let arr2 = [] |
| | | arr.forEach(item=>{ |
| | |
| | | } |
| | | }) |
| | | return arr2; |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 获取包含 ABC 字符的列表 |
| | |
| | | * @returns 包含 ABC 字符的数组列表 |
| | | */ |
| | | function getABCList(f){ |
| | | try { |
| | | let regex = /[=\+\-\*\%\(\)\/\^\s]/g; |
| | | let fouList = [ |
| | | "SUM", |
| | |
| | | return item&& /[a-zA-Z]/.test(item)&&item!='undefined'&&item!='null' |
| | | }); |
| | | return arr; |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | /** |
| | | * 获取所有单元格 |
| | |
| | | * @returns 返回一个对象,其键为单元格的唯一标识符(由列和行ID拼接而成),值为null |
| | | */ |
| | | function getAllCell(f){ |
| | | try { |
| | | let arr = changeParameter(f) |
| | | let arr0 = {} |
| | | arr.forEach(item=>{ |
| | | arr0[getColumnNameFromId(`${item.c}`+'-'+`${item.r}`)] = null |
| | | }) |
| | | return arr0; |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | |
| | | /** |
| | |
| | | * @returns 返回计算后的结果,如果计算失败则返回0 |
| | | */ |
| | | function compute(f,comValue){ |
| | | try { |
| | | let str = f |
| | | // 获取单元格对应值 |
| | | let arr = getAllCell(f) |
| | |
| | | str = str.replaceAll(a, obj[a]) |
| | | } |
| | | // 计算 |
| | | try{ |
| | | for (var a in arr) { |
| | | str = str.replaceAll(a, arr[a]) |
| | | } |
| | | return eval(str) |
| | | }catch(e){ |
| | | console.log('e',e) |
| | | return 0 |
| | | } catch (error) { |
| | | |
| | | } |
| | | } |
| | | |