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

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

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

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

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

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

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

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

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

最佳答案

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

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()
     }

关于google-apps-script - 搜索整个电子表格以查找单元格内的文本,然后打印整行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11095888/

相关文章:

google-sheets - 通过 Google 表格中的单元格引用将 Query 和 Arrayformula 结合起来

javascript - 如何计算不同日期的 24 小时格式的两个时间之间的差异?

google-apps-script - 使用 GAS 解压缩 gz 文件抛出错误异常 : Invalid argument

javascript - 空单元格导致代码中断 For 循环 - 从电子表格生成 Google 文档

google-sheets - 选择日期列时,Google 表格中的 GETPIVOTDATA 似乎不起作用(#REF 错误)

google-sheets - Google表格查询不会显示所有值

regex - 从电子表格中单元格的值解析字符串和对象

google-apps-script - 如何使用 GAS 中的高级 Gmail 服务发送 Gmail 邮件?

google-sheets - 使用正则表达式提取进行 Vlookup - Google 表格

google-sheets - 如何计算 2 列中的唯一对并使用 Google 表格中的 ArrayFormula 进行排序?