【问题标题】:Search whole spreadsheet to find text within a cell and then print the whole row搜索整个电子表格以在单元格中查找文本,然后打印整行
【发布时间】:2012-06-19 06:59:36
【问题描述】:

我有一个包含多行和多列数据的 Google 表格,我想在电子表格的每一行中搜索某个短语,然后如果该行包含其中一个中的短语,则将该行打印到新工作表上数据列(比如说 D 列)。

我的包含所有数据的工作表被创造性地称为“所有数据”。同样,我只包含所选数据的工作表称为“所选数据”。 假设我正在寻找的短语是“你好”。

目前在“选定数据”表中,我在单元格 A1 中有以下内容:

=QUERY('All the data'!A:D,"select find("hello",All the data!D:D)>0")

这会产生解析错误,我希望它打印“所有数据”表中包含“所有数据”表中 D 列中“hello”的所有行。

我做错了什么?我是否使用了正确的功能?还有其他更容易使用的功能吗?正确的公式是什么?

最终我想将其转换为 Google Apps 脚本。

【问题讨论】:

  • 由于你的最后一句话,我不会写这个作为答案,但电子表格函数解决方案可能是:=QUERY('All the data'!A:D;"select * where D contains 'hello'")。请注意,这将在“奥赛罗”中找到“你好”。
  • 感谢您的快速回复亚当。您的评论足以让我成为答案。如果您想这样发布,我会将其标记为已回答。我将尝试自己转换为 Google Apps 脚本。

标签: google-apps-script google-sheets google-sheets-query


【解决方案1】:

这是一个您可以尝试的“脚本解决方案”。有很多可能的方法,这是其中之一...

function testfindRow(){ ;// this function only to test the other one by giving it a parameter and using its result.
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var otherSheet=ss.getSheets()[1];// assuming there is a second sheet to copy to
        var datatocopy=findRow('Hello');
        Logger.log(datatocopy)
        if (datatocopy!=-1){
        otherSheet.getRange(otherSheet.getLastRow()+1,1,1,datatocopy[0].length).setValues(datatocopy);
        }
 }
//
function findRow(item) { ;// the actual search function
        var ss = SpreadsheetApp.getActiveSpreadsheet();
        var sheet=ss.getSheets()[0];
        var values = sheet.getDataRange().getValues();
          for(cc =0; cc < values.length; ++cc) {
            if(values[cc].toString().match(item)==item){break};// here you can eventually use string modifiers like toLowerCase() to allow for wider search criteria
             }
        Logger.log(cc);// the array index in which the result was found, cc+1 is the Row index
        if (cc==values.length){return -1}
         var resultArray=sheet.getRange(cc+1,1,1,values[cc].length).getValues()
         return resultArray ;// the returned value is a 2 dimensions array to use with setValues()
 }

根据您的评论,这是一个返回包含该项目的所有行的版本,我还必须更改错误捕获,但毕竟整个事情都非常简单。

    function testfindRow(){ ;// this function only to test the other one by giving it a parameter and using its result.
            var ss = SpreadsheetApp.getActiveSpreadsheet();
            var otherSheet=ss.getSheets()[1];// assuming there is a second sheet to copy to
            var datatocopy=findRow('Hello');
            if (datatocopy.length>0){
            otherSheet.getRange(otherSheet.getLastRow()+1,1,datatocopy.length,datatocopy[0].length).setValues(datatocopy);
            }
     }
    //
    function findRow(item) { ;// the actual search function
            var ss = SpreadsheetApp.getActiveSpreadsheet();
            var sheet=ss.getSheets()[0];
            var resultArray=new Array();
            var values = sheet.getDataRange().getValues();
              for(cc =0; cc < values.length; ++cc) {
                if(values[cc].toString().match(item)==item){// here you can eventually use string modifiers like toLowerCase() to allow for wider search criteria
// or like this to search only in column D // if(values[cc][3].toString().match(item)==item){
                resultArray.push(values[cc]);
                            };
                 }
            Logger.log(resultArray);// the array of Rows in which the item was found, 
            return resultArray ;// the returned value is a 2 dimensions array to use with setValues()
     }

【讨论】:

  • 也感谢 Serge 的帮助。不幸的是,这似乎对我不起作用。我认为它似乎只返回其中一列(电子表格中的第一列),如果找不到该项目,则会出现“越界”错误。我假设“越界”错误是因为未找到搜索的术语。当找到搜索词时,它似乎复制了 A 列的全部内容,而不仅仅是找到匹配项的行。
  • 抱歉,我现在添加了错误捕获并更正了一些错误。它应该按预期工作。
  • 谢谢谢尔盖!您的编辑使其工作,现在唯一的事情是它只选择第一行包含这个值(“你好”),而不是每一行。我将如何更改它以显示每一行?
  • 好问题!我会看看我能找到什么并随时通知你:-)
  • 知道了...我想这就是你需要的,不是吗?
【解决方案2】:

电子表格函数的解决方案是:

=QUERY('All the data'!A:D;"select * where D contains 'hello'")

注意:这会在“奥赛罗”中找到“hello”。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多