3


我正在为一所学校构建一个容器绑定的谷歌应用程序脚本。学校要求每个书面项目都有“学校标题”。学校使用积木 AG 作为句号。我的目标是会有一个菜单“School Heading”,打开后会有子菜单“A Block”、“B Block”、“C Block”,并且在每个子菜单中每个班级都有一个选项,其中将该类的标题插入到文档的标题中

这是我的代码:

var BLOCKS = "abcdefg";
var CLASSES = ["English", "History", "Science", "Writing", "Latin", "Math", "Study Skills"];
var FUNCTION_NAMES;
var global = {};

init();

/**
 * 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];
    Logger.log("Block: " + block)
    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));
    defineFunctions(block, this);
  }
  menu.addToUi();
}

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

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

function createHeading(class, block){
  var header = DocumentApp.getActiveDocument().getHeader();
  if(!header){
    header = DocumentApp.getActiveDocument().addHeader();
  }
  header.insertParagraph(0, "Name\n{class}, Block {block}".replace("{class}", class).replace("{block}", block)).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));
    }
  }
}

当我选择School Heading > A Block > English我得到'Script function not found engA'

我的问题是,为什么 eval 不起作用,我是否可以将匿名函数传递给 Menu.addItem?

4

2 回答 2

5

我相信 eval 可能正在工作,但不能保证它与调用菜单时执行的脚本实例相同。在您准备好调用菜单之前,Google Apps 脚本无法将所有这些功能保留在内存中,因此到那时,您已经有了一个干净的状态,并且不再引用所有这些功能。

在这个阶段不可能传递匿名函数/参数,它被归类为增强:https ://code.google.com/p/google-apps-script-issues/issues/detail?id=477 不多虽然在这方面取得了进展。

你为什么不使用侧边栏而不是菜单,在那里你可以创建一个带有选择器的 UI,基于它可以插入正确的标题。一旦你有了这个机制,你就可以将侧边栏用于其他很酷的东西。

https://developers.google.com/apps-script/guides/docs

于 2013-09-15T03:25:53.927 回答
3

eval()在函数的循环内定义函数for,因此范围规则意味着它们在该范围之外不可用。通过将该代码移到所有其他函数之外,eval()'d 函数将可用于脚本的每个运行实例。

这有效:

var BLOCKS = "abcdefg";
var CLASSES = ["English", "History", "Science", "Writing", "Latin", "Math", "Study Skills"];
var FUNCTION_NAMES;
var global = {};

init();
/// Moved
for(var _i = 0; _i < BLOCKS.length; _i++){
var _block = BLOCKS[_i];
  for(var _j = 0; _j < FUNCTION_NAMES.length; _j++){
    var _funcName = FUNCTION_NAMES[_j] + _block;
    eval("function " + _funcName + " () { createHeading('"+ CLASSES[_j] + "', '" + _block + "'); }");
  }
}
///
debugger;  // Pause in debugger (All the functions are there!)

/**
 * 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() {
  // 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];
    Logger.log("Block: " + block)
    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));
    //defineFunctions(block, this);
  }
  menu.addToUi();
}

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

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

function createHeading(class, block){
  var header = DocumentApp.getActiveDocument().getHeader();
  if(!header){
    header = DocumentApp.getActiveDocument().addHeader();
  }
  header.insertParagraph(0, "Name\n{class}, Block {block}".replace("{class}", class).replace("{block}", block)).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));
    }
  }
}
于 2013-09-15T04:15:37.330 回答