在Google表格脚本中对listFilesByName的排序结果 [英] Sorting Results of listFilesByName in Google Sheet Script

查看:91
本文介绍了在Google表格脚本中对listFilesByName的排序结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Google表格中创建一个标签,以跟踪特定文件夹中的文件.我已经成功修改了我在网上找到的脚本,以按文件夹ID获取列表,但似乎无法弄清楚如何使结果按名称顺序显示.这是我正在使用的代码,但我将文件夹ID代替了myFolderId:

I am making a tab in a Google Sheet to keep track of files in a particular folder. I have successfully modified a script I found online to get the list by folder id, but I can't seem to figure out how to get the results to appear in order by name. Here is the code I'm using but I'm putting the folder id in place of 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);
  }
}

推荐答案

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

First option, sort the sheet

第二个选项,我尝试对脚本进行注释,以便您理解步骤

Second option, I tried to comment the script so you will understand the steps

/*
 * 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()])
  })
}

要点链接

这篇关于在Google表格脚本中对listFilesByName的排序结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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