3

I'm getting this error: "Cannot convert range to (class) on this line:

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

This is the function:

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

I need to pass ranges of single cells to 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.')
  }
}

I only found suggestions to fix this error when setValues() was involved, but nothing to help me here. Thank you for your help!

1 Answer 1

3

Found the problem.

I was passing a range object instead of integer to the function. Here it is working:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.