1

I tried to do simple sort by 2 columns, there is many examples. but I got parameter error. and cant understand why here is my code:

function myFunction1() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getActiveSheet();
  var dateColumn = 1;
  var houerColumn = 3;

  sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns()).activate();
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheets()[0];
  sheet.sort([{ column: 1, ascending: true }, { column: 3, ascending: true}]);
};

and the error I got is: Exception: Cannot convert '[object Object],[object Object]' to int. on row number 10 (the sort row). what do I do wrong?

2
  • Can you give us an extract of your data, or look closely what values are in A10 and C10. Pls, precise also which sheet do you want to sort : you first choose the active sheet, then the first sheet (index 0) = may be the line var sheet = ss.getSheets()[0] is not necessary. Commented Jul 25, 2021 at 11:21
  • 1
    There are no object parameters in sheet sort: developers.google.com/apps-script/reference/spreadsheet/… There is just an integer and a boolean Commented Jul 25, 2021 at 16:20

1 Answer 1

5

It seems you are over-complicating things. The way you're trying to sort is usually done on ranges, not sheets. This code should suffice:

function myFunction1(){
  SpreadsheetApp.getActiveSheet().getDataRange().sort([{ column: 1, ascending: true }, { column: 3, ascending: true}]);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.