sorting - 纸张自动排序

标签 sorting google-apps-script google-sheets

我正在尝试弄清楚如何按字母顺序自动对工作表进行排序。每当我在 A-C 列下放置一个新条目时,我希望它能够自动与其余数据一起排序。

我听说这必须使用 Google Apps 脚本来完成。谁能帮我解决这个问题吗?

提前致谢!

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

最佳答案

电子表格可以轻松地通过脚本进行排序,并且脚本可以轻松地由电子表格“事件”触发。

onEdit 是应该满足您的需求的事件之一。 Doc herehere .

那么排序过程就是shown in the doc,我复制了下面的代码:

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}]);

关于sorting - 纸张自动排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35563652/

相关文章:

javascript - 如何从谷歌电子表格中获取谷歌文档中的段落并创建文档的链接?

arrays - 使用 JSON 对 Swift 数组进行排序

sorting - 按功能编程语言排序

c - 使用函数对动态数组进行冒泡排序

regex - 在范围内使用时,带正则表达式的 Google Sheets 自定义函数在交替行上失败

google-apps-script - 如何使用谷歌应用程序脚本根据我在另一列上设置的条件从一列获取值?

node.js - Google Sheets API——自动获取最大范围的表格

java - 如何对 map 的键进行排序

javascript - Google 应用程序脚本 : use the mailapp. sendmail 服务发送仅带有偶尔附件的邮件

node.js - google Sheets api v4 在特定行后附加行或将行移动到特定位置