Writing Scripts for Google Drive

Thanks to tips from John Sonmez’s book “Soft Skills: The Software Developer’s Life Manual”, I write a weekly report to my supervisor.  This is great for my career on so many levels. My supervisor sees my progress on my work and also I have a record kept for annual reviews. I write my weekly reports in Google Docs, but it was becoming really repetitive and a time sink to write them up and keep them organized. So I wrote a script to help me with that.

The Google Apps Scripts are based on Javascript so it’s pretty easy to pick up and use. You can read more about Google Apps Scripts and getting started in the links below.

Here’s the format that I was aiming for:

Here’s the code:
Link to Pastebin

Folder ID Numbers can be located by traversing to the folder in Google Drive and then look at the URL: https://drive.google.com/drive/folders/FolderID#Here

I’m not a huge fan of Javascript so forgive me if its kind of ugly looking. It works so I’m satisfied with it in general. Okay so here’s what’s happening in the code:

  1. Create the document
  2. Get the date and rename the document
  3. Loop through and write weekdays into the document body and add bullet points below each day
  4. Copy the file to desired folder destination
  5. Delete old file in root folder (there is no built-in “move function” in Google App Script yet.)

Hope you found this helpful. Send me questions or comments to hey@charlesbickel.com or Tweet at me @charles_bickel

// Slice dates give double digit mm & dd
function sliceit(x) {
thing = ("0" + (x)).slice(-2);
return thing

// Get Monday
function dateDiff() {
var day = d.getDay(),
diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday
return diff

// Get date & create name of document
function setName() {
d = new Date();
diff = dateDiff(d);
thatmonday = new Date(d.setDate(diff));
thatfriday = new Date(d.setDate(thatmonday.getDate() + 4));
newformat = 'Completed ' + thatmonday.getFullYear() + '_' + sliceit(thatmonday.getMonth() + 1) + '/' +
sliceit(thatmonday.getDate()) + ' - ' + sliceit(thatfriday.getMonth() + 1) + '/' + sliceit(thatfriday.getDate())
return newformat

// Write each day of the week in document
function writeDays(step) {
d = new Date();
var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
eachday = new Date(d.setDate(diff + step));
eachdayformat = days[eachday.getDay()] + ' ' + sliceit((eachday.getMonth() + 1)) + '/' + sliceit(eachday.getDate()) + ':';
return eachdayformat

// Move from root to work folder
function moveFile(file, folder) {

function myFunction() {
// Create document
var folder = DriveApp.getFolderById("EnterYourFolderIDNumberHere");
var doc = DocumentApp.create(setName());
var file = DriveApp.getFileById(doc.getId());
var body = doc.getBody();

// For loop write days of the week and assign bullet points
for (step = 0; step < 5; step++){

moveFile(file, folder);