[英]App Script - How can I Speed up My setBackground() function?
我正在为 App 脚本上的setBackground()
function 苦苦挣扎。 我怎样才能加快速度? 它正在工作,但执行速度非常慢。
我写了这个:
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);
}
}
}
}
我看到我可能需要使用批处理操作,但我不知道如何让它工作。
问题是,当一个单元格的值发生变化时,我需要为一系列单元格着色。 有任何想法吗?
谢谢
var value = sSheet.getActiveCell().getValue();
)。 因此,使用循环没有意义。getActiveCell().getColumn()
。 默认情况下,此事件 object 作为参数传递给onEdit
(下面示例中的e
),但您应该将其作为参数传递给您的changeColor
function。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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.