Question

Trying to sort a spreadsheet based on the first column 'timestamp'. After running the script below, a 'sort range must include all columns on a form sheet' error is returned. There are A-S columns in the sheet. Columns A-O contain form data with columns P-S containing human input.

function sortForm() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Change to the "form responses" spreadsheet
  var sheet = ss.getSheetByName("Form Responses");
  var lastCol = 19
  var lastRow = sheet.getLastRow();

  // Get range
  var r = sheet.getRange(2, 1, sheet.getMaxRows() -1, lastCol);

  // Sort the range
  r.sort([{column: 1, ascending: false}]);

}

What could be causing this error?

Was it helpful?

Solution

+1 @nah_ny - I had the same problem, and it seems that the new version of google spreadsheets dont allow range sorting if that range is in a google forms response sheet:

// not working
function sort_form_responses() {
    // get spreadsheet
    var sa = SpreadsheetApp.getActiveSpreadsheet();
    // get forms response sheet by name 
    var sn = sa.getSheetByName("Form Responses 2");
    // get sheet range, assumes headers in row 1
    var r = sn.getRange(2, 1, sn.getMaxRows()-1, sn.getMaxColumns());
    // sort range array
    r.sort([{ column: 2, ascending: false }]);
    ...

A solution that worked in my case was instead to use the sort method on the entire response sheet object:

// working
function sort_form_responses() {
    // get spreadsheet
    var sa = SpreadsheetApp.getActiveSpreadsheet();
    // get forms response sheet by name 
    var sn = sa.getSheetByName("Form Responses 2");
    // sort entire sheet required for form sheets
    sn.sort(2,false)
    ...

OTHER TIPS

The new Google sheets provides a new way to sort spreadsheets:

function sortSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();

  // Change to the "form responses" spreadsheet
  var sheet = ss.getSheetByName("Requests");
  sheet.sort(1, false);
}

encountered same error. tried everything, but after several hours just recreated spreadsheet and it worked.
Seems some spreadsheets got corrupted with all this "check a new look of google spreadsheets".
So, just try to recreate spreadsheet and readd all scripts.

I have the same error and created a new sheet and form.

used the code

function sortmysheet () {
  var sheet      = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  var lastColumn = sheet.getMaxColumns();  // the last column of the sheet
  var lastRow    = sheet.getMaxRows();     // Returns the position of the last row

  var range = sheet.getRange(1, 1, lastRow, lastColumn);
  range.sort([2,1]);  
}

I included the entire sheet in the range as you can see.

I also tried portions that included limited rows but maximum columns. Tried deleting all the columns except A,B. Tried setting the range numerically.

This worked on a sheet that is not attached to a form but I get the same error on a form linked sheet.

If I unlink the form, the script works.

However I have found a solution that works in certain cases.

Although it is not describe this way in the documentation, you can chain the sheet.sort() function.

So I used

function sortmysheet () {
  var sheet      = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
  sheet.sort(2, false).sort(1, true);
}

Obviously this will only work on the entire sheet. I set the names in my header rows so they will not be moved.

I found this solution via Matthew Taylor

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top