英文:
Google Sheets script to sort entire sheet in descending order
问题
我正在尝试创建一个函数,通过第4列对表格DATA进行降序排序,
```javascript
function specialSort(){
var sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
sheet.sort({ column: 4, ascending: false })
}
其他答案(例如这里)似乎推荐这种方法。但为什么这段代码会引发错误Exception: Cannot convert '[object Object]' to int
?如果我改用sheet.sort(4)
,我可以得到我想要的结果(按第4列对整个表格进行排序),只不过它是升序的。
请注意:我想要(降序)对整个表格按第4列进行排序,而不仅仅是对该列本身进行排序。
<details>
<summary>英文:</summary>
I am trying to create a function which sorts the sheet DATA by column 4, in descending order,
function specialSort(){
var sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
sheet.sort({ column: 4, ascending: false })
}
Other answers (e.g., [here][1]) seem to recommend this approach. But why does this code throw the error `Exception: Cannot convert '[object Object]' to int`? If I instead use simply `sheet.sort(4)`, I get the result I want (sorting the whole sheet by column 4), except that it's in ascending order.
Note well: I want to (descending) sort the whole sheet by column 4, not just the column itself.
[1]: https://stackoverflow.com/questions/35563652/automatic-sorting-on-sheets
</details>
# 答案1
**得分**: 1
From your script, I thought that `sort(sortSpecOb)` is for Class Range. The arguments of `sort(columnPosition, ascending)` of Class Sheet are `columnPosition`(整数), `ascending`(布尔值). I thought that this might be the reason for your current issue. In your script, how about the following modification?
### Modified script:
```javascript
function specialSort() {
var sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
sheet.sort(4, false);
}
References:
英文:
From your script, I thought that sort(sortSpecOb)
is for Class Range. The arguments of sort(columnPosition, ascending)
of Class Sheet are columnPosition
(Integer), ascending
(Boolean). I thought that this might be the reason for your current issue. In your script, how about the following modification?
Modified script:
function specialSort() {
var sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
sheet.sort(4, false);
}
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论