Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google Sheet脚本中listFilesByName的排序结果_Google Apps Script_Google Sheets_Google Drive Api - Fatal编程技术网

Google apps script Google Sheet脚本中listFilesByName的排序结果

Google apps script Google Sheet脚本中listFilesByName的排序结果,google-apps-script,google-sheets,google-drive-api,Google Apps Script,Google Sheets,Google Drive Api,我在谷歌工作表上做了一个标签,用来跟踪特定文件夹中的文件。我已经成功地修改了我在网上找到的一个脚本,以按文件夹id获取列表,但我似乎不知道如何按名称顺序显示结果。下面是我正在使用的代码,但我将文件夹id替换为myFolderId: /** * List all files in Google Drive folder. * * @param {string} folderName (optional) Name of folder on Google Drive * * Ada

我在谷歌工作表上做了一个标签,用来跟踪特定文件夹中的文件。我已经成功地修改了我在网上找到的一个脚本,以按文件夹id获取列表,但我似乎不知道如何按名称顺序显示结果。下面是我正在使用的代码,但我将文件夹id替换为myFolderId:

 /**
 * List all files in Google Drive folder.
 *
 * @param {string} folderName    (optional) Name of folder on Google Drive
 *
 * Adapted from:
 * http://ctrlq.org/code/19854-list-files-in-google-drive-folder
 * https://gist.github.com/hubgit/3755293
 */
function listFilesInFolder(id) {
  // If we have not been provided a folderName, assume we will interact with     user.
  var interactive = (typeof folderName === 'undefined');

  // Get name of folder to list
  if (interactive) {
    id = 'myFolderId';
  }

  if (id === '')
    return;  // No name provided, exit quietly

  var folder = DriveApp.getFolderById(id);
  var contents = folder.getFiles();

  var file, data, sheet = SpreadsheetApp.getActiveSheet();
  sheet.clear();

  sheet.appendRow(["Name", "Date"]);

  // Loop over files in folder, using file iterator
  while (contents.hasNext()) {
    file = contents.next();

    if (file.getMimeType() == MimeType.GOOGLE_SHEETS) { // "SPREADSHEET"
      // Skip displaying spreadsheets - I don't know why...
      continue;
    }

    data = [ 
      file.getName(),
      file.getDateCreated(),
    ];

    sheet.appendRow(data);
  }
}

第一个选项,对工作表进行排序

第二种选择是,我试图对脚本进行注释,以便您理解这些步骤

/*
 * Logs in a SpreadSheet the files of a given folder
  * @param {string} folder id
 */

function listFilesInFolder(id){
  // You can change it and get ss from a given ID
  var sheet = SpreadsheetApp.getActiveSheet();

  // array to hold our data
  var data = [];

  // An iterator that allows scripts to iterate over a potentially large collection of files
  var files = DriveApp.getFolderById(id).getFiles();

  // We loop on iterator and append results to an array
  while (files.hasNext()) {
   var file = files.next();
   // we append our data with result, we have now an array of files
   data.push(file);
  }

  // lets sort our array
  data = data.sort(function(file1, file2){
      if (file1.getName().toLowerCase() < file2.getName().toLowerCase())
          return -1;
      else if (file1.getName().toLowerCase() > file2.getName().toLowerCase())
          return 1;
      else 
        return 0;
    }
  )

  // lets add it to our sheet
  // some labels
  sheet.appendRow(["Name", "Date"]);
  // real data
  data.forEach(function(file){
    sheet.appendRow([file.getName(), file.getDateCreated()])
  })
}
/*
*在电子表格中记录给定文件夹的文件
*@param{string}文件夹id
*/
函数ListFileInfolder(id){
//您可以更改它并从给定ID获取ss
var sheet=SpreadsheetApp.getActiveSheet();
//数组来保存我们的数据
var数据=[];
//一种迭代器,允许脚本在可能较大的文件集合上进行迭代
var files=DriveApp.getFolderById(id).getFiles();
//我们在迭代器上循环并将结果附加到数组中
while(files.hasNext()){
var file=files.next();
//我们用结果附加数据,现在我们有了一个文件数组
数据推送(文件);
}
//让我们对数组进行排序
data=data.sort(函数(file1,file2){
if(file1.getName().toLowerCase()file2.getName().toLowerCase())
返回1;
其他的
返回0;
}
)
//让我们把它添加到我们的工作表中
//一些标签
表.附录行([“名称”,“日期]);
//真实数据
data.forEach(函数(文件){
sheet.appendRow([file.getName(),file.getDateCreated()]))
})
}

在末尾添加这一行

//for sorting the Name column    
sheet.sort(1); 

谢谢我现在只是在将结果放入表格后对其进行排序。我只是想要一个更优雅的解决方案,可以消除额外的步骤。你的解决方案对我来说很有意义(评论很好,谢谢),所以我会在我有机会再做这个小项目时尝试一下。再次感谢。