How to get current file name in Google Script?

I want to send automatic emails from my table script. In the title I want to indicate the name of the file (in this example, “My workbook”, and not “Sheet1”) in the subject line. How can I get the name of the file to / from which the script is running?

enter image description here

I was hoping to get the object Filefrom the object SpreadsheetApp, but it does not offer this.

+5
source share
3 answers

.getActive().getName(); will be quite effective.

SpreadsheetApp is not specific to a spreadsheet, you need to specify it and then get the name, therefore getActive ().

+7
source

, script, :

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

This solution works with all types of Google application files (Docs, Sheets, Forms, ...)

You need to use driveapp

The code below refers to a form, for example:

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

Just get the document from the corresponding application, then use DriveApp to get the file by its identifier, after which you will have a file name

+1
source

All Articles