Add a translate sidebar to your Google Drive documents

I like using Google Drive; even so much that I’ve almost stopped using Word as my standard text processor. It’s just so easy to get used to editing your documents “in the cloud”. Just log in on some shady wi-fi network and start editing your documents!

The research sidebar is one of the well-implemented features of Google Drive, offering a sort-of “mini-google” in  a sidebar that automatically knows what you’re typing about, and offers some help on searching research articles, images and word definitions. Pretty amazing stuff! But what I noticed while typing my documents is that I was often flipping between another tab, namely the Google Translate tab. You could say my English vocabulary isn’t up-to-date and I should just spend more time learning English words… or I could make it easier for myself to quickly search for an English translation of a Dutch word, or any word in any language for that matter. So I chose for the last option, I wrote a small Google Apps Script that would let me, just like the research bar, quickly find the words I need. I don’t know why Google didn’t include this in the first place, it’s probably on a to-do list somewhere…


Copy the code from the box below. Keep your cursor to the right of the green line to make sure you don’t include all the line numbers, you don’t want to include those..

In your Google document in which you want to use the sidebar,go to Tools -> Script Editor. A new tab opens, with a programming interface. Remove all the dummy code you see, and replace it with what you just copied. Press CTRL + S to save the script. Switch back to your document and click Tools -> Script Manager. Now a list of “functions” appears that you can run. Select the top one called “onOpen” and press the “Run” button. If everything went well, you should now see an new menu item called “Extra”. In this “Extra” menu, there’s an item called “Translate Sidebar”. Click on it and enjoy!

Translate sidebar screenshot

This is how it will look like after running. Just like you'd expect


//Translate SideBar by Pepf, copy everything!

//adjust to modify available languages, make sure each array has the same amount of items!
var langs = ['English','French','Nederlands','Deutsch'];
var langcodes = ['en','fr','nl','de'];

function onOpen() {
  // Add a menu with some items, some separators, and a sub-menu.
      .addItem('Translate sidebar', 'showSidebar')

 * Shows a custom HTML user interface in a sidebar in the Google Docs editor.
function showSidebar() {
  // Display a sidebar with custom UiApp content.
  var uiInstance = UiApp.createApplication()
  .setTitle('Translate SideBar')
  var panel = uiInstance.createVerticalPanel();

  panel.add(uiInstance.createLabel('Text to translate'));

  //input panel
  var inputPanel = uiInstance.createHorizontalPanel();

  //input textbox
  var inputText = uiInstance.createTextBox().setName("inputText");

  //input language listbox
  var inputLanguage = uiInstance.createListBox().setName("inputLanguage").setId("inLang");
  ListBoxAttrs(inputLanguage).setSelectedIndex(2); //input starts as dutch ;)


  //Button to switch languages
  var switchLang_btn = uiInstance.createButton("⇵");

  panel.add(uiInstance.createLabel('Translated Text:'));

  //output panel
  var outputPanel = uiInstance.createHorizontalPanel();

  //output text area
  var outputTxt = uiInstance.createTextBox().setName("outputTxt").setId("outputTxt");

  //output language listbox
  var outputLanguage = uiInstance.createListBox().setName("outputLanguage").setId("outLang");


  var switchHandler = uiInstance.createServerHandler("Switchlanguages");

  var translateHandler = uiInstance.createServerHandler("translate");

  //auto translate on changing input or language, or switching language


 * Translate callback
function translate(eventInfo) {
   var parameter = eventInfo.parameter;

   //fetching input language from listbox
   var inputLang = parameter.inputLanguage;
   Logger.log("input lang: " + inputLang);

   //fetching output language from listbox
   var outputLang = parameter.outputLanguage;
   Logger.log("output lang: " + outputLang);

   var inputText = parameter.inputText;  // the value of inputText
   var english = LanguageApp.translate(inputText, inputLang, outputLang);
   //var outputTxt = parameter.outputTxt;
   var app = UiApp.getActiveApplication();
   return app;


 * Switchlanguages client callback
function Switchlanguages(eventInfo) {
   var parameter = eventInfo.parameter;   

  //fetching input language from listbox
   var inputLang = langcodes.indexOf(parameter.inputLanguage);

   //fetching output language from listbox
   var outputLang = langcodes.indexOf(parameter.outputLanguage);

   //Switch and change
   var app = UiApp.getActiveApplication();
   return app;


 * Add items and styles to listbox
function ListBoxAttrs(lb) {
  for (n=0; n<langs.length; n++){
  lb.setStyleAttribute("font-size", "0.8em")
  lb.setSize("auto", "30px");
  return lb;