从循环陈述中爆发

发布于 2025-02-08 20:27:13 字数 952 浏览 3 评论 0 原文

我有一个小问题,因为我对编码不太好,我有一个对我有用的代码,但我想改进它。我希望我的代码在找不到任何数据时只能离开循环并显示此消息“ Aucune要求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');
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

强者自强 2025-02-15 20:27:13

尽管您可以为循环中断,但还有其他方法可以提高效率。比从循环中突破更重要的是最大程度地减少了资源重型流程。查看您的代码,您避免重复 appendrow()

这是完成您发布的内容的更有效方法:

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

未注册:

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

了解更多信息:

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:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文