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

254
Visualizações
breaking out from a for loop statement

I have a small issue as I'm not that good with coding, I have a code that it's working for me good but I want to improve it. i want that my code when it will not find any data to just get out of the loop for and show this message "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 Respostas
Responde à pergunta

0

Although you can break out of a for loop, there are other ways you can be more efficient. More important than breaking out of your loop, is minimizing the most resource heavy processes. Looking at your code, you avoid repetitively appendRow().

Here's a more efficient way to accomplish what you've posted:

// 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')

Uncommented:

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')

Learn More:

  • --> Array Methods <--
  • Array.length
  • Array.filter()
  • Array.every()
  • Array.map()
  • Array.slice()
  • Array.forEach()
  • Range.setValues()
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