如何在Google Script中获取当前文件的名称?

时间:2022-02-21 07:04:58

I want to send automated emails from the script of my spreadsheet. In the title of these, I want the file's name (In this example, "My Workbook", not "Sheet1") in the subject of the email. How do I get the name of the file in/from which the script is running?

我想从电子表格的脚本中发送自动电子邮件。在这些标题中,我想要电子邮件主题中的文件名(在此示例中,“我的工作簿”,而不是“Sheet1”)。如何获取运行脚本的文件的名称?

如何在Google Script中获取当前文件的名称?

I was hoping to get a File object from a SpreadsheetApp object, but it doesn't offer that.

我希望从SpreadsheetApp对象中获取File对象,但它不提供。

2 个解决方案

#1


.getActive().getName(); would be pretty effective.

。.getActive()的getName();会非常有效。

SpreadsheetApp doesn't refer to a specific spreadsheet, you need to specify one then get the name, hence the getActive().

SpreadsheetApp没有引用特定的电子表格,您需要指定一个然后获取名称,因此getActive()。

#2


If you want to get the name of the spreadsheet that you are running the script from use:

如果要获取正在运行脚本的电子表格的名称,请使用:

var name = SpreadsheetApp.getActiveSpreadsheet().getName();

#1


.getActive().getName(); would be pretty effective.

。.getActive()的getName();会非常有效。

SpreadsheetApp doesn't refer to a specific spreadsheet, you need to specify one then get the name, hence the getActive().

SpreadsheetApp没有引用特定的电子表格,您需要指定一个然后获取名称,因此getActive()。

#2


If you want to get the name of the spreadsheet that you are running the script from use:

如果要获取正在运行脚本的电子表格的名称,请使用:

var name = SpreadsheetApp.getActiveSpreadsheet().getName();