Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script - How To Have One Script In multiple containers?

I have a container-bound Google Apps Script that is bound to a document, it works fine, adds the menu, works fine. My question is, how do I automatically have the container bound script automatically run in EVERY DOCUMENT?

Also, Is it possible to make a chrome extension to modify google docs and if so how do I go about doing so?

here is my code:

var BLOCKS = "abcdefg";
var CLASSES = ["English", "History", "Science", "Writing", "Latin", "Math", "Study Skills"];
var FUNCTION_NAMES;
var global = this;
init();
for(var i = 0; i < BLOCKS.length; i++){
  defineFunctions.call(global, BLOCKS[i]);
}

/**
 * The onOpen function runs automatically when the Google Docs document is
 * opened. Use it to add custom menus to Google Docs that allow the user to run
 * custom scripts. For more information, please consult the following two
 * resources.
 *
 * Extending Google Docs developer guide:
 *     https://developers.google.com/apps-script/guides/docs
 *
 * Document service reference documentation:
 *     https://developers.google.com/apps-script/reference/document/
 */
function onOpen() {
  init();
  // Add a menu with some items, some separators, and a sub-menu.
  var menu = DocumentApp.getUi().createMenu('School Heading')
  for(var i = 0; i < BLOCKS.length; i++){
    block = BLOCKS[i];
    menu = menu.addSubMenu(DocumentApp.getUi().createMenu(block + " Block")
                           .addItem('English', 'eng' + block)
                           .addItem('History', 'his' + block)
                           .addItem('Science', 'sci' + block)
                           .addItem('Writing', 'wri' + block)
                           .addItem('Latin', 'lat' + block)
                           .addItem('Math', 'mat'  + block) 
                           .addItem('Study Skills', 'stu' + block));
  }
  menu.addToUi();
}

function onInstall() {
  onOpen();
}

function getName(){
  var contact = ContactsApp.getContact(Session.getActiveUser().getEmail());
  var name;
  if(contact.getNickname()){
    name = contact.getNickname();
  } else {
    name = contact.getGivenName();
  }
  name += " ";
  name += contact.getFamilyName();
  return name;
}

function getFunc(class,block){
  return function(){
    createHeading(class,block);
  }
}

function defineFunctions(block){
  Logger.log(FUNCTION_NAMES)
  for(var i = 0; i < FUNCTION_NAMES.length; i++){
    var funcName = FUNCTION_NAMES[i] + block;
    eval.call(global, "function " + funcName + " () { createHeading('"+ CLASSES[i] + "', '" + block + "'); }");
  }
}

function formatDate(date){
  // September 26, 2013
  date = new Date();
  var string = "";
  Logger.log(date.toLocaleDateString());
  //return date;
}

function createHeading(class, block){
  var header = DocumentApp.getActiveDocument().getHeader();
  if(!header){
    header = DocumentApp.getActiveDocument().addHeader();
  }
  var name = Session.getActiveUser();
  header.insertParagraph(0, getName() + "\n{class}, {block} Block\n{date}".replace("{class}", class).replace("{block}", block).replace("{date}",(new Date()).toLocaleDateString())).setAlignment(DocumentApp.HorizontalAlignment.RIGHT);
}

function init(){
  if(!Array.isArray(BLOCKS)){
    BLOCKS = BLOCKS.toUpperCase().split("");
  }
  if(!Array.isArray(CLASSES)){
    CLASSES = CLASSES.split("\n");
  }
  if(!Array.isArray(FUNCTION_NAMES) || FUNCTION_NAMES.length !== CLASSES.length){
    FUNCTION_NAMES = [];
    for(var i = 0; i < CLASSES.length; i++){
      FUNCTION_NAMES.push(CLASSES[i].toLowerCase().substring(0,3));
    }
  }
}
like image 644
Ari Porad Avatar asked Sep 15 '13 15:09

Ari Porad


1 Answers

It is not possible unless you add the script to each document manually. For documents that are created henceforth, you can have a template document (the template doc has the script) and make a copy of it instead of creating a new document.

like image 179
Srik Avatar answered Sep 19 '22 08:09

Srik