Как вы получаете папку, откуда файл на Google Диске, для отображения в Google Sheets - PullRequest
0 голосов
/ 30 мая 2018

В настоящее время код, который у меня есть, отображает только имя файла, источник URL-адреса файла и тип файла, я пытался использовать getFolder (), но, похоже, это не работает для меня, это то, что я до сих пор.Любые идеи о том, как лучше всего реализовать функции папок (если необходимо или нет), чтобы отображать в отдельном столбце, где каждый файл из папки?

function search(Phrase, FolderID) {
  // Prompt the user for a search term
  var searchTerm = Browser.inputBox("Enter the string to search for:");

  // Get the active spreadsheet and the active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  // Set up the spreadsheet to display the results
  var headers = [["File Name", "File Type", "URL"]];
  sheet.clear();
  sheet.getRange("A1:C1").setValues(headers);

  // Search the files in the user's Google Drive for the search term based on if the word is included in thefile or name
  // Search Reference Guide: https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)
  var files = DriveApp.searchFiles("fullText contains '"+searchTerm.replace("'","\'")+"'");
  //var SearchString = 'fullText contains "' + Phrase + '" and "' + FolderID + '" in parents';
  //var files = DriveApp.searchFiles(SearchString);
  // create an array to store our data to be written to the sheet 
  var output = [];
  // Loop through the results and get the file name, file type, and URL
  while (files.hasNext()) {
    var file = files.next();

    var name = file.getName();
    var type = file.getMimeType();
    var url = file.getUrl();
    // push the file details to our output array (essentially pushing a row of data)
    output.push([name, type, url]);
  }
  // write data to the sheet
  sheet.getRange(2, 1, output.length, 3).setValues(output);
}

РЕДАКТИРОВАТЬ

function search(Phrase, FolderID) {
  // Prompt the user for a search term
  var searchTerm = Browser.inputBox("Enter the string to search for:");

  // Get the active spreadsheet and the active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  // Set up the spreadsheet to display the results
  var headers = [["File Name", "File Type", "URL", "Folder"]];
  sheet.clear();
  sheet.getRange("A1:D1").setValues(headers);

  // Search the files in the user's Google Drive for the search term based on if the word is included in thefile or name
  // Search Reference Guide: https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)
  var files = DriveApp.searchFiles("fullText contains '"+searchTerm.replace("'","\'")+"'");
  //var SearchString = 'fullText contains "' + Phrase + '" and "' + FolderID + '" in parents';
  //var files = DriveApp.searchFiles(SearchString);
  // create an array to store our data to be written to the sheet 
  var output = [];
  // Loop through the results and get the file name, file type, and URL
  while (files.hasNext()) {
    var file = files.next();

    var name = file.getName();
    var type = file.getMimeType();
    var url = file.getUrl();
    var folderNames = "";
    var folders = file.getParents();
    while (folders.hasNext()) {
        var folder = folders.next();
        Logger.log(folder.getName());
        folderNames += folder.getName() + ", ";
    }
    // push the file details to our output array (essentially pushing a row of data)
    output.push([name, type, url, folders]);
  }
  // write data to the sheet
  sheet.getRange(2, 1, output.length, 4).setValues(output);
}

1 Ответ

0 голосов
/ 30 мая 2018

Вы можете получить доступ к папке файла с помощью метода getParents().
В вашем коде это может выглядеть следующим образом:

while (files.hasNext()) {
    var file = files.next();

    var name = file.getName();
    var type = file.getMimeType();
    var url = file.getUrl();
    var folderNames = "";
    var folders = file.getParents();
    while (folders.hasNext()) {
        var folder = folders.next();
        folderNames += folder.getName() + ", ";
    }
    // push the file details to our output array (essentially pushing a row of data)
    output.push([name, type, url, folderNames]);
  }

Другие методы файла класса можно найти здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...