[英]Delete a row in google spreadsheet based on value contained in a cell in row
So this is a question that has been asked before, with a slightly different twist: I need to delete rows in a google sheet based on a value contained within a cell in that row. 因此,这是之前提出的一个问题,但略有不同:我需要根据该行单元格中包含的值删除google工作表中的行。
The issue is previous answers I have found allow me to do this only if the value is exact. 问题是我发现的以前的答案仅允许值正确时才允许执行此操作。 I have a LET AGREED phrase which occasionally appears but it comes with other text in the cell as well.
我有一个LET AGREED词组,该词组偶尔出现,但单元格中还包含其他文本。 I want to delete any rows in which the phrase LET AGREED appears.
我想删除出现短语LET AGREED的所有行。
Two solutions I found which came very close: 我发现了两个非常接近的解决方案:
Delete a row in google spreadsheet base on value of cell in row 根据行中单元格的值在Google电子表格中删除一行
Delete a row in google spreadsheet base on contents of cell in row 根据行中单元格的内容在Google电子表格中删除一行
Any help appreciated, I'm not great with Google Scripts! 任何帮助表示赞赏,我对Google脚本不满意!
I assume you want this to happen whenever something is edited? 我假设您希望每当进行某些编辑时都会发生这种情况? The .search() method can be called strings to see if a substring is present.
可以将.search()方法称为字符串,以查看是否存在子字符串。 If it is, it returns the index of where it was found.
如果是,它将返回找到它的位置的索引。 If it is not found, it returns -1.
如果找不到,则返回-1。
This is an onEdit trigger, and it will get called the moment anything on your sheet is changed. 这是一个onEdit触发器,当工作表中的任何内容更改时都会被调用。 So the moment the text "LET AGREED" is saved in a cell, the row will disappear.
因此,一旦文本“ LET AGREED”保存在单元格中,该行就会消失。
Also, if you are copy and pasting into this sheet a range of data where the words "LET AGREED" will be present in many rows, it will be able to handle deleting all of those rows. 同样,如果您要复制并粘贴到此表中的一系列数据,其中“ LET AGREED”一词将出现在许多行中,它将能够处理所有这些行的删除。
function onEdit(e) {
var ss = e.source;
var sheet = ss.getSheets()[0];
var range = sheet.getDataRange();
var values = range.getValues();
var rows_deleted = 0;
for (var i = 0; i < values.length; i++) {
for (var j = 0; j < values[i].length; j++) {
var value = values[i][j];
//row numbers are 1-based, not zero-based like this for-loop, so we add one AND...
//every time we delete a row, all of the rows move down one, so we will subtract this count
var row = i + 1 - rows_deleted;
//if the type is a number, we don't need to look
if (typeof value === 'string') {
var result = value.search("LET AGREED");
//the .search() method returns the index of the substring, or -1 if it is not found
//we only care if it is found, so test for not -1
if (result !== -1) {
sheet.deleteRow(row);
rows_deleted++;
}
}
}
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.