I'm working with Google App Script. I am trying to:
- Make a copy of a spreadsheet
- Give the copy a particular file name, and
- Save it to a particular folder.
It seems to me that the code below should work, but it doesn't. It does accomplish tasks 1 and 3 - that is, it makes a copy and saves the copy to my desired folder. However, it does not save the file with the file name I specify. Instead, it saves it as "Copy of [original spreadsheet filename]." To do this, I am using File#makeCopy
(Note: I know this question has been answered before, but I believe it has not been answered since Google sunset the DocList service.)
When I made this simplified MCVE, I found that it works as expected:
function saveAsSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy("desired file name", destFolder);
} //END function saveAsSpreadsheet
However, my original code does not - the copy's name is wrong:
function saveAsSpreadsheet() {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var buildingNameAddress = sheet.getRangeByName('buildingNameAddress').getValue();
Logger.log(buildingNameAddress); //Logs correct string
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy(buildingNameAddress, destFolder);
}