计算excel两列中间的间隔多少列
详细描述
需求要求计算excel文件中两列中间隔了多少列?比如A列和AA列
思路
- 将列号比如(A或者AA)转化成对应的数字,A对应1,AA对应27
如何将字母转化为数字?
parseInt('A'.toLowerCase(), 26) - 9
- 两个数字做减法
代码实现
// 将大写字母转化为数字
function toNumber(str) {
return str.split('').reduce((acc, curItem, curIndex) => {
let result = acc
result += Math.pow(26, curIndex) * (parseInt(curItem.toLowerCase(), 26) - 9)
return result
}, 0)
}
console.log("toNumber('AA') - toNumber('A') = ", toNumber('AA') - toNumber('A'))