在DriveApp searchFiles查询中使用变量的值 [英] Use variable's value in DriveApp searchFiles query

查看:91
本文介绍了在DriveApp searchFiles查询中使用变量的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能有一个愚蠢的问题,但找不到解决方法

I may have silly question but I can't find a solution

我在Google驱动器中浏览了很多文件,每个文件中都有一个0-10000范围内的数字,所以我做到了:

I have go through a lot of files in my google drive and inside every file there is a number within 0-10000 range, so I made this:

for(var i = 0; i < 10000; i++)
{
  var test = "IT" + i;
  Logger.log(test);
  var search = DriveApp.getFolderById("ID")
                       .searchFiles('fullText contains test');
  Logger.log(search);
  while (search.hasNext()) {
    var file = search.next();
    if(search.hasNext())
      break;
    Logger.log(file.getName());
  }
  ...

我的问题是,如何放置这样的变量:

My question is, how can I put a variable like this:

.searchFiles('fullText contains $variable');

以便我可以使用变量test的值,即"IT0",而不是文字字符串test?

so that I can use the value of the variable test, i.e. "IT0", rather than the literal string test?

推荐答案

每个 Drive REST API文档:

  • contains运算符仅对fullText的整个字符串标记执行匹配.例如,如果文档的全文包含字符串 HelloWorld ,则仅查询fullText contains 'HelloWorld'返回结果.在这种情况下,诸如fullText contains 'Hello'之类的查询不会返回结果.
  • 如果contains运算符被双引号括起来,则匹配精确的字母数字短语.例如,如果文档的fullText包含字符串 Hello there world ,则查询fullText contains '"Hello there"'将返回结果,但查询fullText contains '"Hello world"'不会.此外,由于搜索是字母数字,因此如果文档的fullText包含字符串 Hello_world ,则查询fullText contains '"Hello world"'将返回结果.
  • The contains operator only performs matching on entire string tokens for fullText. For example, if the full text of a doc contains the string HelloWorld only the query fullText contains 'HelloWorld' returns a result. Queries such as fullText contains 'Hello' do not return results in this scenario.
  • The contains operator matches on an exact alphanumeric phrase if it is surrounded by double quotes. For example, if the fullText of a doc contains the string Hello there world, then the query fullText contains '"Hello there"' will return a result, but the query fullText contains '"Hello world"' will not. Furthermore, since the search is alphanumeric, if the fullText of a doc contains the string Hello_world, then the query fullText contains '"Hello world"' will return a result.

评论中,您提到您尝试了searchFiles('fullText contains "IT"' + test),该搜索不符合要求的搜索子句语法-评估结果类似于fullText contains "IT"0(或fullText contains "IT"IT0,具体取决于您是否更改了其他代码部分)而不是fullText contains "IT0"

In comments you mention you tried searchFiles('fullText contains "IT"' + test) which does not conform to the required search clause syntax - it evaluates to something like fullText contains "IT"0 (or fullText contains "IT"IT0, depending on if you changed your other code parts) instead of fullText contains "IT0"

调试基于变量的查询中的错误时的一种好习惯是先执行构造操作(以便您可以准确地记录查询),然后实际执行查询:

A good practice when debugging errors in variable-based queries is to first perform the construction (so you can accurately log the query) and then actually do the query:

var myQuery = "fullText contains \"" + myVar + "\"";
// var mySingleQuotedQuery = "fullText contains '" + myVar + "'";
try {
  /* use myQuery */
}
catch (err) {
  err.message = "Error using 'myQuery': " + err.message;
  // Log debug info to StackDriver:
  console.error({message: err.message, error: err, query: myQuery, variable: myVar});
  throw err;
}

这篇关于在DriveApp searchFiles查询中使用变量的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆