在工作表上自动排序 [英] Automatic Sorting on Sheets

查看:112
本文介绍了在工作表上自动排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何自动按字母顺序排列我的工作表。每当我在列AC下添加一个新条目时,我希望它会自动与其余数据一起排序。



我听说这必须使用Google应用程序脚本。任何人都可以帮我解决这个问题吗?

预先致谢!

https://docs.google.com/spreadsheets/d/1XH4mrKa6W4se5WwM6oKqh959gG2kAQVtAmOnml13VoE/edit#gid=0

解决方案

电子表格很容易从脚本中排序,脚本可以通过电子表格事件轻松触发。



onEdit 是符合您需求的事件之一。 此处的文档
此处



那么排序过程是在文档中显示的我重现了下面的代码:

  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
var sheet = ss.getSheets()[0];
var range = sheet.getRange(A1:C7);

//按第一列中的值排序(A)
range.sort(1);

//按第二列中的值排序(B)
range.sort(2);

//按列降序排序
range.sort({column:2,ascending:false});

//按列B降序排列,然后按A列升序
//注意使用数组
range.sort([{column:2,ascending:false },{column:1,ascending:true}]);

//对于按升序排序的行,升序参数为
//可选,只需使用带整列的整数即可。请注意,
//通常保持排序规范一致会产生更易读的
//代码。我们可以表示早先的排序为:
range.sort([{column:2,ascending:false},1]);

//或者,如果我们希望所有列按升序排列,我们将使用
//以下内容(这会使第2列升序)
range.sort( [2,1]);
// ...相当于
range.sort([{column:2,ascending:true},{column:1,ascending:true}]);


I'm trying to figure out how to sort my sheet alphabetically automatically. Whenever I put a new entry under columns A-C, I would like it to automatically sort together with the rest of the data.

I've heard that this must be done using Google apps scripts. Can anyone help me with that?

Thanks in advance!

https://docs.google.com/spreadsheets/d/1XH4mrKa6W4se5WwM6oKqh959gG2kAQVtAmOnml13VoE/edit#gid=0

解决方案

Spreadsheets are easy to sort from a script and a script can easily be triggered by a spreadsheet "event".

onEdit is one of these events that should fit your demand. Doc here and here.

then the sort process is shown in the doc, I reproduce the code below :

var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[0];
 var range = sheet.getRange("A1:C7");

 // Sorts by the values in the first column (A)
 range.sort(1);

 // Sorts by the values in the second column (B)
 range.sort(2);

 // Sorts descending by column B
 range.sort({column: 2, ascending: false});

 // Sorts descending by column B, then ascending by column A
 // Note the use of an array
 range.sort([{column: 2, ascending: false}, {column: 1, ascending: true}]);

 // For rows that are sorted in ascending order, the "ascending" parameter is
 // optional, and just an integer with the column can be used instead. Note that
 // in general, keeping the sort specification consistent results in more readable
 // code. We could have expressed the earlier sort as:
 range.sort([{column: 2, ascending: false}, 1]);

 // Alternatively, if we wanted all columns to be in ascending order, we would use
 // the following (this would make column 2 ascending)
 range.sort([2, 1]);
 // ... which is equivalent to
 range.sort([{column: 2, ascending: true}, {column: 1, ascending: true}]);

这篇关于在工作表上自动排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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