Automating the boring tasks in Google Drive

Have you ever got bored doing a monotonous task over and over again, on google drive, and wish there was a simpler and less repetitive way to get these tasks run. Google provides a scripting language called Google Apps Script for that very reason, which lets you automate the boring stuff. There are other ways to automate tasks in Google Drive such as using external libraries/modules in programming languages like Python. One of the many libraries in Python for this is PyDrive, which lets you use the Google Drive API to automate your own tasks. For this blog tutorial, we will be sticking to the Google Apps Script which doesn’t require any additional software as you can write this all in your browser. Google Apps Script is a flavour of JavaScript and has a very similar syntax to JavaScript, while also allowing you to do Google Drive specific operations easily.

Through this blog tutorial, we will explore how to create some simple automated operations to do which you can expand on afterwards.

To start go to your Google Drive homepage at https://drive.google.com/drive/my-drive.

Click on “New”, then press “more” at the bottom of the pop-up. You should now see an option called “Google App Script” click on this.

This should open a screen like the screenshot below.

1.jpg

Automating creating a new GoogleDoc and sharing

Often when you are working in a workplace or in a team project you might be having to create multiple Google Drive documents and sheets. This often requires sharing with the same multiple people which can become quite annoying after a while. Especially if it’s there is a long list of people. So we are going to create a script to do this for us!

Firstly, once we have created a new Google App Script document, edit the project name to whatever you would like. I’m going to call mine “Create GDoc for project”. This might take a few seconds to be done. If you have an error try creating a new project instance by clicking “File” => “New” => “Project”.

Once you have created a project you will see a file called Code.gs which has a function called “myFunction()”. Feel free to delete this as we will start with a blank file. 

We will start by creating a function to do the operation.

function createDocument() {

}

We have created the function so let’s create the new document. As we are using the Google App Script language Google has made it really easy to create new documents by using the “DocumentApp” class.

function createDocument() {
  var doc = DocumentApp.create('Daily Briefing');
}

Now for the last step of our simple script, let’s share the document with a set of predefined users. Firstly we need to get the document file id. We can get this easily by using the “getId()“ method with our doc object. Afterwards, we are going to use an array to store the other user’s emails. Using a for loop we will, one by one, add each new user as an editor to the document by the file id.

function createDocument() {
  var doc = DocumentApp.create('Daily Briefing');
  var fileId = doc.getId();
  
  var editors = ["user1@gmail.com", "user2@gmail.com"];
  
  for (var i=0; i<editors.length; i++) {
    DriveApp.getFileById(fileId).addEditor(editors[i]);
  }
}

To run the finished program press the run button at the top of the window. When you first run the program for the first time you might get a popup like below.

3.PNG

If this happens follow the steps to authorise your app to run. And allow the app to access it’s required permissions.

We have now finished showing you how to do this basic example, but that shouldn’t stop you looking further. Why not create your own scripts for your own need or edit this code yourself with the help of the Google App Script Guides to add new features. You could modify the code so it’s run at certain times of the day or you could even have the document set up with a predefined style already.

Do you want to learn more?

If you enjoyed this tutorial and want to find out more about programming and scripting why not join one of our Online Summer Camps for Kids and Teens.


Author: Robert Nimmo