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

How do I get the current file's name in Google Script?

我想从电子表格的脚本发送自动电子邮件。在这些标题中,我希望在电子邮件的主题中包含文件名(在此示例中,"My Workbook",而不是 "Sheet1")。 如何获取脚本 运行 的文件 in/from 的名称?

我希望得到 File object from a SpreadsheetApp object,但它不提供。

.getActive().getName(); 会很有效。

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

如果您想获取您所在的电子表格的名称 运行 使用脚本:

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

此解决方案适用于所有类型的 Google 应用程序文件(文档、表格、表单...)

您需要使用 DriveApp

以下代码引用Form,例如:

const currentForm = FormApp.getActiveForm()
const formFile = DriveApp.getFileById(currentForm.getId())
const formName = formFile.getName()

从对应的app中获取文档,然后使用DriveApp通过ID获取文件,得到文件名