简体   繁体   English

根据行中单元格中包含的值在Google电子表格中删除一行

[英]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.

相关问题 Google电子表格根据值0删除行 - Google spreadsheet delete row based on value 0 Google 电子表格:根据该行中的单元格值从范围复制行 - Google spreadsheet :Copying row from a range based on a cell value in that row 如何根据单元格值更改行的背景颜色? -Google电子表格 - How to change background color of row based on cell value? -Google Spreadsheet 根据单元格中的值将行的一部分从一个 Google 电子表格复制到另一个 Google 电子表格? - Copying part of a row from one Google spreadsheet to another google spreadsheet based on a value in a cell? 根据行中单元格的内容删除谷歌电子表格中的一行 - Delete a row in google spreadsheet base on contents of cell in row 用于将Google电子表格行的部分内容复制到另一个电子表格并全部基于单元格添加值的脚本 - Script for copying parts of a google spreadsheet row to another spreadsheet and adding a value all based on a cell 根据特定单元格范围内的值从特定范围内删除谷歌电子表格中的行 - Delete row in google spreadsheet from a specific range based on values from a specific cell range 根据来自另一个特定单元格范围的值从特定范围删除谷歌电子表格中的行 - Delete row in google spreadsheet from a specific range based on values from another specific cell range 在Google电子表格上基于1个单元格移动一行并确认第二个单元格不为空 - Moving a row on google spreadsheet based on 1 cell and verifying second cell is not blank 如何根据单元格中的值将一行从一个Google电子表格移动到另一个? - How to move a row from one google spreadsheet to another based on a value in a cell?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM