Reputation: 1035
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!
Upvotes: 3
Views: 5591
Reputation: 1035
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);
}
}
Upvotes: 3