Google Apps 脚本 - 如何在多个容器中拥有一个脚本? [英] Google Apps Script - How To Have One Script In multiple containers?

查看:22
本文介绍了Google Apps 脚本 - 如何在多个容器中拥有一个脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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() + "
{class}, {block} Block
{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("
");
  }
  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));
    }
  }
}

解决方案

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.

这篇关于Google Apps 脚本 - 如何在多个容器中拥有一个脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆