Difference between revisions of "Google Docs - Creating an Add On"

From NoskeWiki
Jump to navigation Jump to search
(Created page with "==About== {{DaughterPage|mother=Google Docs, and is related to: JavaScript}} This demos a <b>Google Docs Add On</b> started by a fellow Googler, Ye Yang, and then I g...")
 
Line 4: Line 4:
 
This demos a <b>Google Docs Add On</b> started by a fellow Googler, Ye Yang, and then I generalized it because I realized I wanted to turn it into this article.  :)
 
This demos a <b>Google Docs Add On</b> started by a fellow Googler, Ye Yang, and then I generalized it because I realized I wanted to turn it into this article.  :)
  
[[image:Google_docs_addon_meeting_notes_1000.png|thumb|center|1000px|What the thing looks like after a run]]
+
[[image:Google_docs_addon_meeting_notes.png|thumb|center|701px|What the thing looks like after a run]]
  
  

Revision as of 20:02, 11 September 2019

About

NOTE: This page is a daughter page of: Google Docs, and is related to: JavaScript


This demos a Google Docs Add On started by a fellow Googler, Ye Yang, and then I generalized it because I realized I wanted to turn it into this article. :)

What the thing looks like after a run


Background and Instructions

The right API to help is the Google Docs API... but that's slow to process, so here's what you need to do:

  1. Create a brand new Google Doc ........................................... (tip: Try typing into Chrome: docs.new)
  2. Click menu bar: Tools > Script Editor.
  3. Enter the code into the Script Editor and save .................... (tip: You can hit the play button to test one of the functions before saving)
  4. Reload your Google Doc and approve permissions.
  5. Run the Script by clicking the new menu item that appears: Utilities > Start Meeting Notes for Today.


Google Docs Add On - Meeting Notes Generator

Code.gs

/**
 * Deault list of team members as a comma separated string.
 * @type {string}
 */
var DEFAULT_TEAM_MEMBERS = "amitbehal,anoske,scalman,berkiten,sloth,yeya";

/**
 * Randomly shuffles given array.
 */
shuffle = function(arr, startOffset, endOffset) {
  var randFn = Math.random;
  for (var i = arr.length - 1 - endOffset; i > startOffset; i--) {
    var j = Math.floor(randFn() * (i + 1));      // Choose a random array index in [0, i] (inclusive with i).
    var tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
  }
};


/**
 * Called when doc opens. Creates menu iteam for "Insert Date".
 */
function onOpen() {
  // Add a menu item:
  DocumentApp.getUi().createMenu('Utilities')
      .addItem('Start Meeting Notes for Today', 'insertDateAndNames')
      .addItem('Change Default Names', 'displayInfo')
      .addToUi();
}


/**
 * Inserts the date as a title and randomly shuffles our team member names.
 */
function insertDateAndNames() {
  var doc = DocumentApp.getActiveDocument();
  var cursor = doc.getCursor();
  var body = doc.getBody();

  if (cursor) {
    // Insert date as heading:
    var dateString = Utilities.formatDate(new Date(), "GMT", "yyyy MMMM dd");
    var offset = body.getChildIndex(cursor.getElement());
    body.insertParagraph(++offset, dateString).setHeading(DocumentApp.ParagraphHeading.HEADING1);
    
    // Prompt for team members present:
    var ui = DocumentApp.getUi();
    var promptInstructions = 'Enter present usernames: \n\n.... if empty will use default as:\n\n' + DEFAULT_TEAM_MEMBERS;
    var response = ui.prompt('Team members present', promptInstructions, ui.ButtonSet.OK);
    var teamMembersString = response.getResponseText() == '' ? DEFAULT_TEAM_MEMBERS : response.getResponseText();
    Logger.log(teamMembersString);

    // Shuffle team members:
    var teamMembers = teamMembersString.split(',');    
    shuffle(teamMembers, /*startOffset=*/1, /*endOffset=*/0);  // First team member name is locked.

    // For each team member, add their name followed by a bullet point:
    teamMembers.forEach(function(person, i) {
      body.insertParagraph(++offset, person).setHeading(DocumentApp.ParagraphHeading.NORMAL);
      body.insertListItem(++offset, "...").setGlyphType(DocumentApp.GlyphType.BULLET);
    });
  } else {
    DocumentApp.getUi().alert('Cannot find a cursor in the document.');
  }
}


/**
 * Displays a popup with info about this plugin.
 */
function displayInfo() {
  var ui = DocumentApp.getUi();
  var popup = ui.alert('To modify the default team members:\n\
  1. Click Menu bar > Tools > Script Editor\n\
  2. Change "DEFAULT_TEAM_MEMBERS" (near the top of the script editor)\n\
  3. Save the script then reload the doc\n\n\
To learn more about creating fun addons like this try:\n\
  http://andrewnoske.com/wiki/Google_Docs_-_Creating_an_Add_On\n\n\
CREDIT: This script was created by Ye Yang and modified by Andrew Noske.');
}


Links

  • Apps Scripts Intro - A very concise easy, powerful example of creating a script... *but* this script is for Google Sheets, so limited help.