Search code examples
google-apps-scriptgoogle-chrome-extensiongoogle-docs-api

How to build a simple journaling applications with Google-apps


As the title said I want to build a simple application that has the following features:

  1. It is activated by a menu item on the Chrome browser.
  2. When clicked it will open a file with the current date in a specific folder of my Google Docs.

    a) If the file exists, then:

    • If already opened, bring it on focus
    • Otherwise, open it

    b) Otherwise, create the file with the current date in the specified directory.

  3. Add a facility to search in the journal

I know how to implement most of the features, except the following:

  • How to link a script to menu-item (maybe with a blank doc which just holds a script)

  • How to bring an open doc to focus.

I tried to use a single file with a date insertion in tools, but after size increased, it became unusable. All the journaling systems I found, did not provide what I was looking for.


Solution

  • An easy solution that would accomplish all your needs but bringing the file into focus when it is already open, is the following:

    Creating a Google Apps Script WebApp.

    The idea here is that you can easily create a GAS WebApp that you can add to your browser's Bookmars Bar. Upon clicking it, it will create the daily journal file if needed and then redirect you to it. You can see the code below:

    var TIMEZONE = 'GMT';
    var FOLDER_ID = 'YOUR_FOLDER_ID';
    var FILE_NAME_FORMAT = 'yyyy-MM-dd';
    
    function createDocFile(fileName, folder) {
      return Drive.Files.insert({
        "title":    fileName,
        "mimeType": "application/vnd.google-apps.document",
        "parents":  [{"id": folder.getId()}]
      }).alternateLink;
    }
    
    
    function doGet() {
      var today = new Date();
      var fileName = Utilities.formatDate(today, TIMEZONE, FILE_NAME_FORMAT);
    
      var fileURL;
      var folder = DriveApp.getFolderById(FOLDER_ID);
      var fileIterator = folder.getFilesByName(fileName);
    
      if (fileIterator.hasNext()) {
        fileURL = fileIterator.next().getUrl();
      } else {
        fileURL = createDocFile(fileName, folder);
      }
    
      return HtmlService.createHtmlOutput('<script>window.location.href = "' + fileURL + '";</script>');
    }
    

    In order to make it work for you, you will have to adapt the variables declared on top as you desire.

    Further to that, you will also need to enable the Drive API Advanced Google Service. To do so, you can follow the following steps:

    • From the script editor, go to Resources > Advanced Google Services.
    • In the window that will open, search for "Drive API" and enable it. Click OK.

    Finally, you can deploy it going to Publish > Deploy as web app. Make sure to copy the URL that will show there and create a Bookmark in the Bookmark bar with that URL.

    Alternative

    If you really want to make it work such that the open document focuses when it already exists on your browser, you will definitely have to look into creating Google Chrome extensions. In order to make this check work, you may be interested in using the Google Chrome extension's windows api.