当前位置: 动力学知识库 > 问答 > 编程问答 >

How can you get script to look at a certain sheet in a spreadsheet?

问题描述:

I am trying to send emails from a spreadsheet. The script grabs the emails and the body of the email from a sheet within the spreadsheet. When this sheet is the first sheet, the script works fine. However, for reasons that I won't go into I need another sheet to be first. I have tried using:


var ss = SpreadsheetApp.getActive.Sheet();


var sheet = ss.getSheetByName('Email');

However, when I do this I get the message


TypeError: Cannot find function getSheetByName in object Sheet.

(line 3, file "Code")


Is there another way to get the script to get information from a sheet besides the first sheet in a spreadsheet?

网友答案:

To get a sheet in a spreadsheet you need to use a method that belongs to a sheet parent's class, in other words, a method from the spreadsheet class.

Spreadsheet is the container of all its sheets, that is a basic concept.

So simply use a code like below :

var ss = spreadSheetApp.getActiveSpreadSheet();// this gets you the active spreadsheet in which you are working   
var sheet = ss.getSheetByName('emails');// if the sheet you want is called 'emails

You could easily avoid such simple problems by making use of the autocomplete feature included in the script editor : simply type spread followed by CONTROL SPACE and you'll see all the available methods in this context, the first one will be spreadSheetApp, then type a dot (.) and you will see all the methods in that class and the type of object it returns... choose the one you need and continue like that al along.

see an illustration below :

分享给朋友:
您可能感兴趣的文章:
随机阅读: