Google Apps脚本-将匿名函数传递给Menu.addItem [英] Google Apps Script - Pass Anonymous Function To Menu.addItem

查看:71
本文介绍了Google Apps脚本-将匿名函数传递给Menu.addItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在为学校构建容器绑定的Google Apps脚本.学校要求每个书面项目都有学校标题".学校使用A-G区块作为期间.我的目标是将有一个菜单"School Heading",在打开时它具有子菜单"A Block","B Block","C Block",并且在每个子菜单中每个类都有一个选项,会将该类的标题插入文档的标题


I am building a container bound google apps script for a school. The school requires every written item to have the 'school heading'. The school uses blocks A-G as periods. My goal is that there will be a menu, 'School Heading', when opened it has the submenus 'A Block', 'B Block', 'C Block', and in each submenu there will be an option for each class, which will insert the heading for that class into the document's heading

这是我的代码:

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

当我选择学校标题>块>英语时,我得到找不到脚本功能engA"

When I select School Heading > A Block > English I get 'Script function not found engA'

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

My Question is, why is eval not working, and is it possible for me to pass an anonymous function to Menu.addItem?

推荐答案

您的 eval()在函数的 for 循环内定义函数,因此,作用域规则表示它们在该范围之外不可用.通过将该代码移到所有其他函数之外, eval()函数将可用于脚本的每个正在运行的实例.

Your eval() is defining functions inside a for loop in a function, so the scoping rules mean that they just aren't available outside of that scope. By moving that code outside of all other functions, the eval()'d functions will be available to every running instance of the script.

这有效:

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

这篇关于Google Apps脚本-将匿名函数传递给Menu.addItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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