Reputation: 10050
According to the documentation, there is a function named getRangeByName(String)
in class Spreadsheet but it seems to be undefined for me.
function onEdit(e) {
if (e) {
var ss = e.source.getActiveSheet();
Logger.log(ss.getName);
Logger.log(ss.getRange);
Logger.log(ss.getRangeByName);
}
}
// outputs:
// function getName() {/* */}
// function getRange() {/* */}
// undefined
Is this because event.source.getActiveSheet()
returns something else other than a Spreadsheet
object, which contradicts the official documentation (screenshot)?
(https://developers.google.com/apps-script/understanding_events)
If that's the case, how can I get a range by name on the active sheet from the event's source? Thanks.
Upvotes: 4
Views: 7267
Reputation: 16551
By doing var ss = e.source.getActiveSheet();
the variable ss
is Sheet type, not of type Spreadsheet, so the problem. The method getRangeByName(name)
belongs to Spreadsheet.
Try the following:
function onEdit(e) {
if (e) {
var ss = e.source;
var s = ss.getActiveSheet();
Logger.log(s.getName);
Logger.log(s.getRange);
Logger.log(ss.getRangeByName);
}
}
// outputs:
// function getName() {/* */}
// function getRange() {/* */}
// function getRangeByName() {/* */}
Upvotes: 4