Google Apps Script: You Can Code (part 3)

Google Apps Script: You Can Code (part 3)

Google Apps Script (1)

 

Google Apps Script is a great way to get started with learning to program. You need to know very little JavaScript and are able to create really useful projects quickly.

For this tutorial we will NOT add any new JavaScript techniques. Check out the first tutorial where you changed the name of a Google Doc and the second tutorial where you created a Google Document out of thin air.

Project

In this tutorial you will create copies of a document in your Google Drive. What is nice about using a script instead of simply using file->make a copy is that the copy will be made in the same folder that the file is in rather than in MyDrive. You can also use the script to make multiple copies at once which is a real time saver.

script.google.com

Go to http://script.google.com to create a blank project.

Function

Delete the default function and write your own. I am naming my function copyDoc().

function copyDoc(){
}

DriveApp

When using Google Apps Script you need to call the type of document you are going to use. In this case we are going to be using files in Google Drive so we call the DriveApp. Read through the list of methods you can use in the DriveApp. This will give you ideas for script projects you might want to create.

Define a variable to call the DriveApp. For this project we are making a copy of a document. You will need the file ID of the document you are copying. To obtain this open the document and copy the string of numbers and letters that comes before the /edit.
google app id

Use the method getFileByID() appended to the DriveApp. Within the parenthesis paste the document ID. Make sure this text string is in quotations. Notice the semi-colon at the end of the line.

var drive=DriveApp.getFileById(‘paste ID here’);

Screen Shot 2015-03-14 at 5.47.29 PM

 

Make a Copy

Use the method makeCopy() to create a copy of the document. Remember to append it to your variable drive to make a copy of the document you had previously chosen. Notice the semi-colon at the end of the line.

drive.makeCopy();

Run Script

This project has 2 lines of code! Click on the save icon in the toolbar. I named my script CopyDocument. You will need to authorize the script. Once the script has saved use the Run menu to run your copyDoc function. Check your Google Drive to see that the document was indeed copied.
2015-03-14_17-54-55

Complete Code

function copyDoc() {
var drive=DriveApp.getFileById(‘1YsebxTFgSsbUqTTBOOgl4Y8PiCN31d9S54WrEa13tMI’);
drive.makeCopy();
}

Click Here for my sample code

Kick it Up a Notch

Making a single copy of a document is not that mind blowing, but if you have ever had to make a copy of the same document multiple times you will really appreciate a script that does this for you. I like to create badges with Google Drawing. I have a template for my badges that I have to copy sometimes 30 times for a project.

[expand title=”Click Here for directions on kicking it up a notch”]

Add to the Script

Take the script that you just created and we will add a couple of lines of code. Look at the 2nd tutorial for directions on making a loop.

Instead of making one copy we want to make multiple copies. This is a good use of a loop.

Loop

For the first line of your function start a for loop. Decide how many copies you want to make.

for(i=0; i<30; i++){
}

2015-03-14_18-27-03

Run Script

Just as you did before use the Run menu to run your copyDoc function. Check Google Drive to see that the document was created multiple times.

Full Script

function copyDoc() {
for(i=0; i<3; i++){

var drive=DriveApp.getFileById(‘1YsebxTFgSsbUqTTBOOgl4Y8PiCN31d9S54WrEa13tMI’);
drive.makeCopy();
}
}

Click Here for my sample script.

[/expand]

© 2024 All Rights Reserved.