Google apps script 搜索整个电子表格以查找单元格中的文本,然后打印整行

Google apps script 搜索整个电子表格以查找单元格中的文本,然后打印整行,google-apps-script,google-sheets,google-sheets-query,Google Apps Script,Google Sheets,Google Sheets Query,我有一个Google Sheets,它包含几行和几列数据,我想在电子表格的每一行中搜索某个短语,然后在新的工作表上打印该行,如果该行包含其中一列数据中的短语(比如D列) 我的包含所有数据的工作表创造性地称为“所有数据”。类似地,仅包含选定数据的工作表称为“选定数据”。 让我们说我要找的短语是“你好” 目前在“选定数据”表中,A1单元格中有以下内容: =QUERY('All the data'!A:D,"select find("hello",All the data!D:D)>0") 这

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

我的包含所有数据的工作表创造性地称为“所有数据”。类似地,仅包含选定数据的工作表称为“选定数据”。 让我们说我要找的短语是“你好”

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

=QUERY('All the data'!A:D,"select find("hello",All the data!D:D)>0")
这会产生一个解析错误,我希望它打印“所有数据”表中的所有行,这些行在“所有数据”表的D列中包含“hello”

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


最后,我想将其转换为谷歌应用程序脚本。

电子表格功能解决方案将是:

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

注意:这将在《奥赛罗》中找到“hello”。

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

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(){;//此函数仅通过给另一个函数一个参数并使用其结果来测试它。
var ss=SpreadsheetApp.getActiveSpreadsheet();
var otherSheet=ss.getSheets()[1];//假设有第二张要复制的工作表
var datatocopy=findRow('Hello');
Logger.log(数据目录)
如果(数据复制!=-1){
otherSheet.getRange(otherSheet.getLastRow()+1,1,1,datatocopy[0].length).setValues(datatocopy);
}
}
//
函数findRow(item){;//实际的搜索函数
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheets()[0];
var values=sheet.getDataRange().getValues();
对于(cc=0;cc
在您的评论之后,这里是一个返回包含该项的所有行的版本,我还必须更改错误捕获,但毕竟整个过程非常简单

    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()
     }
function testfindRow(){;//此函数仅通过给另一个函数一个参数并使用其结果来测试它。
var ss=SpreadsheetApp.getActiveSpreadsheet();
var otherSheet=ss.getSheets()[1];//假设有第二张要复制的工作表
var datatocopy=findRow('Hello');
如果(数据复制长度>0){
otherSheet.getRange(otherSheet.getLastRow()+1,1,datatocopy.length,datatocopy[0].length).设置值(datatocopy);
}
}
//
函数findRow(item){;//实际的搜索函数
var ss=SpreadsheetApp.getActiveSpreadsheet();
var sheet=ss.getSheets()[0];
var resultArray=新数组();
var values=sheet.getDataRange().getValues();
对于(cc=0;cc
由于您的最后一句话,我不会将此作为回答,但电子表格功能解决方案可以是:
=QUERY('All data'!A:D;“select*其中D包含“hello”)
。注意,这将在“Otherlo”中找到“hello”。谢谢你的快速回复。Adam。你的评论对我来说足够好,可以作为答案。如果你想发布它,我会将其标记为已回答。我会尝试自己将其转换为谷歌应用程序脚本。谢谢你Serge的帮助。不幸的是,这似乎对我不起作用。它似乎只返回了一个列ns(电子表格中的第一个)我认为,如果没有找到该项,则会出现“越界”错误。我假设为“越界”错误是因为找不到搜索项。当找到搜索项时,它似乎复制了A列的全部内容,而不仅仅复制了找到匹配项的行。很抱歉,我现在添加了一个错误陷阱,并更正了一些错误。它应该可以正常工作。谢谢Serge!您的编辑使它正常工作,现在唯一的问题是that它只拾取第一行中包含此值(“hello”)的行,而不是每一行。我如何更改它以使其显示每一行?好问题!我将查看我能找到的内容并随时通知您:-)明白了…我想这正是您需要的,不是吗?