Google Scripts:无法将范围转换为(类)错误

时间:2016-05-11 09:34:48

标签: google-apps-script

我收到此错误: “无法在此行上将范围转换为(类):

var cellFrom = ss.getRange(rowFrom, i).getCell(1,1);

这是功能:

function copyValidations(sheetName, rowFrom, rowTo, column, numColumns) {
 /* Copy validations of all cells in rowFrom to cells in rowTo, starting at column.\*/

  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getSheetByName(sheetName);

  for (var i = column;i<= column + numColumns;i++) {
    var cellFrom = ss.getRange(rowFrom, i).getCell(1,1);
    var cellTo = ss.getRange(rowTo, i).getCell(1,1);
    copyCellDataValidation(cellFrom, cellTo); 
  }
}

我需要将单个单元格的范围传递给copyCellDataValidation:

function copyCellDataValidation(cellFrom, cellTo) {
 /* Copies the data validation rules and data from a single cell to another cell
*/

  var rule = cellFrom.getDataValidation();
  if (rule != null) {
   var criteria = rule.getCriteriaType();
   var args = rule.getCriteriaValues();
   cellTo.setDataValidation(rule.copy().withCriteria(criteria, args).build());
  } else {
   //Logger.log('copyCellDataValidation: The cell does not have a data-validation rule.')
  }
}

我只是在涉及setValues()时找到了修复此错误的建议,但没有什么可以帮到我。 谢谢你的帮助!

1 个答案:

答案 0 :(得分:3)

发现问题。

我正在将一个范围对象而不是整数传递给该函数。 这是工作:

function copyValidations(sheetName, rowFrom, rowTo, column, numColumns) {
 /* Copy validations of all cells in rowFrom to cells in rowTo, starting at column.\*/

  var sh = SpreadsheetApp.getActiveSpreadsheet();
  var ss = sh.getSheetByName(sheetName);

  for (var i = column;i<= column + numColumns;i++) {
    var cellFrom = ss.getRange(rowFrom, i);
    var cellTo = ss.getRange(rowTo, i);
    copyCellDataValidation(cellFrom, cellTo); 
  }
}