Google Apps脚本 - 如何在多个容器中安装一个脚本?

时间:2013-09-15 15:22:57

标签: google-apps-script containers google-docs

我有一个绑定到文档的容器绑定的Google Apps脚本,它工作正常,添加菜单,工作正常。我的问题是,如何在每个文档中自动运行容器绑定脚本?

此外,是否可以制作Chrome扩展程序以修改Google文档,如果可以,我该怎么做?

这是我的代码:

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));
    }
  }
}

1 个答案:

答案 0 :(得分:1)

除非您手动将脚本添加到每个文档,否则无法进行此操作。对于以后创建的文档,您可以拥有模板文档(模板doc具有脚本)并复制它而不是创建新文档。