2017-10-21 98 views
0

我想写一个谷歌床单脚本来搜索一系列我的云端硬盘文件的变量搜索词。只要我一直将我想要使用“Joshs文件”进行搜索的文件授予我的权限,我的代码就可以正常工作。但我希望能够通过在我的工作表的单元格B1中输入的可变搜索词搜索fullText。我用什么语法来做到这一点?如何编写Google表格脚本,用于在我的表格的单元格B1中搜索我的驱动器文件中的变量搜索项?

fullText包含什么?

function SearchFiles() { 
    var sheet = SpreadsheetApp.getActiveSpreadsheet(); 
    var sh0 = sheet.getSheets()[0]; 
var search0 = sh0.getRange("B1").getValue(); 

    //Please enter your search term in the place of Letter 
    var searchFor = 'title contains "Joshs File"' + 'and fullText contains "X"'; 
var names =[]; 
    var fileIds=[]; 
    var files = DriveApp.searchFiles(searchFor); 
    while (files.hasNext()) { 
    var file = files.next(); 
    var name = file.getName(); 
    names.push(name); 

    } 

    for (var i=0;i<names.length;i++){ 
    Logger.log(names[i]); 


    } 

    var body = Logger.getLog(); 
var range=sh0.getRange("A5"); 
range.setValue(body); 

} 

回答

0

尝试使用执行这段代码:

function SearchFiles(){ 
var textString = "Sample"; 

var files = DriveApp.getFolderById(FolderID).searchFiles( 
     'fullText contains "'+textString+'"'); 
     while (files.hasNext()) { 
      var file = files.next(); 
      Logger.log(file.getName()); 
     } 

} 

就像你如何concatenate a variable and string in JavaScript你必须使用“+”,以便它被认为是一个变量。

希望这会有所帮助。