Quickstart: Add-on for Google Docs

Complete the steps described in the rest of this page, and in about five minutes you'll have created a Google Docs add-on that translates text in a sidebar. To see what the add-on looks like when it's finished, just install Translate from the Google Docs add-ons store.

Set it up

  1. Create a new Google Doc.
  2. From within your new document, select the menu item Tools > Script editor. If you are presented with a welcome screen, click Blank Project.
  3. Delete any code in the script editor and paste in the code below.

    Open code in new window

  4. Create a new file by selecting the menu item File > New > Html file. Name the file "Sidebar".
  5. Delete any code in the new editor tab and paste in the code below.

    Open code in new window

    <!DOCTYPE html>
    <html>
      <head>
        <base target="_top">
        <link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
        <!-- The CSS package above applies Google styling to buttons and other elements. -->
    
        <style>
        .branding-below {
          bottom: 56px;
          top: 0;
        }
    
        .branding-text {
          left: 7px;
          position: relative;
          top: 3px;
        }
    
        .col-contain {
          overflow: hidden;
        }
    
        .col-one {
          float: left;
          width: 50%;
        }
    
        .logo {
          vertical-align: middle;
        }
    
        .radio-spacer {
          height: 20px;
        }
    
        .width-100 {
          width: 100%;
        }
        </style>
      </head>
      <body>
        <div class="sidebar branding-below">
          <form>
            <div class="block col-contain">
              <div class="col-one">
                <b>Selected text</b>
                <div>
                  <input type="radio" name="origin" id="radio-origin-auto" value="" checked="checked">
                  <label for="radio-origin-auto">Auto-detect</label>
                </div>
                <div>
                  <input type="radio" name="origin" id="radio-origin-en" value="en">
                  <label for="radio-origin-en">English</label>
                </div>
                <div>
                  <input type="radio" name="origin" id="radio-origin-fr" value="fr">
                  <label for="radio-origin-fr">French</label>
                </div>
                <div>
                  <input type="radio" name="origin" id="radio-origin-de" value="de">
                  <label for="radio-origin-de">German</label>
                </div>
                <div>
                  <input type="radio" name="origin" id="radio-origin-ja" value="ja">
                  <label for="radio-origin-ja">Japanese</label>
                </div>
                <div>
                  <input type="radio" name="origin" id="radio-origin-es" value="es">
                  <label for="radio-origin-es">Spanish</label>
                </div>
              </div>
              <div>
                <b>Translate into</b>
                <div class="radio-spacer">
                </div>
                <div>
                  <input type="radio" name="dest" id="radio-dest-en" value="en">
                  <label for="radio-dest-en">English</label>
                </div>
                <div>
                  <input type="radio" name="dest" id="radio-dest-fr" value="fr">
                  <label for="radio-dest-fr">French</label>
                </div>
                <div>
                  <input type="radio" name="dest" id="radio-dest-de" value="de">
                  <label for="radio-dest-de">German</label>
                </div>
                <div>
                  <input type="radio" name="dest" id="radio-dest-ja" value="ja" checked="checked">
                  <label for="radio-dest-ja">Japanese</label>
                </div>
                <div>
                  <input type="radio" name="dest" id="radio-dest-es" value="es">
                  <label for="radio-dest-es">Spanish</label>
                </div>
              </div>
            </div>
    
            <div class="block form-group">
              <label for="translated-text"><b>Translation</b></label>
              <textarea class="width-100" id="translated-text" rows="10"></textarea>
            </div>
    
            <div class="block">
              <input type="checkbox" id="save-prefs">
              <label for="save-prefs">Use these languages by default</label>
            </div>
    
           <div class="block" id="button-bar">
              <button class="blue" id="run-translation">Translate</button>
              <button id="insert-text">Insert</button>
            </div>
          </form>
        </div>
    
        <div class="sidebar bottom">
          <img alt="Add-on logo" class="logo" src="https://googledrive.com/host/0B0G1UdyJGrY6XzdjQWF4a1JYY1k/translate-logo-small.png" width="27" height="27">
          <span class="gray branding-text">Translate sample by Google</span>
        </div>
    
        <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
        </script>
        <script>
          /**
           * On document load, assign click handlers to each button and try to load the
           * user's origin and destination language preferences if previously set.
           */
          $(function() {
            $('#run-translation').click(runTranslation);
            $('#insert-text').click(insertText);
            google.script.run.withSuccessHandler(loadPreferences)
                .withFailureHandler(showError).getPreferences();
          });
    
          /**
           * Callback function that populates the origin and destination selection
           * boxes with user preferences from the server.
           *
           * @param {Object} languagePrefs The saved origin and destination languages.
           */
          function loadPreferences(languagePrefs) {
            $('input:radio[name="origin"]')
                .filter('[value=' + languagePrefs.originLang + ']')
                .attr('checked', true);
            $('input:radio[name="dest"]')
                .filter('[value=' + languagePrefs.destLang + ']')
                .attr('checked', true);
          }
    
          /**
           * Runs a server-side function to translate the user-selected text and update
           * the sidebar UI with the resulting translation.
           */
          function runTranslation() {
            this.disabled = true;
            $('#error').remove();
            var origin = $('input[name=origin]:checked').val();
            var dest = $('input[name=dest]:checked').val();
            var savePrefs = $('#save-prefs').is(':checked');
            google.script.run
                .withSuccessHandler(
                  function(textAndTranslation, element) {
                    $('#translated-text').val(textAndTranslation.translation);
                    element.disabled = false;
                  })
                .withFailureHandler(
                  function(msg, element) {
                    showError(msg, $('#button-bar'));
                    element.disabled = false;
                  })
                .withUserObject(this)
                .getTextAndTranslation(origin, dest, savePrefs);
          }
    
          /**
           * Runs a server-side function to insert the translated text into the document
           * at the user's cursor or selection.
           */
          function insertText() {
            this.disabled = true;
            $('#error').remove();
            google.script.run
                .withSuccessHandler(
                  function(returnSuccess, element) {
                    element.disabled = false;
                  })
                .withFailureHandler(
                  function(msg, element) {
                    showError(msg, $('#button-bar'));
                    element.disabled = false;
                  })
                .withUserObject(this)
                .insertText($('#translated-text').val());
          }
    
          /**
           * Inserts a div that contains an error message after a given element.
           *
           * @param msg The error message to display.
           * @param element The element after which to display the error.
           */
          function showError(msg, element) {
            var div = $('<div id="error" class="error">' + msg + '</div>');
            $(element).after(div);
          }
        </script>
      </body>
    </html>
    
  6. Select the menu item File > Save all. Name your new script "Translate Quickstart" and click OK (The script's name is shown to end users in several places, including the authorization dialog.)

Try it out

  1. Switch back to your document and reload the page.
  2. After a few seconds, a Translate Quickstart sub-menu will appear under the Add-ons menu. (If you chose a different name for your script, that name will appear instead.) Click Add-ons > Translate Quickstart > Start.
  3. A dialog box will appear and tell you that the script requires authorization. Click Continue. A second dialog box will then request authorization for specific Google services. Read the notice carefully, then click Allow.
  4. A sidebar will appear. To test it, type some text into your document, then select it and click the blue Translate button. To replace the text in the document, click Insert.

Publish

Since this is an example add-on, our tutorial ends here. If you were developing a real add-on, the last step would be to publish it for other people to find and install. To learn more, check out our add-ons guide.

Learn more

To continue learning about how to extend Google Docs with Apps Script, take a look at the following resources:

Send feedback about...

Apps Script
Apps Script