且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Google Apps脚本:在脚本中排序时可以指定标题行吗?

更新时间:2023-12-05 21:27:40


在所有情况下,var sheet = SpreadsheetApp.getActiveSheet();, var rows =
sheet.getDataRange();, firstRow是标题行
data的数组。

In all cases, var sheet = SpreadsheetApp.getActiveSheet();, var rows = sheet.getDataRange();, and firstRow is an array of the header row data.

使用getDataRange(),你可以得到你想要排序的范围(也就是排除标题行):

Rather than using getDataRange(), you can just get the range you want to sort (that is, exclude the header row):

var sheet = SpreadsheetApp.getActiveSheet();
var rows = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());






下一部分只是一个答案/观察到次要问题:
$ b


This next part is just an answer/observation to a "secondary" question:


脚本完成后,标题行与所有$一起排序

After the script is finished, the header row was sorted along with all the other rows although the first row is frozen.

我认为这对GAS来说有点限制,其中一些行是冻结的这些电子表格工具方法不能很好地互相发挥作用 - 此问题与您的不一样,但我认为它是(种)相关的。

I think this is a bit of a limitation in GAS where some of these "spreadsheet tool" methods don't play nice with one another - this issue is not the same as yours but I think it's (kind of) related.

因此,使用冻结头文件解决方法,我会感兴趣的是看看 SpreadsheetApp.flush(); 紧接在 sheet.setFrozenRows(1); 之后插入它。

So with the "frozen header" workaround, I'd be interested to see if SpreadsheetApp.flush(); inserted just after sheet.setFrozenRows(1); makes it work.