Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

478
Vistas
Split string for every X number of characters in a Google Sheet

I want to split a cell value into multiple lines by splitting a string by every 5th character.

Example: in below sample (screenshot) we have ABCDEFGHIJKLMNOPQRSTUVWXYZ in A1 and in B1 I want the string to be split after every 5th letter.

How can I do this?

Screenshot of Google Sheet displaying desired outcome

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

From your google-apps-script tag, when Google Apps Script is used, how about the following sample script?

Sample script:

Please copy and paste the following script to the script editor of Google Spreadsheet and save the script. And, please put a custom function of =SAMPLE(A1) to a cell. By this, the result value is put to the cell.

function SAMPLE(value) {
  const v = [...value];
  const res = [];
  while (v.length > 0) {
    res.push(v.splice(0, 5).join(""));
  }
  return res.join("\n");
}

Or, for example, if you want to use the custom function as =SAMPLE(A1) and =SAMPLE(A1:D5), how about the following sample script?

function output_(value) {
  const v = [...value];
  const res = [];
  while (v.length > 0) {
    res.push(v.splice(0, 5).join(""));
  }
  return res.join("\n");
}

function SAMPLE(values) {
  return Array.isArray(values) ? values.map(r => r.map(c => output_(c))) : output_(values);
}

Note:

  • For example, when you use a built-in formula, how about the following formula? This formula supposes the text of ABCDEFGHIJKLMNOPQRSTUVWXYZ to a cell "A1". Please put this formula in the cell.

      =JOIN(CHAR(10),REGEXEXTRACT(A1,REGEXREPLACE(A1,"(.{0,5})","($1)")))
    

References:

  • Custom Functions in Google Sheets
  • map()
  • splice()
about 4 years ago · Juan Pablo Isaza Denunciar

0

function test() {
  let s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  let a = s.split("").slice();
  let d = 0;
  s.split("").forEach((r, i) => {
    if (i > 0 && i % 5 == 0) {
      a.splice(i + d++, 0, '\n');
    }
  });
  Logger.log(a.filter(e => e).flat().join(""));
}

Execution log
8:20:25 PM  Notice  Execution started
8:20:24 PM  Info    ABCDE
FGHIJ
KLMNO
PQRST
UVWXY
Z
8:20:26 PM  Notice  Execution completed
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda