简体   繁体   English

使用JavaScript在锚定标记中的HTML链接的alpha排序数组

[英]alpha sort array of HTML links in anchor tags using javascript

Ok - This one involves both javascript and Google Apps Script. 好的-这涉及到javascript和Google Apps脚本。 The problem is with the javascript. 问题出在javascript。

I am creating a list of links to files in a Google Drive folder using the following code: 我正在使用以下代码创建指向Google云端硬盘文件夹中文件的链接列表:

function doGet(e) {
  var template = '<table style =""><tr><th>name</th></tr>APPS_SCRIPT_CONTENT</table>';
  var dir = 'MY_FOLDER_ID';
  var folder = DriveApp.getFolderById(dir);
  var contents = folder.getFiles();
  var filelist = []; 
  var file, name, url = []; 
  while (contents.hasNext()) {
   file = contents.next();
   name = file.getName();
   url = file.getUrl();
   filelist = filelist.concat('<tr><td><a id="' + name + '" href="' + url + '">' + name + '</a></td></tr>');
  }
}

This generates an array filelist that I want to alpha sort. 这将生成我要进行alpha排序的数组文件列表。 I want to sort these by the name of the file (in the ID to put it before the messy google link). 我想按文件名对它们进行排序(在ID中将其放在凌乱的Google链接之前)。

filelist.sort();
var output = HtmlService.createHtmlOutput(template.replace('APPS_SCRIPT_CONTENT', filelist));
return output.setTitle('Directory List').setSandboxMode(HtmlService.SandboxMode.IFRAME);

For four sample files, I get the following output: 对于四个示例文件,我得到以下输出:

,,,
name
Sample 2
Zzz file
aaa file
sample 1

A couple of problems: 1: They aren't alpha sorted. 几个问题:1:它们不是按字母排序的。 In fact, they are sorted by last modified date. 实际上,它们是按上次修改日期排序的。 If I modify a file and rerun the script, the modified file jumps to the top. 如果我修改文件并重新运行脚本,修改后的文件将跳到顶部。 2: I have no idea where the three commas above the 'name' came from. 2:我不知道“名称”上方的三个逗号来自何处。

What can I do next? 接下来我该怎么办?

Since folder is used to getFiles which is a collection of files you can use underscore.js to basically sort them as json data like the following: 由于文件夹用于获取文件的集合getFiles,因此您可以使用underscore.js将它们基本上按json数据进行排序,如下所示:

function doGet(e) {

  var template = '<table style =""><tr><th>name</th></tr>APPS_SCRIPT_CONTENT</table>';
  var dir = 'MY_FOLDER_ID';

  var files = [{name: 'Sample 2', url:''},{name: 'zzz file', url:''},{name: 'aaa file', url:''},{name: 'Sample 1', url:''}];

  var sortedArray = _.sortBy(files, 'name');

  var filelist = [];

  _.each(sortedArray, function(file){
      filelist += '<tr><td><a id="' + file.name + '" href="' + file.url + '">' + file.name + '</a></td></tr>'
  });

}

Underscore is really good at sorting collections of data. Underscore确实擅长对数据集合进行排序。 If you're unsure about the structure of folder you can console.log(JSON.stringify(folder)) which will show you the JSON structure of the folder. 如果不确定文件夹的结构,可以使用console.log(JSON.stringify(folder)),它会向您显示文件夹的JSON结构。

Here is a plunker snippet as well: http://plnkr.co/edit/EDO3DWVOraRb5Y02XOvh?p=preview 这也是一个小段代码: http ://plnkr.co/edit/EDO3DWVOraRb5Y02XOvh?p=preview

This will address your comma Dilemma, you are getting that extra commas because you are converting your array to a string here: 这将解决您的逗号难题,您将获得额外的逗号,因为您在此处将数组转换为字符串:

template.replace('APPS_SCRIPT_CONTENT', filelist)

Which basically joins the array objects which a comma, gives you a output like this: 它基本上将数组对象连接起来,并用逗号将其输出如下:

<table style =""><tr><th>name</th></tr><tr><td>Filename</td></tr>,<tr><td>Filename</td></tr>,<tr><td>Filename</td></tr>,<tr><td>Filename</td></tr>,<tr><td>Filename</td></tr>,

As you can see all the extra commas are not within a tr or td tag. 如您所见,所有多余的逗号都不在tr或td标记内。 This pushes them out of the table and displays it on top. 这会将它们推出表格并显示在顶部。 The correct syntax for replace would be this: 替换的正确语法如下:

template.replace('APPS_SCRIPT_CONTENT', filelist.join(""))

Gives this: 给出以下内容:

<table style =""><tr><th>name</th></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>Filename</td></tr><tr><td>

Edit: Your sort is working as excepted. 编辑:您的排序工作正常。 Since you have lower case and uppercase filename it sorts the uppercase first and then sorts the lower case letters. 由于您使用小写和大写文件名,因此它首先对大写字母进行排序,然后对小写字母进行排序。

I modified the code to help sort better: 我修改了代码以帮助更好地排序:

while (contents.hasNext()) {
    file = contents.next();

    name =file.getName()
    idName = name.toUpperCase() // Use upper case for ID and it should sort correctly.
    url = file.getUrl();

    filelist = filelist.concat('<tr><td><a id="' + idName + '" href="' + url + '">' + name + '</a></td></tr>');
} 
filelist.sort()

Hope that helps, let me know! 希望对您有所帮助,让我知道!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM