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

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

Google Sheets script to sort entire sheet in descending order

问题

  1. 我正在尝试创建一个函数,通过第4列对表格DATA进行降序排序,
  2. ```javascript
  3. function specialSort(){
  4. var sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
  5. sheet.sort({ column: 4, ascending: false })
  6. }

其他答案(例如这里)似乎推荐这种方法。但为什么这段代码会引发错误Exception: Cannot convert '[object Object]' to int?如果我改用sheet.sort(4),我可以得到我想要的结果(按第4列对整个表格进行排序),只不过它是升序的。

请注意:我想要(降序)对整个表格按第4列进行排序,而不仅仅是对该列本身进行排序。

  1. <details>
  2. <summary>英文:</summary>
  3. 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 })
}

  1. 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.
  2. Note well: I want to (descending) sort the whole sheet by column 4, not just the column itself.
  3. [1]: https://stackoverflow.com/questions/35563652/automatic-sorting-on-sheets
  4. </details>
  5. # 答案1
  6. **得分**: 1
  7. 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?
  8. ### Modified script:
  9. ```javascript
  10. function specialSort() {
  11. var sheet = SpreadsheetApp.getActive().getSheetByName('DATA');
  12. sheet.sort(4, false);
  13. }

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:

  1. function specialSort() {
  2. var sheet = SpreadsheetApp.getActive().getSheetByName(&#39;DATA&#39;);
  3. sheet.sort(4, false);
  4. }

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:

确定