passing arguments in google apps script

假如想象 提交于 2019-12-10 00:21:47

问题


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?


回答1:


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.




回答2:


Why are you declaring a variable in your function that has the same name as the function parameter that you are passing? This will reset the value that was just passed. Try removing var sheetName; from your function.



来源:https://stackoverflow.com/questions/51131119/passing-arguments-in-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!