[英]Google apps script - Why isn't my for-loop executing?
I'm trying to write a simple script to clear the values from a specific Google Sheets range [H29:H] if the background color of the cell = #ffff00.如果单元格的背景颜色 = #ffff00,我正在尝试编写一个简单的脚本来清除特定 Google 表格范围 [H29:H] 中的值。 While the script doesn't return any errors, it isn't making any changes to cells that meet those criteria (or anywhere else in the sheet).
虽然脚本不会返回任何错误,但它不会对符合这些条件的单元格(或工作表中的任何其他位置)进行任何更改。 Any guidance as to where I am going wrong?
关于我哪里出错的任何指导?
function resetCells() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Test Sheet');
var rangeData = sheet.getDataRange();
var lastRow = rangeData.getLastRow();
var searchRange = sheet.getRange('H29:H');
var rangeColors = searchRange.getBackgrounds();
for ( i = 29 ; i < lastRow - 1; i++){
if(rangeColors[i][8] === '#ffff00'){
sheet.getRange(i,8).clearContent();
};
};
}
When I saw your script, I thought that the index of rangeColors
might not be corresponding to i
in the for a loop.当我看到你的脚本时,我认为
rangeColors
的索引可能与 for 循环中的i
不对应。 I thought that this might be the reason for your issue.我认为这可能是您的问题的原因。 In this case, how about the following modification?
在这种情况下,如何进行以下修改?
for ( i = 29 ; i < lastRow - 1; i++){
if(rangeColors[i][8] === '#ffff00'){
sheet.getRange(i,8).clearContent();
};
};
for (i = 0; i < rangeColors.length; i++) {
if (rangeColors[i][0] === '#ffff00') {
sheet.getRange(i + 29, 8).clearContent();
}
}
or, in your situation, when sheet.getRange(i + 29, 8).clearContent()
is used at outside of the loop, the process cost might be able to be reduced a little.或者,在您的情况下,当
sheet.getRange(i + 29, 8).clearContent()
在循环外使用时,处理成本可能会降低一点。 In this case, please modify as follows.在这种情况下,请进行如下修改。
var rangeList = rangeColors.reduce((ar, [h], i) => {
if (h === '#ffff00') ar.push("H" + (i + 29));
return ar;
}, []);
sheet.getRangeList(rangeList).clearContent();
#ffff00
, the cell content is clear.#ffff00
时,单元格内容清晰。 What you did wrong你做错了什么
In rangeColors[i][8]
you were assuming rangeColors
related to the cells in the spreadsheet.在
rangeColors[i][8]
中,您假设rangeColors
与电子表格中的单元格相关。 But they actually relate to the searchRange
you made.但它们实际上与您所做的
searchRange
相关。 So rangeColors[i][8]
should be rangeColors[i][0]
.所以
rangeColors[i][8]
应该是rangeColors[i][0]
。 Where i
starts at 0
. i
从0
开始。
To make it "extra fun" for you rangeColors
is an array which is zero-based indexed.为了让你“额外有趣”
rangeColors
是一个从零开始索引的数组。 And when you are working with a range you can retreive the first cell with range.getCell(1, 1)
, not range.getCell(0, 0)
.当您使用范围时,您可以使用
range.getCell(1, 1)
检索第一个单元格,而不是range.getCell(0, 0)
。
Because I got a little carried away I wrote this solution ( may be an interesting extra to read )因为我有点忘乎所以我写了这个解决方案(可能是一个有趣的额外阅读)
I don't think mapping an array of arrays ( rangeColors
) to a range is a great way to do this;我不认为将 arrays (
rangeColors
)数组映射到一个范围是一个很好的方法。 It's hard to keep track on what your code is doing.很难跟踪您的代码在做什么。
What I did was:我所做的是:
range.getCell(1, 1)
is the top left cell in the range .range.getCell(1, 1)
是range 中左上角的单元格。 This is the (main) mistake you made in your code. code:代码:
resetCells( '#ffff00' );
function resetCells( color ) {
const app= SpreadsheetApp.getActiveSpreadsheet();
const sheet = app.getSheetByName('Sheet1');
const range = sheet.getRange('F4:F50');
for( let row = 1; row < range.getNumRows() + 1; row++ ) {
for( let column = 1; column < range.getNumColumns() + 1; column++ ) {
const cell = range.getCell( row, column );
if( cell.getBackground() === color )
cell.clearContent();
}
}
}
Disclaimer: I never used this google-app-script before.免责声明:我以前从未使用过这个 google-app-script。 So I may have done something wrong performance-wise;
所以我可能在性能方面做错了什么; iterating a large amount of cells takes a little long;
迭代大量单元格需要一点时间;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.