Chris Barrett
Chris Barrett

Reputation: 601

Get Current Google Script Name to a Variable

Does anyone know a way to get the name of the current google script file that's bound to the sheet you are using and write it to a variable? I want to put it into a sheets cell to keep track of changes I make.

Cheers

Upvotes: 4

Views: 2644

Answers (3)

Thierry
Thierry

Reputation: 1

I know this is coming way after... but I faced the same roadblock recently (quite new to GAS).

Did you add the Drive app in Services? That's how I solved it: all Drive functions are available then.

Upvotes: 0

Lord Null
Lord Null

Reputation: 956

var id = ScriptApp.getScriptId();
var name = DriveApp.getFileById(id).getName();
Logger.log(name);

Upvotes: 12

Amit Agarwal
Amit Agarwal

Reputation: 11258

For standalone Google Scripts (not bound to any Google Sheet or Docs), you can use the DriveApp service to get the file name from Drive.

Right-click the file in Google Drive and copy the Share Link. Then get the ID from the shared link and use the DriveApp service to retrieve the name from the file ID.

function getName() {
 var fileId = "123";
 var file = DriveApp.getFileById(fileId);
 return file.getName();
}

Upvotes: 1

Related Questions