0

I am following Google's "Data Merge" tutorial to import data from Google Sheets into Google Slides, and the part that does not work fully is "Copying an existing presentation." When I run the provided Apps Script code, I am getting a copy of the file with the name "Copy of ..." instead of "New presentation title".

// Duplicate the template presentation using the Drive API.
var copyTitle = 'New presentation title';
var requests = {
  name: copyTitle
};
var driveResponse = Drive.Files.copy({
  resource: requests
}, originalpresentationID);
var presentationCopyId = driveResponse.id;
Logger.log(driveResponse.title) # Logger returns: "Copy of ..." 

How can I properly create a copy of an existing file, with my desired name? It seems the tutorial is wrong.

tehhowch
  • 9,645
  • 4
  • 24
  • 42
cek11lru
  • 379
  • 2
  • 14

2 Answers2

1

I tried doing this and faced the same issue. Looks like a bug. As a way around try renaming your copy file after is has been created. Here's the code.

var copyTitle = 'New presentation title';
    var driveResponse = Drive.Files.copy({          
    }, originalpresentationID);
    var presentationCopyId = driveResponse.id;
    var copyPPT = DriveApp.getFileById(presentationCopyId);
    copyPPT.setName(copyTitle);
Darpan Sanghavi
  • 1,443
  • 2
  • 17
  • 32
0

The code used in that example mixes in some syntax from other languages and client libraries.

The Drive "advanced service" in Google Apps Script uses v2 of the Drive REST API, for which there are some property name differences. Notably, the filename is title, not name.

Additionally, the syntax for a drive#file resource (aka metadata) in the Drive.Files.copy request is not as a child resource property of the parameter, but as the parameter directly:

const metadata = {
  title: "new name",
  // other properties
};
const options = {
  // optional request parameters
  // fields: "*",
};
var newFileMetaData = Drive.Files.copy(metadata, sourceId, options);
tehhowch
  • 9,645
  • 4
  • 24
  • 42