Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
How to increment a set of 2 letters using GAS?

How could this one be tweaked so that it could increment a set of two letters, so that it'd look like this:

AA, AB, AC...AZ, BA, BB, BC, etc

This is borrowed from tckmn, but it addresses one letter only.

var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
function incrementChar(c) {
    var index = alphabet.indexOf(c)
    if (index == -1) return -1 // or whatever error value you want
    return alphabet[index + 1 % alphabet.length]
}

Appreciate your help!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You just need two loops. One to iterate over the alphabet, and the second to iterate over the alphabet on each iteration of the first loop.

const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

const arr = [];

for (let i = 0; i < alphabet.length; i++) {
  for (let j = 0; j < alphabet.length; j++) {
    arr.push(`${alphabet[i]}${alphabet[j]}`);
  }  
}

console.log(arr);

about 4 years ago · Juan Pablo Isaza Relatório

0

In your situation, how about the following sample script?

const increase = s => {
  const idx = [...s].reduce((c, e, i, a) => (c += (e.charCodeAt(0) - 64) * Math.pow(26, a.length - i - 1)), -1);

  // Ref: https://stackoverflow.com/a/53678158
  columnIndexToLetter = (n) => (a = Math.floor(n / 26)) >= 0 ? columnIndexToLetter(a - 1) + String.fromCharCode(65 + (n % 26)) : "";

  return columnIndexToLetter(idx + 1);
};

const samples = ["A", "Z", "AA", "AZ", "ZZ"];
const res1 = samples.map(e => increase(e));
console.log(res1); // <--- [ 'B', 'AA', 'AB', 'BA', 'AAA' ]

// If you want to give one value, please use the following script.
const sample = "AA";
const res2 = increase(sample);
console.log(res2); // <--- AB

  • When this script is run, ["A", "Z", "AA", "AZ", "ZZ"] is converted to [ 'B', 'AA', 'AB', 'BA', 'AAA' ].

Reference:

  • map()
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda