应用程序脚本 - 如何加快我的setbackground()函数?

发布于 2025-01-25 09:46:08 字数 1784 浏览 0 评论 0原文

我在应用程序脚本上的setbackground()函数努力。我该如何加快速度?它正在工作,但执行非常慢。

我已经写了这篇文章:

function changeColor(sheetName, startColorCol, sizeCellCol, totalCellCol) {

    var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)

    for (z = startColorCol; z <= totalCellCol; z = z + sizeCellCol) {

        // As this is called onEdit() we don't want to perform the entire script every time a cell is
        // edited- only when a status cell is mofified. 
        // To ensure this, before anything else we check to see if the modified cell is actually in the status column.
        if (sSheet.getActiveCell().getColumn() == z) {
            var row = sSheet.getActiveRange().getRow();
            var value = sSheet.getActiveCell().getValue();
            var col = "white"; // Default background color
            var colLimit = z; // Number of columns across to affect

            switch (value) {
                case "fait":
                    col = "MediumSeaGreen";
                    break;
                case "sans réponse":
                    col = "Orange";
                    break;
                case "proposition":
                    col = "Skyblue";
                    break;
                case "Revisions Req":
                    col = "Gold";
                    break;
                case "annulé":
                    col = "LightCoral";
                    break;
                default:
                    break;
            }
            if (row >= 3) {
                sSheet.getRange(row, z - 2, 1, sizeCellCol).setBackground(col);
            }
        }
    }
}

我看到我可能需要使用批处理操作,但我不知道如何使它起作用。

问题是,当更改一个值时,我需要为一系列单元格着色。有什么想法吗?

谢谢

I'm struggling with my setBackground() function on App script. How can I speed it up? It's working but the execution is very slow.

I have written this:

function changeColor(sheetName, startColorCol, sizeCellCol, totalCellCol) {

    var sSheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName)

    for (z = startColorCol; z <= totalCellCol; z = z + sizeCellCol) {

        // As this is called onEdit() we don't want to perform the entire script every time a cell is
        // edited- only when a status cell is mofified. 
        // To ensure this, before anything else we check to see if the modified cell is actually in the status column.
        if (sSheet.getActiveCell().getColumn() == z) {
            var row = sSheet.getActiveRange().getRow();
            var value = sSheet.getActiveCell().getValue();
            var col = "white"; // Default background color
            var colLimit = z; // Number of columns across to affect

            switch (value) {
                case "fait":
                    col = "MediumSeaGreen";
                    break;
                case "sans réponse":
                    col = "Orange";
                    break;
                case "proposition":
                    col = "Skyblue";
                    break;
                case "Revisions Req":
                    col = "Gold";
                    break;
                case "annulé":
                    col = "LightCoral";
                    break;
                default:
                    break;
            }
            if (row >= 3) {
                sSheet.getRange(row, z - 2, 1, sizeCellCol).setBackground(col);
            }
        }
    }
}

I saw I might need to use batch operations but I have no idea how to make it works.

The thing is, I need to color a range of cells when the value of one is changed. Any ideas ?

Thanks

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

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

发布评论

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

评论(1

梦里泪两行 2025-02-01 09:46:08

问题:

  • 您只想检查一个单元格,该单元格(var value = ssheet.getActivecell()。getValue();)。因此,使用循环是没有意义的。

解决方案:

  • 使用事件对象获取有关数据的数据编辑的单元格(表,列索引,行索引等),而不是每次使用循环getActivecell()。getColumn()。此事件对象将默认情况下传递给oneDit作为参数(e在下面的示例中),但您应将其传递给changeColor函数作为争论。
  • 在执行其他操作之前,请检查编辑的单元格是否是您要跟踪的范围内编辑的单元格之一(正确的表格,第3行,startColorCol之间的列totalcellcol

function onEdit(e) {
  // ...Some stuff...
  changeColor(e, sheetName, startColorCol,sizeCellCol, totalCellCol);
}

function changeColor(e, sheetName, startColorCol,sizeCellCol, totalCellCol) {
  const range = e.range;
  const column = range.getColumn();
  const row = range.getRow();
  const sSheet = range.getSheet();
  if (sSheet.getName() === sheetName && column >= startColorCol && column <= totalCellCol && row >= 3) {
    const value = range.getValue();
    let col = "white"; // Default background color
    switch (value) {
      case "fait":
        col = "MediumSeaGreen";
        break;
      case "sans réponse":
        col = "Orange";
        break;
      case "proposition":
        col = "Skyblue";
        break;
      case "Revisions Req":
        col = "Gold";
        break;
      case "annulé":
        col = "LightCoral";
        break;
      default:
        break;  
    }
    sSheet.getRange(row, column-2, 1, sizeCellCol).setBackground(col);
  }
}

Issues:

  • You only want to check a single cell, the cell that was edited (var value = sSheet.getActiveCell().getValue();). Therefore, it doesn't make sense to use a loop.

Solution:

  • Use the event object to get the data regarding the edited cell (sheet, column index, row index, etc.), instead of using a loop and checking getActiveCell().getColumn() each time. This event object is passed to onEdit as a parameter by default (e in the sample below), but you should pass it to your changeColor function as an argument.
  • Before doing anything else, check whether the edited cell is one of the edited cell is in the range you are tracking (correct sheet, row over 3, column between startColorCol and totalCellCol.
  • If the edited cell is in the proper range, update the background colors.

Code sample:

function onEdit(e) {
  // ...Some stuff...
  changeColor(e, sheetName, startColorCol,sizeCellCol, totalCellCol);
}

function changeColor(e, sheetName, startColorCol,sizeCellCol, totalCellCol) {
  const range = e.range;
  const column = range.getColumn();
  const row = range.getRow();
  const sSheet = range.getSheet();
  if (sSheet.getName() === sheetName && column >= startColorCol && column <= totalCellCol && row >= 3) {
    const value = range.getValue();
    let col = "white"; // Default background color
    switch (value) {
      case "fait":
        col = "MediumSeaGreen";
        break;
      case "sans réponse":
        col = "Orange";
        break;
      case "proposition":
        col = "Skyblue";
        break;
      case "Revisions Req":
        col = "Gold";
        break;
      case "annulé":
        col = "LightCoral";
        break;
      default:
        break;  
    }
    sSheet.getRange(row, column-2, 1, sizeCellCol).setBackground(col);
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文