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
Here is my code:
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));
}
}
}
When I select School Heading > A Block > English I get 'Script function not found engA'
My Question is, why is eval not working, and is it possible for me to pass an anonymous function to Menu.addItem?