You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript如何通过for循环按6字母步长生成电子表格列标识数组

实现方案

你可以先实现一个列号转列字母的工具函数,之后按照每次偏移6列的规则循环CantAMS次即可自动生成所有变量,无需手动逐个枚举:

const CantAMS = 10
// 列号转列字母工具函数
function columnToLetter(column) {
  let temp, letter = '';
  while (column > 0) {
    temp = (column - 1) % 26;
    letter = String.fromCharCode(temp + 65) + letter;
    column = (column - temp - 1) / 26;
  }
  return letter;
}

// 初始化所有数组
const ColumnasAm = []
const AMName = []
const Ammail = []
const Check = []
const Columni = []

// 循环生成对应值,每次偏移6列
for (let i = 0; i < CantAMS; i++) {
  // 计算当前轮次的基准列号,匹配原有列号规则
  const currentCol = 1 + 6 * i;
  const baseColLetter = columnToLetter(currentCol);
  Columni.push(currentCol);
  ColumnasAm.push(`${baseColLetter}1:${baseColLetter}`);
  AMName.push(ws.getRange(`${baseColLetter}2`).getValue());
  // Ammail取基准列后一列
  Ammail.push(ws.getRange(`${columnToLetter(currentCol + 1)}2`).getValue());
  Check.push(ws.getRange(`${baseColLetter}5`).getValue());
}

生成的所有数组内容和你手动编写的完全一致,可直接按原有逻辑使用。

内容的提问来源于stack exchange,提问作者Kristian Andreas Brinckmann

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 00:45:07