Google apps script 气体-发现空电池相当慢
我在谷歌应用程序脚本中有以下代码。它在800多行中循环查找一列的空单元格。但这需要很长时间。有没有办法加快速度Google apps script 气体-发现空电池相当慢,google-apps-script,Google Apps Script,我在谷歌应用程序脚本中有以下代码。它在800多行中循环查找一列的空单元格。但这需要很长时间。有没有办法加快速度 for(var t = 2; t <= lastrow; t++) { if (Geojson_Data.getRange(t,col1).getValue() == "") { Geojson_Data.deleteRow(t); } } for(var t=2;t函数delrowswithnothingcola(){ var
for(var t = 2; t <= lastrow; t++)
{
if (Geojson_Data.getRange(t,col1).getValue() == "")
{
Geojson_Data.deleteRow(t);
}
}
for(var t=2;t函数delrowswithnothingcola(){
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getRange(2,1,sh.getLastRow()-1,1);
var vA=rg.getValues();
var d=0;
对于(var i=0;i
如果您需要一个真正的工具,那么请查看脚本,它在不更改行队列和不销毁公式的情况下批量删除行
/**
* Runs the snippet.
* Removes rows by condition 'A:A=""'.
* @ignore
*/
function run() {
var sheet = SpreadsheetApp.getActiveSheet();
deleteRowsByConditional_(sheet, function(row) {
return row[0] === '';
});
}
function deleteRowsByConditional_(sheet, condition, action) {
var values = sheet.getDataRange().getValues();
values.unshift([]);
values.reverse().forEach(
function() {
var i = this.l - arguments[1];
if (this.condition.apply(null, [arguments[0], i, arguments[2]])) {
this.isContinue++;
} else if (this.isContinue) {
if (action) action(arguments[2], i, this.isContinue);
this.sheet.deleteRows(i, this.isContinue);
this.isContinue = 0;
}
},
{ sheet: sheet, condition: condition, isContinue: 0, l: values.length }
);
}
尝试优化代码,使您不会在循环中调用诸如getRange(…)
之类的方法。一次性获取所有数据(通过getValues()
),将数据作为数组处理,然后一次性更新工作表(通过setValues()
)。使用当前的实现,在循环的每次迭代中,您都在读取和更新工作表,这是非常低效的。可能的重复只是为了添加到响应中,此代码假设您正在查看列A
,以指定只需更改getRange(2,1,sh.getLastRow()-1,1)
到getRange(2,col1,sh.getLastRow()-1,1);
注意,这只是为了更改要检查的列,答案中提供的代码是完全正确的。
var values = Geojson_Data.getDataRange().getValues();
var col1Arr = col1 - 1;
var t = values.length;
while(t--)
if(values[t][col1Arr] === '')
Geojson_Data.deleteRow(t + 1);
/**
* Runs the snippet.
* Removes rows by condition 'A:A=""'.
* @ignore
*/
function run() {
var sheet = SpreadsheetApp.getActiveSheet();
deleteRowsByConditional_(sheet, function(row) {
return row[0] === '';
});
}
function deleteRowsByConditional_(sheet, condition, action) {
var values = sheet.getDataRange().getValues();
values.unshift([]);
values.reverse().forEach(
function() {
var i = this.l - arguments[1];
if (this.condition.apply(null, [arguments[0], i, arguments[2]])) {
this.isContinue++;
} else if (this.isContinue) {
if (action) action(arguments[2], i, this.isContinue);
this.sheet.deleteRows(i, this.isContinue);
this.isContinue = 0;
}
},
{ sheet: sheet, condition: condition, isContinue: 0, l: values.length }
);
}