使用javascript的锚点标记中的HTML链接的alpha排序数组 [英] alpha sort array of HTML links in anchor tags using javascript

查看:86
本文介绍了使用javascript的锚点标记中的HTML链接的alpha排序数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的 - 这个包含javascript和Google Apps脚本。问题在于JavaScript。



我使用以下代码创建了Google云端硬盘文件夹中文件的链接列表:

 函数doGet(e){
var template ='< table style =>< tr>>< th>名称< / th>< / tr> APPS_SCRIPT_CONTENT< ; /表>';
var dir ='MY_FOLDER_ID';
var folder = DriveApp.getFolderById(dir);
var contents = folder.getFiles();
var filelist = [];
var文件,名称,url = [];
while(contents.hasNext()){
file = contents.next();
name = file.getName();
url = file.getUrl();
filelist = filelist.concat('< tr>< td>< a id ='+ name +'href ='+ url +'>'+ name +'< /一个>< / TD>< / TR>');


$ / code $ / pre

这会生成一个数组文件列表, 。我想通过文件名来排序这些文件(在ID中放置在杂乱的谷歌链接之前)。

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

对于四个示例文件,我得到以下输出:

  ,,, 
名称
示例2
Zzz文件
aaa文件
示例1

几个问题:
1:它们不是alpha排序的。实际上,它们是按最后修改日期排序的。如果我修改文件并重新运行脚本,修改后的文件跳转到顶部。
2:我不知道姓名之上的三个逗号来自哪里。



接下来我可以做什么?

解决方案

逗号难题,你会得到额外的逗号,因为你在这里将你的数组转换为字符串:

$ $ p $ template.replace('APPS_SCRIPT_CONTENT ',filelist)

其中基本上加入了一个逗号的数组对象,为您提供如下输出:

 < table style =>< tr>< th>名称< / th>< / tr> ;< TR>< TD>文件名< / TD>< / TR>,< TR>< TD>文件名< / TD>< / TR>,< TR>< TD>文件名< / < td>< / tr>,< tr>< td>文件名< / td>< / tr>,< tr>< td>  

正如您所看到的,所有额外的逗号不在tr或td标签内。这将它们推出桌面并将其显示在顶部。替换的正确语法是:

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

给出这个:

 < < />< th>< / th> < TR>< TD>文件名< / TD>< / TR>< TR>< TD>文件名< / TD>< / TR>< TR>< TD>文件名< / TD>< ; / TR>< TR>< TD>文件名< / TD>< / TR>< TR>< TD>文件名< / TD>< / TR>< TR>< TD>文件名< / TD>< / TR>< TR>< TD>文件名< / TD>< / TR>< TR>< TD>文件名< / TD>< / TR>< TR>< TD> 

编辑:
您的排序例外。由于您使用小写和大写的文件名,因此首先将大写字母排序,然后对小写字母进行排序。



我修改了代码以帮助更好地进行排序:

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

name = file.getName()
idName = name.toUpperCase()//使用大写字母作为ID,它应该正确排序。
url = file.getUrl();
$ b $ filelist = filelist.concat('< tr>< td>< a id ='+ idName +'href ='+ url +'>'+ '< / A>< / TD>< / TR>');
}
filelist.sort()

希望有帮助,请告诉我!

Ok - This one involves both javascript and Google Apps Script. The problem is with the javascript.

I am creating a list of links to files in a Google Drive folder using the following code:

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. I want to sort these by the name of the file (in the ID to put it before the messy google link).

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. 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.

What can I do next?

解决方案

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. 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!

这篇关于使用javascript的锚点标记中的HTML链接的alpha排序数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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