Aim of the puzzle: Use Apps Script to create and rename a copy of a file in Drive.
Walk through of the solution: Apps Script has custom code functions that connect to Drive. One example is .makeCopy()
, which creates a copy of a file from Drive. It returns a new file that has the same contents as the file it copied but a different id.
If .makeCopy()
is given a string as an argument, it will set the name of the copy to that string. For example, file.makeCopy('I am a copy!')
will set the name of the copy to 'I am a copy!'
. If .makeCopy()
is used without an argument, the copy will have the default title 'Copy of [file name]'
.
To complete the puzzle, create a variable named secondCopy
that stores a new copy of file
using file.makeCopy()
. Then, add a string as an argument inside the parentheses of .makeCopy()
.For example, file.makeCopy('Copy 2 of Inventory of Cat Toys')
. Next, create a variable called secondCopyName
that stores the name of the copy using .getName()
. Finally, print secondCopyName
to the console.
Sample code solution:
let id = '1gHAiEKJd8NMdjI_EcI12y3iIYk3E8wokAJEOijsZcf0';
let file = DriveApp.getFileById(id);
let firstCopy = file.makeCopy('Copy 1 of Inventory of Cat Toys');
let firstCopyName = firstCopy.getName();
console.log(firstCopyName);
let secondCopy = file.makeCopy('Copy 2 of Inventory of Cat Toys');
let secondCopyName = secondCopy.getName();
console.log(secondCopyName);
JavaScript concepts: Variables, Strings
Apps Script concepts: DriveApp
, .getFileById()
, .makeCopy()
, .getName()