Create a Copy Explainer

Aim of the puzzle: Use Apps Script to write a function that creates a copy of a contract document from Docs, renames the copy, and returns its id.

Walk through of the solution: For the last topic of this course, you’ll use Apps Script to create a function that automates the process of creating contracts.

In this puzzle, you’ll write the 1st of two helper functions that will be used later in the main function. The function should create a copy of a contract document from Docs, rename the copy, and return its id.

In the code that you start off with, the makeCopy helper function takes an id, and a name.

To complete the puzzle, create a variable called file that stores the file retrieved using DriveApp.getFileById(id). Then, create another variable called copy that stores a copy of file named name using file.makeCopy(name). Finally, add a return statement that returns the id of copy using copy.getId().

JavaScript concepts: Variables, Strings, Parameters, Arguments, Function Declarations, Function Calls
Apps Script concepts: DriveApp, .openById(), .makeCopy(), .getId()

2 Likes

In the previous tutorial there is a typo:
“This agreement certifies that Saanvi will pay $275 for True Pruning.”
instead of
“This agreement certifies that Saanvi will pay $275 for Tree Pruning.”

Capture3
I am having trouble…Please help!!!

Hey there, take a look at your variable declarations. They should look like this:

let variableName = value

Hope this helps!
Ben

let contractId = '226uAXY0YRUj-HMW5LQZ2YeeJgyDqmJTpnzRkGmFLch0';
  
function makeCopy(id, name) {
    let DriveApp.getFilesById(id);
    let file.makeCopy(name);
    
}

console.log(makeCopy(contractId, 'Copy of the contract'));

Hey there, you have the same issue I wrote about in the post above yours. The variable declarations need fixing.

Remember, variables need a name, then the assignment operator =, then a value, like this:

let animal = 'dog' // this creates a variable named animal
// with the value 'dog'

What you have is kind of like this:

let 'dog' // this is invalid code and will cause an error

Hope this helps! Let me know if you have any more questions.
Ben

What did I do wrong? pls tell.

let contractId = '226uAXY0YRUj-HMW5LQZ2YeeJgyDqmJTpnzRkGmFLch0';
  
function makeCopy(id, name){
 let file = DriveApp.getFileById(id);
     let copy = file.makeCopy(name);
    
}

console.log(makeCopy(contractId, 'Copy of the contract'))
return copy.getId().

@HARISH_Kabadwal Put let in front of copy and file, like this:

let file = DriveApp.getFileById(id);
let copy = file.makeCopy(name);

Then, move the return statement so it’s inside the function body, at the end.

@Shanto_P_Shaju You also have the return statement outside the function. Try moving it inside, at the end of the function.

What’s wrong with my code??

There is nothing wrong here

Hmmm. Your code seems fine to me. Try sending a bug report, maybe they were a bug. :thinking:

Pummarin

Hey, sorry for the delay! Just seeing your post now. Somehow the puzzle thinks that the return statement is outside the function body, but your code looks fine, so somewhere there’s a bug.

Try posting the code here in the forum, so I can take a look. There might be extra characters in the text that we aren’t seeing, such as a zero-width space. When you post it to the forum, put it in a code block by using 3 backticks above and below the code, like this:

```

// paste code here

```

Hope this helps!
Ben

What did I do wrong?

let contractId = '226uAXY0YRUj-HMW5LQZ2YeeJgyDqmJTpnzRkGmFLch0';
  
function makeCopy(id, name) {
    let file = Driveapp.getFilebyId(id);
    let copy = file.makeCopy(name);
    return copy.getId()

}

console.log(makeCopy(contractId, 'Copy of the contract'));

Write DriveApp instead of driveapp and write getFileById instead of getFilebyId.

Look at the “a” in “DriveApp” and at the “b” in “getFileById”