google-apps-script - "The coordinates of the range are outside the dimensions of the sheet"排序时出错

标签 google-apps-script google-sheets

我正在尝试创建一个脚本来对当前事件工作表/选项卡上的表格进行排序,但我收到无法识别的错误。我不断收到 The coordinates of the range are outside the dimensions of the sheet.。我的表中有从 columnA 到 columnQ 的数据。 columnA 中的数据是标识号,columnB 到 columnQ 包含公式。我的脚本做错了什么?

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}]);

最佳答案

很有可能是线路上的错误

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

很有可能的解决办法是改成

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

上面是因为范围从第 2 行开始并且因为工作表很可能在最后一行有内容。

相关

关于google-apps-script - "The coordinates of the range are outside the dimensions of the sheet"排序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49157222/

相关文章:

google-docs-api - 序列化继续时出现意外异常

javascript - 谷歌应用程序脚本 : Copy and Paste Formulas Only

javascript - 使用 IMPORTRANGE 删除一个电子表格从另一电子表格提取数据的权限

javascript - 在电子邮件正文中开始一个新段落

javascript - 为什么我在 Google Scripts 中对看似完全相同的输入执行 indexOf 操作会得到不一致的结果?

xpath - 如何在Google表格中找出适合importxml的xpath?

javascript - 谷歌应用脚​​本: Parsing XML results in error: "Cannot find function getChildren in object"

google-apps-script - Google Apps 脚本中表单的动态选择选项

javascript - 当dateformat为字符串时过滤一组json数据

google-apps-script - DocumentApp.getActiveDocument() 返回 null