未找到范围,Google 应用程序脚本 Google 工作表

Range Not Found, Google Apps Script for Google Sheets

我正在尝试在 Google 表格的 Apps 脚本中为此排序函数构建自定义范围,但我不断收到错误消息:“异常:未找到范围;sortProductionLog @ macros.gs :15

我确定这是基本的东西,但我搜索了很远,但似乎找不到。任何人都可以解释一下吗?

谢谢, 瑞安

SORT_ORDER = [
{column: 118, ascending: true},  // 3 = column number, sorting by descending order
{column: 119, ascending: true}, // 1 = column number, sort by ascending order 
{column: 117, ascending: true},
{column: 25, ascending: true}
];

function sortProductionLog(){
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName(SHEET_NAME);
  var LastRow_WithValue = sheet.getLastRow();
  var LastColumn_WithValue = sheet.getLastColumn();
  var buildRange = "4,1," + (LastRow_WithValue - 4) + "," + (LastColumn_WithValue - 1);
  var range = sheet.getRange(buildRange);
  ss.toast(buildRange);
  range.sort
}

问题是您正在尝试使用字符串 "4, 1, ..." 作为 .getRange() 参数。该函数最多接受四个数字,或“A1 表示法”中的单个字符串。

来源:

建议修复:

var buildRange = [4,1, LastRow_WithValue - 4, LastColumn_WithValue - 1];
var range = sheet.getRange(...buildRange);

此外,这里有一个综合版本:

function sortProductionLog() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(SHEET_NAME)
  sheet.getRange(4, 1, sheet.getLastRow()-4, sheet.getLastColumn()-1)
       .sort(SORT_ORDER)
}

这样试试:

function sortProductionLog() {
  const ss = SpreadsheetApp.getActive()
  var sh = ss.getSheetByName("SHEET_NAME");
  var range = sh.getRange(4, 2, sh.getLastRow() - 3, sh.getLastColumn());
  ss.toast(range.getA1Notation());
  range.sort([{ column: 118, ascending: true }, { column: 119, ascending: true }, { column: 117, ascending: true }, { column: 25, ascending: true }]);
}