[英]How to delete rows fast if they have empty values at specific columns in Google App Script
below is a code that checks if the cells at columns [G, H, J] are empty and delete the row where the the condition is true.下面的代码检查 [G, H, J] 列的单元格是否为空并删除条件为真的行。 Nevertheless, the runtime of the code below is extremely slow, needs approx 15 minutes per 1500 table entries.然而,下面代码的运行时间非常慢,每 1500 个表条目需要大约 15 分钟。 is there any way to optimise it ?有什么办法可以优化吗? can I hash the rows that meet the condition below and then delete them all at once?我可以对满足以下条件的行进行哈希处理,然后一次将它们全部删除吗?
PS: the original code can be found here https://gist.github.com/dDondero/285f8fd557c07e07af0e which I adapted it to my use case. PS:可以在此处找到原始代码https://gist.github.com/dDondero/285f8fd557c07e07af0e我将其改编为我的用例。
function deleteRows() {
var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getDataRange();
var lastRow =sheet.getRange(1,1).getDataRegion(SpreadsheetApp.Dimension.ROWS).getLastRow() + 1;
var values = rows.getValues();
var row;
var rowsDeleted = 0;
for (var i = 0; i < lastRow; i++) {
row = values[i];
if (row[9] == '' && row[7] == '' && row[6] == ''
) {
sheet.deleteRow((parseInt(i)+1) - rowsDeleted);
rowsDeleted++;
}
}
}
function DeleteEmptyRows() {
const sheet = SpreadsheetApp.getActiveSheet()
const values = sheet.getDataRange()
.getValues()
.filter(row => row[9] !== '' && row[7] !== '' && row[6] !== '')
sheet.getDataRange().clearContent()
return sheet.getRange(1, 1, values.length, values[0].length)
.setValues(values)
}
If you have any background colors attached to rows, let me know and I can make an adjustment for you.如果您在行上附加了任何背景颜色,请告诉我,我可以为您进行调整。
This code will filter out all rows with the empty cells specified, clear the sheet, and then set the values.此代码将过滤掉所有指定空单元格的行,清除工作表,然后设置值。
function deleteRows() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const rg = sh.getDataRange();
const vs = rg.getValues().filter(r => !(!r[6] || !r[7] || !r[9]));
rg.clearContent();
sh.getRange(1, 1, vs.length, vs[0].length).setValues(vs);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.