This project is a continuation of my independent learning journey with LinkedIn Learning, I completed the “Google Apps Script for JavaScript Developers” course by Jesse Freeman.

By taking this course with JavaScript and Software Engineering foundations, I was able to easily understand how Google’s Apps Script works, its use cases, and how it’s both similar and different to JavaScript. I highly recommend checking out the course on LinkedIn Learning if you don’t know how to use Google Apps Script yet, because it goes into great depth for a tutorial.
Within this course, I learned the following:
- How to use Google Apps Script’s development environment
- .gs (Google) differs to .js (JavaScript). They appear similar, but .gs is back-end (server-side) and .js is front end (user-side).
- Therefor, all .gs code is executed on the server end (not on your local machine).
- How to utilize Logger.log() for more efficient debugging.
- .gs methods are created globally, allowing usage of functions in other script files to be used universally.
What you’ll need to run this code:
- A Google Account with access to Google Apps Script (almost any Gmail account will work). The previously hyperlinked LinkedIn Learning course will explain how to enable Google Apps Script.
- A Desktop Computer with access to Google Drive via a web browser.
- Code.gs code snippet (copied below)
- index.html Form Webpage code snippet (copied below)
Code.gs (file code)
// This is necessary for the HTML form to load
function doGet(e) {
return HtmlService.createHtmlOutputFromFile('index.html');
}
function formFunction(formData) {
// Get the ID of the document you would like to duplicate
var fileURL = formData.filelink;
var fileID = url_selection(fileURL);
// Find the document
var file = DriveApp.getFileById(fileID);
/*// Make is parseable -- ended up not needing
var googleDoc = DocumentApp.openById(file.getId());*/
// Get the number of duplicates
var dup = formData.fileduplnum;
for (var idx=0; idx < dup; idx++){
file.makeCopy();
}
}
function url_selection(url_str){
// Get the ID out of the docs.google.... link
var url_blocks = url_str.split("/")[5];
return url_blocks;
}
index.html (file code)
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<style>
h1 {text-align: center; font-family: cursive; border: outset;}
p {color: black; font-size: larger;}
button {font-size: x-large; font-family: cursive;}
</style>
</head>
<body>
<script>
function completed(){
alert("If your input is valid, check your Google Drive.");
}
</script>
<h1>Google Drive File Duplicator</h1>
<!-- Next Line Credit to https://stackoverflow.com/a/49183599 for reminding me about event.preventDefault() so it doesn't turn to a blank page -->
<form id="duplicateForm" onsubmit="event.preventDefault(); google.script.run.formFunction(this); return completed();">
<p>1. Please go to the document you would like to copy.<br>
2. Click "Share".<br>
3. Click "Copy Link".<br>
4. Return Here and paste the link:</p>
<input type="text" name="filelink">
<p>5. Type the number of duplicates you would like to make (i.e. 4):</p>
<input type="number" name="fileduplnum">
<br><br>
<button type="submit" value="Submit">Duplicate</button>
<p id="stat"></p>
</form>
<br>
</body>
</html>
With these two scripts copied into a Google Apps Script project, simply find a document in Google Drive you want to copy, Right-click the document you want to duplicate, and click “Copy Link” (since we will be doing this from our own accounts, the file does not need to be public).
Now in the Google Apps Script click Publish >> Deploy as web app. Give the app a version number and a name and then you can continue. Copy the link and run it in a new tab, and it should work for you as webpage instead of code!
For those who are not aware, LinkedIn Learning is an educational library that is inundated with countless helpful courses to learn new skills. You may be familiar with LinkedIn Learning if you have previous used Lynda tutorials. Courses include all kinds of business, creative, and technological skills — such as software, audio engineering, business analysis, and more.
If you attend a University you may be eligible for a free subscription through your institution. If not, you might also be able to access courses through your local public library branch (or via your workplace).
Tags: #LiLChampions2021 #LinkedInLearning #QuickTips