Nicole Batte
Nicole Batte

Reputation: 25

How to change Google Sheets file name based on cell information

I am trying to rename my Google Sheets File name based on contents of at least one cell (ideally, it would be a combination of information from two cells: B2 and D2, but I can make contents of one cell work).

I am able to change the worksheet name from the contents of one cell, but am struggling to change the actual file name.

Upvotes: 0

Views: 1626

Answers (1)

MiStral
MiStral

Reputation: 21

To change the Spreadsheet's name you will need DriveApp too and change its filename. Try this example:

// get the active spreadsheet
var ss = SpreadsheetApp.getActiveSpreadsheet();

// get a value from the B2 cell on sheet "Sheet" as new filename
var newFileName = ss.getRange("Sheet!B2").getValue();

// get the spreadsheet ID
var ssID = ss.getId();

// get the spreadsheet file by ID
var ssFile = DriveApp.getFileById(ssID);

 // set new filename for the spreadsheet
ssFile.setName(newFileName);

Upvotes: 2

Related Questions