Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

enter image description here

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

like image 734
Ky. Avatar asked May 11 '15 18:05

Ky.


People also ask

Is there a way to emulate Vlookup in Google script?

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.

How do I view Google App Script logs?

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.


1 Answers

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

like image 54
Hoang Trinh Avatar answered Oct 10 '22 03:10

Hoang Trinh