Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Scripts: cannot convert range to (class) error

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!

like image 712
Mor Sagmon Avatar asked Jun 30 '26 23:06

Mor Sagmon


1 Answers

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); 
  }
}
like image 198
Mor Sagmon Avatar answered Jul 03 '26 03:07

Mor Sagmon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!