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