'TypeError: Cannot read property 'getRange' of undefined

Why AppScript in google sheets doesn't find the range in the sheet?The SpreadsheetApp finds the sheet but shows error in line 6.

I tried ActiveRange and just getRange defining this in the sheet, but I always see this error.

Please help me I'm not a programmer.

Below is my function:

function sendEmailtoworkers() { 
    let ss = SpreadsheetApp.getActiveSpreadsheet();
    let sheet = ss.getSheetByName('number')[0];
    let list = sheet.getRange(2,2,9,2);
    MailApp.sendEmail( list, "пупкин", "Пожалуйста, зайди на этот файл: docs.google.com/spreadsheets/d/…);
}


Solution 1:[1]

getSheetByName('number') will give you the sheet object, you're trying to access [0] from that, which returns undefined.

You can either use

let sheet = ss.getSheetByName('number')

or

let sheet = ss.getSheets()[0];

getSheets() returns list of sheet objects, you can access through index. To know more, click here!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul Nikhil