“The coordinates of the range are outside the dimensions of the sheet” error when sorting

て烟熏妆下的殇ゞ 提交于 2021-02-05 08:48:06

问题


I am trying to create a script to sort a table on the current active sheet/tab but I am getting an error that I could not identify. I keep getting The coordinates of the range are outside the dimensions of the sheet.. My table has data in them from columnA to columnQ. Data in columnA are identification numbers and columnB to columnQ contain formulas. What am I doing wrong with my script?

var sortFirst = 7; //index of column to be sorted by; 1 = column A, 2 = column B, etc.
  var sortFirstAsc = true; //Set to false to sort descending

  var sortSecond = 8;
  var sortSecondAsc = true;

  var sortThird = 9;
  var sortThirdAsc = true;

  var activeSheet = SpreadsheetApp.getActiveSheet();
  var sheetName = activeSheet.getSheetName(); //name of sheet to be sorted
  var sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
  var range = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());
  range.sort([{column: sortFirst, ascending: sortFirstAsc}, {column: sortSecond, ascending: sortSecondAsc}, {column: sortThird, ascending: sortThirdAsc}]);

回答1:


It's very likely that the error occurs on the line

var range = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn());

It's very likely that the solution is to change it by

var range = sheet.getRange(2, 1, sheet.getLastRow() - 1, sheet.getLastColumn());

The above because the range start on row 2 and because it's very likely that sheet has content on its last row.



来源:https://stackoverflow.com/questions/49157222/the-coordinates-of-the-range-are-outside-the-dimensions-of-the-sheet-error-whe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!