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?
I was hoping to get a File
object from a SpreadsheetApp
object, but it doesn't offer that.
Yes, it is possible to emulate many of the built-in functions by using Class Spreadsheet (SpreadsheetApp) and JavaScript Array Object and its methods, but "the emulations" usually will be slower than built-in functions.
Use the Apps Script execution log To view these logs, at the top of the editor, click Execution log. When you run a function or use the debugger, the logs stream in real time. You can use either the Logger or console logging services in the built-in execution log.
This solution works for all types of Google apps files (Docs, Sheets, Forms, ...)
You need to use DriveApp
The below code refers to Form, for instance:
const currentForm = FormApp.getActiveForm()
const formFile = DriveApp.getFileById(currentForm.getId())
const formName = formFile.getName()
Just get the document from the corresponding app, then use DriveApp to get the file by its ID, after that you will have the file name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With