Google Sheets脚本以降序排序整个工作表

huangapple go评论52阅读模式
英文:

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 &#39;[object Object]&#39; 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&#39;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(&#39;DATA&#39;);
  sheet.sort(4, false);
}

References:

huangapple
  • 本文由 发表于 2023年6月9日 10:25:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76436811.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定