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'); }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: