在Google Apps脚本中传递参数 [英] passing arguments in google apps script
问题描述
我正在尝试简化我的Google Apps脚本代码并提高其可读性.为此,我决定不再编写每次要在代码中激活新工作表的setActiveSheet()
函数,而是决定编写一个可以完成此工作的函数,只需调用该函数并将工作表的名称作为参数,例如这个:
I am trying to simplify my google apps script code and increase its readability. To this end, instead of calling setActiveSheet()
function each time I want to activate a new sheet in my code, I decided to write a function that does the job and just call that function and include the name of the sheet as an argument like this:
function setSheet (sheetName) {
var sheetName;
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(sheetName));
var sheet = spreadsheet.getActiveSheet();
};
然后例如,如果我要激活名为学生"的工作表,我将编写以下简单代码:
Then for example if I want to activate the sheet named "Students", I would write the following simple code:
setSheet("Students");
以上代码无效.有帮助吗?
The above code won't work. Any help?
推荐答案
我确实检查了上面的代码,对我来说很好用.我将附上使用的代码段.
I did check up the above code and it works fine to me. I will attach the code snippet used.
function setSheet (sheetName) {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName(sheetName));
var sheet = spreadsheet.getActiveSheet();
}
function myfunction(){
setSheet("Students");
}
我还注意到,函数终止后,还使用了多余的分号.
I also noted there is an extra semi-colon used after the function is terminated which is not required.
这篇关于在Google Apps脚本中传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!