可以弄清楚如何完成我的循环(如何重置循环?)

发布于 2025-02-12 22:31:40 字数 569 浏览 0 评论 0原文

我有一个带有一个列的电子表格,每行中的数量很小(例如: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 技术交流群。

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

发布评论

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

评论(2

暖阳 2025-02-19 22:31:40

如果您只希望循环继续进行直到到达工作表的末端,则可以使用:

function forloop() {

  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getSheetByName("Sheet1");
  var rows = sheet.getDataRange().getValues().length;

  for (let i = 1; i < rows + 1; 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");
    }
  } // function ends
}

使用您给出的代码段,您才会在CurrentCell和NextCell的值最多总和3时突出显示该框的颜色。

但是,但是什么。我相信您正在尝试实现,这是每个第三行的强调,因此,如果您正在寻找的话,这也是解决方案:

function forloop() {

  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getSheetByName("Sheet1");
  var rows = sheet.getDataRange().getValues().length;

  for (let i = 1; i < rows + 1; i++) {
    if (i%3 === 0) {
      sheet.getRange(i, 1).setBackground("#8e7cc3");
    }
  } // function ends
}

这是我第一次回答溢出问题的问题:),希望它会有所帮助。

If you simply want the loop to continue until it reaches the end of your sheet, you can use:

function forloop() {

  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getSheetByName("Sheet1");
  var rows = sheet.getDataRange().getValues().length;

  for (let i = 1; i < rows + 1; 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");
    }
  } // function ends
}

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:

function forloop() {

  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getSheetByName("Sheet1");
  var rows = sheet.getDataRange().getValues().length;

  for (let i = 1; i < rows + 1; i++) {
    if (i%3 === 0) {
      sheet.getRange(i, 1).setBackground("#8e7cc3");
    }
  } // function ends
}

This is my first time answering a question on overflow :), hope it helps.

ぶ宁プ宁ぶ 2025-02-19 22:31:40

要将行突出显示3,您需要使用Modulo。

modulo是一个数学操作,在一个整数时找到其余的
除以另一个

,这意味着数字可以除以分数。在您的情况下,3、6、9、12 ...可除以3,剩余零。使用此功能将确保该行只能每3行突出显示。

在我的示例代码中,我收集了从A1开始并将其存储在数组中的所有行数。该数组将用作getRangelist()的参数,该参数可以突出显示数组中的给定单元格。它将在一个操作中设置多个单元背景。

代码:

function forloop() {
  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getSheetByName("Sheet1");
  let lastRow = 9; //set last row
  let startRow = 1 //set starting row
  let column = "A"; //set column
  let rangeList = []; //create empty array
  for (let i = startRow; i <= lastRow; i++) { //loop from start row to last row
    if(i % 3 == 0){ //get the modulo of iterator, if it is 0 add a1 notation to the array
      rangeList.push(column+i) 
    }
  }
  sheet.getRangeList(rangeList).setBackground("#8e7cc3"); //set background
}

输出:

”在此处输入图像描述”

参考:

To highlight rows by 3, you need to use modulo.

Modulo is a math operation that finds the remainder when one integer
is divided by another

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:

function forloop() {
  let spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  let sheet = spreadsheet.getSheetByName("Sheet1");
  let lastRow = 9; //set last row
  let startRow = 1 //set starting row
  let column = "A"; //set column
  let rangeList = []; //create empty array
  for (let i = startRow; i <= lastRow; i++) { //loop from start row to last row
    if(i % 3 == 0){ //get the modulo of iterator, if it is 0 add a1 notation to the array
      rangeList.push(column+i) 
    }
  }
  sheet.getRangeList(rangeList).setBackground("#8e7cc3"); //set background
}

Output:

enter image description here

Reference:

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