Hello,
To make the script affect multiple rows, we can adjust the approach. The getCell method only gets the first cell in the first row of range "r", which is why it's only populating rules for row 10 (the first row in your example). Instead, we should get the entire first column of "r" so the script will populate rules for all the rows.
We can use the following code to do this:
const firstColumnOfRange = src.getRange(
r.getRow(),
r.getColumn(),
r.getNumRows(),
1
)
firstColumnOfRange.offset(0, 4).setDataValidation(rule)
In this code, firstColumnOfRange represents the entire first column of range "r". Using your example where "r" is "B10:D12", firstColumnOfRange will be range "B10:B12". The offset(0, 4) part of the code then applies the validation rule to all the cells in range "F10:F12" (the range 4 columns to the right of "B10:B12").
To help reduce repetition, I made the following function:
function getOffsetColumn(src, r, columnOffset){
return src.getRange(
r.getRow(),
r.getColumn(),
r.getNumRows(),
1
).offset(0, columnOffset)
}
One way you can use it in your code is like so:
getOffsetColumn(src, r, 4).setDataValidation(rule)
This works the same way as the above. It applies the data validation rule to each cell in column F for the rows in the affected range.
Here is a copy of the spreadsheet that uses this updated approach:
LINK.
Thanks,
Oliver