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

253
Vistas
salir de una declaración de bucle for

Tengo un pequeño problema ya que no soy tan bueno con la codificación, tengo un código que me funciona bien pero quiero mejorarlo. quiero que mi código cuando no encuentre ningún dato simplemente salga del bucle y muestre este mensaje "AUCUNE DEMANDE A VALIDER".

 for (i = 1; i < dataValues.length; i++) { if (dataValues[i][11] === 'COMMANDE CONFIRMER' && dataValues[i][12] != '' && dataValues[i][13] === '') { pasteSheet.appendRow([dataValues[i][0], dataValues[i][1], dataValues[i][2], dataValues[i][3], dataValues[i][4], dataValues[i][5], dataValues[i][6], dataValues[i][7], dataValues[i][8], dataValues[i][9], dataValues[i][10], dataValues[i][11]]); var clearRow = i + 2; copySheet.getRange('A' + clearRow + ':M' + clearRow).clear(); } } // get destination range var destination = pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, max, 1); // clear source values Browser.msgBox('Commande Confirmer'); }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Aunque puede salir de un bucle for , hay otras formas de ser más eficiente. Más importante que salirse de su ciclo, es minimizar los procesos que consumen más recursos. Mirando su código, evita appendRow() repetitivamente.

Aquí hay una manera más eficiente de lograr lo que has publicado:

 // From dataValues, only keep rows that match this criteria: const filteredValues = dataValues.filter(row => row[11] === 'COMMANDE CONFIRMER' && row[12] !== `` && row[13] === ``) // If there are rows matching the criteria... if (filteredValues.length) { // Get all indexes of these rows... const indexes = filteredValues.map(item => dataValues.findIndex(row => row.every((cell, index) => cell === row[index]))+3) // and get each row to only include Columns A - M. const values = filteredValues.map(item => item.slice(0, 11)) // For each index stored... indexes.forEach(index => { // Clear each row at the appropriate index. (Note: If your cells are not formatted, this can be done more efficiently.) copySheet.getRange(`A${index}:M${index}`).clear() }) // Set all values at once. pasteSheet.getRange(pasteSheet.getLastRow()+1, 1, values.length, values[0].length).setValues(values) } Browser.msgBox('Commande Confirmer')

sin comentar:

 const filteredValues = dataValues.filter(row => row[11] === 'COMMANDE CONFIRMER' && row[12] !== `` && row[13] === ``) if (filteredValues.length) { const indexes = filteredValues.map(item => dataValues.findIndex(row => row.every((cell, index) => cell === row[index]))+3) const values = filteredValues.map(item => item.slice(0, 11)) indexes.forEach(index => copySheet.getRange(`A${index}:M${index}`).clear()) pasteSheet.getRange(pasteSheet.getLastRow()+1, 1, values.length, values[0].length).setValues(values) } Browser.msgBox('Commande Confirmer')

Aprende más:

  • --> Métodos de matriz <--
  • Array.length
  • Matriz.filtro()
  • matriz.cada()
  • matriz.mapa()
  • Array.slice()
  • Matriz.paraCada()
  • Rango.establecerValores()
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