Google Drive
Recently, we did a job with Google Drive files. It’s a great cloud storage, where we can store documents, spreadsheets, presentations, etc. You can use up to 15GB totally free (just notice that this space is also connected to your Gmail account) after that 100GB will cost ~2$/month. The biggest advantage of Google Driver over e.g. Dropbox is a built-in document editor. It’s almost like Office set, but completely located in the cloud. Some companies have resigned from the standard Office set, and use only Google Drive. Today I would like to start a cycle of episodes describing more advanced scenarios using JavaScript!
How to find the script editor?
Open any spreadsheet document, then find Script editor section:
I’m assuming that you have none or very little knowledge about programming, especially in JS, so I will try to describe everything to non-programmers as well;)
After opening the Script editor you should see something like this:
Notice the clear editor, with a few buttons. The editor should create for you a blank gs called Code and empty function.
Backup code
Now it’s time to implement some logic, let’s make a backup of the specific file, the code should look like this:
function backupQuestionsSpreadsheet() {
var local_date = new Date().toLocaleDateString()
var backupFolder = DriveApp.getFolderById("source_file_id");
DriveApp.getFileById("target_file_id").makeCopy("Backup - " + local_date, backupFolder);
}
Let’s point out some major steps:
- Rename the function to be more descriptive
- Line 2 is responsible for getting the actual date (day/month/year) and assigning it to a variable called local_date
- In line 3 we are getting a handler to our source folder, instead of „source_file_id” you should provide a valid directory ID. How to find it?
when you are in the directory, your last part of the link will show you directory ID. - Next line is responsible for copying our file to a newly created directory, instead of „target_file_id” you should paste a file which you want to backup. How to find file ID?
it’s a little bit more tricky, after opening a file ID will be after /d/ substring up to the next slash symbol.
And that’s all regarding the programming! Only 4 lines of code 🙂
Now you should run click button (play icon) and check if our script is working correctly.
Our file should appear in the newly created folder.
How to run this script once a week?
What if we wouldn’t like to run this script manually? The solution is pretty easy, all you have to do is to set a timer. Click on the timer icon:
then in a dialogue set a week period (or whatever you want) when the script should be invoked. Be sure to choose the appropriate function file if you have more than 1 function in the scope.