可以弄清楚如何完成我的循环(如何重置循环?)
我有一个带有一个列的电子表格,每行中的数量很小(例如:1、1、3、4、5、5等)。
目标是使用循环计数每一行,直到计数到3,然后突出显示该行,然后重新开始。因此,我的循环可以成功地计算到三分并突出显示,但随后停止。我该如何“重置”循环以使其继续进行?
这是我目前的代码:
function forloop () {
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
let sheet = spreadsheet.getSheetByName("Sheet1");
for (let i = 1; i < 9; i++) {
let currentCell = sheet.getRange(i, 1).getValue();
let nextCell = sheet.getRange(i+1, 1).getValue();
if (currentCell + nextCell === 3) {
sheet.getRange(i,1).setBackground("#8e7cc3");
}
} // loop ends
} // function ends
I have a spreadsheet with one column that has a small number in every row (for example: 1, 1, 3, 4, 5, 5 etc)
The goal is to use a loop to count each row until the count gets to 3, then highlight that row, and start over. So my loop can successfully count to three and highlight, but then it stops. How can I "reset" the loop so it continues?
Here's my code currently:
function forloop () {
let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
let sheet = spreadsheet.getSheetByName("Sheet1");
for (let i = 1; i < 9; i++) {
let currentCell = sheet.getRange(i, 1).getValue();
let nextCell = sheet.getRange(i+1, 1).getValue();
if (currentCell + nextCell === 3) {
sheet.getRange(i,1).setBackground("#8e7cc3");
}
} // loop ends
} // function ends
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只希望循环继续进行直到到达工作表的末端,则可以使用:
使用您给出的代码段,您才会在CurrentCell和NextCell的值最多总和3时突出显示该框的颜色。
但是,但是什么。我相信您正在尝试实现,这是每个第三行的强调,因此,如果您正在寻找的话,这也是解决方案:
这是我第一次回答溢出问题的问题:),希望它会有所帮助。
If you simply want the loop to continue until it reaches the end of your sheet, you can use:
With the code snippet you gave, you will highlight the box that color only when the values of currentCell and nextCell sum up to 3.
But what I believe you are trying to achieve is to highlight every third row, so here is the solution to that as well if it is what you are looking for:
This is my first time answering a question on overflow :), hope it helps.
要将行突出显示3,您需要使用Modulo。
,这意味着数字可以除以分数。在您的情况下,3、6、9、12 ...可除以3,剩余零。使用此功能将确保该行只能每3行突出显示。
在我的示例代码中,我收集了从A1开始并将其存储在数组中的所有行数。该数组将用作
getRangelist()
的参数,该参数可以突出显示数组中的给定单元格。它将在一个操作中设置多个单元背景。代码:
输出:
参考:
To highlight rows by 3, you need to use modulo.
If the modulo of a number is 0, it means the number is divisible by the divisor. In your case 3, 6, 9, 12... are divisible by 3 with zero remainder. Using this will ensure that the row will only get highlighted every 3 rows.
Here in my sample code, I gather all row numbers that are divisible by 3 starting from A1 and store it in an array. The array will be used as a parameter for
getRangeList()
which can highlight the given cells in the array. It will set multiple cells background in one operation.Code:
Output:
Reference: