像这样的示例,如何使用内部的循环子菜单的Java脚本添加项目菜单".addItem(“转到此工作表","S" + i +"GoToS"))? [英] How to adding item menus with the java script of an loop's sub menus inside like this example ".addItem("Go to This Sheet", "S"+i+"GoToS")"?
问题描述
大家好..我有一个问题,关于在GAS中创建菜单(Google Apps脚本)以实施到Google电子表格,而没有三个脚本,花费了很长的路要走,也花费了我的精力,我创建了许多这样的脚本行.这是脚本.这是代码:
Hy, Everyone .. I have a question about creating the menu in GAS (Google Apps Scripts) to implement to a Google Spreadsheet without a three of the scripts which is has been take long long way and take my energy too and many many so many lines of the scripts like this I have created. This is the script. Here's the code :
function Menu1() {
var ui = s.getUi(),
s = SpreadsheetApp,
ss = s.getAcgtiveSpreadsheet(),
sss = ss.getSheets(),
madeMenu = ui.createMenu('Sheet Tools Just For an Example Menus');
for (var i=0; i < sss.length; i++){
madeMenu.addSubMenu(ui.createMenu(sss[i].getName())
.addItem('Go to ...', 'S'+i+'GoToS')
.addItem('Rename ...', 'S'+i+'RenameS')
.addItem('Move ...', 'S'+i+'MoveS'))
madeMenu.addToUi();
}
}
function GoToS(getSheetNumber) {
var sheet = sss[getSheetNumber];
ss.setActiveSheet(sheet);
}
这是我主要问题的解决方法! 由于这些脚本的结构如此,我不得不创建这行代码.参见以下内容:
function S0GoToS() {
GoToS(0)
}
function S1GoToS() {
GoToS(1)
}
function S2GoToS() {
GoToS(2)
}
function S3GoToS() {
GoToS(3)
}
function S4GoToS() {
GoToS(4)
}
function S5GoToS() {
GoToS(5)
}
问题是如何在不使用脚本的第三子代码的情况下创建它们? 我想,我希望还有另一种方法可以创建它们是的,我相信有,但这仅仅是因为我不知道这种方式.请有人应该可以帮助我解决这个问题.任何成就将不胜感激.在此先感谢您抽出宝贵的时间,我为英语差劲而感到抱歉.
The question is How to create them without the third-sub of the scripts ??? I thought and I hope there is the another way to create these for sure yes I believe there is but that just the because I don't know how about that way. Please someone chould be can help me to solve this case. Any achieves will be appreciated. Thanks in advance has taken in your time and I appologies for my poor english.
推荐答案
实际上,您可以动态生成这些函数.这个想法是要在全局"范围内将任何功能的for循环保持在外部之外,这将生成所有这些功能.之后,可以通过菜单操作来调用它们.您可能会如下所示:
You can, in fact, generate those functions dynamically. The idea is to keep a for-loop outside of any of your functions, in the "global" scope, which will generate all these functions. Afterwards, they can be called by a menu action. Your could would look like the following:
// function "constructors"
function createGoToFunction(sheetIndex) {
return function() {
var sheet = SpreadsheetApp.getActive().getSheets()[sheetIndex];
sheet.activate();
}
}
function createRenameFunction(sheetIndex) {
return function() {
// Your rename logic
}
}
function createMoveFunction(sheetIndex) {
return function() {
// Your move logic
}
}
// functions definition
this['ALL_SHEETS'] = SpreadsheetApp.getActive().getSheets();
for (i=0; i<this['ALL_SHEETS'].length; i++) {
this['S'+i+'GoToS'] = createGoToFunction(i);
this['S'+i+'RenameS'] = createRenameFunction(i);
this['S'+i+'MoveS'] = createMoveFunction(i);
}
delete this['ALL_SHEETS'];
delete this['i'];
function Menu1() {
var ui = SpreadsheetApp.getUi();
var sheets = SpreadsheetApp.getActive().getSheets();
var madeMenu = ui.createMenu('Sheet Tools Just For an Example Menus');
for (var i=0; i < sheets.length; i++){
var subMenu = ui.createMenu(sheets[i].getName())
.addItem('Go to ...', 'S'+i+'GoToS')
.addItem('Rename ...', 'S'+i+'RenameS')
.addItem('Move ...', 'S'+i+'MoveS');
madeMenu.addSubMenu(subMenu);
}
madeMenu.addToUi();
}
function onOpen() {
Menu1();
}
为了实现自己的功能,您只需更改定义在顶部的主体(例如,请参见createGoToFunction
).
In order to implement your own functionality for the functions, you just have to change the body of them defined on top (see createGoToFunction
as an example).
这篇关于像这样的示例,如何使用内部的循环子菜单的Java脚本添加项目菜单".addItem(“转到此工作表","S" + i +"GoToS"))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!