Google查询语言-搜索整个表

时间:2019-05-23 17:55:04

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

我有一个spreadsheet,其中包含许多数据列。我正在使用查询检索该数据。

如果我说

query.setQuery("select A where D contains 'Ambassador'");

按预期,我只有第一列,第四列包含我要搜索的数据。

但是,如果我说

query.setQuery("select A where * contains 'Ambassador'");

我得到一个错误,

  

查询错误:INVALID_QUERY无效查询:PARSE_ERROR:遇到   第1行第16列中的“” “” “”

如何在整个数据库中搜索所需的文本?是说“ B包含...或C包含...的地方”的唯一方法吗?

(是的,我知道目前电子表格的优化程度不高。一旦整理出搜索内容,我打算修复该问题。)

1 个答案:

答案 0 :(得分:0)

使用TextFinder查找大使

这些都是使用textfinder在电子表格中包含“大使”的所有位置。搜索非常快。

Sheet: Crew_Traits Cell: D1 Row: 1 Col: 4 Value: Ambassador
Sheet: Crew_Traits Cell: D13 Row: 13 Col: 4 Value: Ambassador
Sheet: Crew_Traits Cell: A30 Row: 30 Col: 1 Value: Ambassador Fox
Sheet: Crew_Traits Cell: A31 Row: 31 Col: 1 Value: Ambassador K'Ehleyr
Sheet: Crew_Traits Cell: D31 Row: 31 Col: 4 Value: Ambassador
Sheet: Crew_Traits Cell: A32 Row: 32 Col: 1 Value: Ambassador Neelix
Sheet: Crew_Traits Cell: D32 Row: 32 Col: 4 Value: Ambassador
Sheet: Crew_Traits Cell: A33 Row: 33 Col: 1 Value: Ambassador Sarek
Sheet: Crew_Traits Cell: A34 Row: 34 Col: 1 Value: Ambassador Shras
Sheet: Crew_Traits Cell: D34 Row: 34 Col: 4 Value: Ambassador
Sheet: Crew_Traits Cell: A35 Row: 35 Col: 1 Value: Ambassador Soval
Sheet: Crew_Traits Cell: A36 Row: 36 Col: 1 Value: Ambassador Spock
Sheet: Crew_Traits Cell: A37 Row: 37 Col: 1 Value: Ambassador Troi
Sheet: 1* Cell: A31 Row: 31 Col: 1 Value: Ambassador Fox
Sheet: 1* Cell: A32 Row: 32 Col: 1 Value: Ambassador K'Ehleyr
Sheet: 1* Cell: A33 Row: 33 Col: 1 Value: Ambassador Neelix
Sheet: 1* Cell: A34 Row: 34 Col: 1 Value: Ambassador Sarek
Sheet: 1* Cell: A35 Row: 35 Col: 1 Value: Ambassador Shras
Sheet: 1* Cell: A36 Row: 36 Col: 1 Value: Ambassador Soval
Sheet: 1* Cell: A37 Row: 37 Col: 1 Value: Ambassador Spock
Sheet: 1* Cell: A38 Row: 38 Col: 1 Value: Ambassador Troi
Sheet: 2* Cell: A31 Row: 31 Col: 1 Value: Ambassador Fox
Sheet: 2* Cell: A32 Row: 32 Col: 1 Value: Ambassador K'Ehleyr
Sheet: 2* Cell: A33 Row: 33 Col: 1 Value: Ambassador Neelix
Sheet: 2* Cell: A34 Row: 34 Col: 1 Value: Ambassador Sarek
Sheet: 2* Cell: A35 Row: 35 Col: 1 Value: Ambassador Shras
Sheet: 2* Cell: A36 Row: 36 Col: 1 Value: Ambassador Soval
Sheet: 2* Cell: A37 Row: 37 Col: 1 Value: Ambassador Spock
Sheet: 2* Cell: A38 Row: 38 Col: 1 Value: Ambassador Troi
Sheet: 3* Cell: A30 Row: 30 Col: 1 Value: Ambassador Fox
Sheet: 3* Cell: A31 Row: 31 Col: 1 Value: Ambassador K'Ehleyr
Sheet: 3* Cell: A32 Row: 32 Col: 1 Value: Ambassador Neelix
Sheet: 3* Cell: A33 Row: 33 Col: 1 Value: Ambassador Sarek
Sheet: 3* Cell: A34 Row: 34 Col: 1 Value: Ambassador Shras
Sheet: 3* Cell: A35 Row: 35 Col: 1 Value: Ambassador Soval
Sheet: 3* Cell: A36 Row: 36 Col: 1 Value: Ambassador Spock
Sheet: 3* Cell: A37 Row: 37 Col: 1 Value: Ambassador Troi
Sheet: 4* Cell: A29 Row: 29 Col: 1 Value: Ambassador K'Ehleyr
Sheet: 4* Cell: A30 Row: 30 Col: 1 Value: Ambassador Sarek
Sheet: 4* Cell: A31 Row: 31 Col: 1 Value: Ambassador Shras
Sheet: 4* Cell: A32 Row: 32 Col: 1 Value: Ambassador Soval
Sheet: 4* Cell: A33 Row: 33 Col: 1 Value: Ambassador Spock
Sheet: 4* Cell: A34 Row: 34 Col: 1 Value: Ambassador Troi
Sheet: 5* Cell: A19 Row: 19 Col: 1 Value: Ambassador Soval
Sheet: 5* Cell: A20 Row: 20 Col: 1 Value: Ambassador Troi
Sheet: Crew_URLs Cell: A31 Row: 31 Col: 1 Value: Ambassador Fox
Sheet: Crew_URLs Cell: B31 Row: 31 Col: 2 Value: /wiki/Ambassador_Fox
Sheet: Crew_URLs Cell: A32 Row: 32 Col: 1 Value: Ambassador K'Ehleyr
Sheet: Crew_URLs Cell: B32 Row: 32 Col: 2 Value: /wiki/Ambassador_K%27Ehleyr
Sheet: Crew_URLs Cell: A33 Row: 33 Col: 1 Value: Ambassador Neelix
Sheet: Crew_URLs Cell: B33 Row: 33 Col: 2 Value: /wiki/Ambassador_Neelix
Sheet: Crew_URLs Cell: A34 Row: 34 Col: 1 Value: Ambassador Sarek
Sheet: Crew_URLs Cell: B34 Row: 34 Col: 2 Value: /wiki/Ambassador_Sarek
Sheet: Crew_URLs Cell: A35 Row: 35 Col: 1 Value: Ambassador Shras
Sheet: Crew_URLs Cell: B35 Row: 35 Col: 2 Value: /wiki/Ambassador_Shras
Sheet: Crew_URLs Cell: A36 Row: 36 Col: 1 Value: Ambassador Soval
Sheet: Crew_URLs Cell: B36 Row: 36 Col: 2 Value: /wiki/Ambassador_Soval
Sheet: Crew_URLs Cell: A37 Row: 37 Col: 1 Value: Ambassador Spock
Sheet: Crew_URLs Cell: B37 Row: 37 Col: 2 Value: /wiki/Ambassador_Spock
Sheet: Crew_URLs Cell: A38 Row: 38 Col: 1 Value: Ambassador Troi
Sheet: Crew_URLs Cell: B38 Row: 38 Col: 2 Value: /wiki/Ambassador_Troi
Sheet: Crew_Basics Cell: A31 Row: 31 Col: 1 Value: Ambassador Fox
Sheet: Crew_Basics Cell: A32 Row: 32 Col: 1 Value: Ambassador K'Ehleyr
Sheet: Crew_Basics Cell: A33 Row: 33 Col: 1 Value: Ambassador Neelix
Sheet: Crew_Basics Cell: A34 Row: 34 Col: 1 Value: Ambassador Sarek
Sheet: Crew_Basics Cell: A35 Row: 35 Col: 1 Value: Ambassador Shras
Sheet: Crew_Basics Cell: A36 Row: 36 Col: 1 Value: Ambassador Soval
Sheet: Crew_Basics Cell: A37 Row: 37 Col: 1 Value: Ambassador Spock
Sheet: Crew_Basics Cell: A38 Row: 38 Col: 1 Value: Ambassador Troi

这是代码: sObj.ssid是电子表格的ID sObj.pattern是规则表达式,在这种情况下,它是“ Ambassador”,不带引号。

function regexSearch(sObj) {
  var ss=SpreadsheetApp.openById(sObj.ssid);
  var tf=ss.createTextFinder(sObj.pattern).useRegularExpression(true);
  var all=tf.findAll();
  var matches='';
  for(var i=0;i<all.length;i++) {
    matches+=Utilities.formatString('Sheet: %s Cell: %s Row: %s Col: %s Value: %s\n',all[i].getSheet().getName(),all[i].getA1Notation(),all[i].getRow(),all[i].getColumn(),all[i].getValue());
  }
  if(matches) {
    sObj.matches=matches;
  }else{
    sObj.message(Utilities.formatString('No Matches found for %s',sObj.pattern));
  }
  return sObj;
}

通过对上面的代码进行一些小调整,我们得到了以下结果:

Sheet: Crew_Traits Row: 13 Col: 4 Value: Ambassador columnA Value: Admiral Picard
Sheet: Crew_Traits Row: 31 Col: 4 Value: Ambassador columnA Value: Ambassador K'Ehleyr
Sheet: Crew_Traits Row: 32 Col: 4 Value: Ambassador columnA Value: Ambassador Neelix
Sheet: Crew_Traits Row: 34 Col: 4 Value: Ambassador columnA Value: Ambassador Shras

现在这是代码:

function regexSearch() {
  var ss=SpreadsheetApp.openById('1I3N5DtdXGWFootaOCQM201K_ao2ZPWSWyw9_l7QcwQg');
  var tf=ss.createTextFinder('Ambassador').useRegularExpression(true);
  var all=tf.findAll();
  var matches='';
  for(var i=0;i<all.length;i++) {
    if(all[i].getColumn()==4) {
      var aValue=all[i].getSheet().getRange(all[i].getRow(),1).getValue();
      matches+=Utilities.formatString('Sheet: %s Row: %s Col: %s Value: %s columnA Value: %s\n',all[i].getSheet().getName(),all[i].getRow(),all[i].getColumn(),all[i].getValue(),aValue);
    }
  }
  Logger.log(matches);
  return matches;  
}