google apps script - Unable to activate a sheet by name -
i'm trying activate monthly sheet upon opening spreadsheet, according current month.
the problem @ end of script, when fails selecting sheet. seems getsheetbyname()
gets null
value, not accepted setactivesheet()
.
function selectmonth(){ var now= new date(); var month= now.getmonth()+1; var ss=spreadsheetapp.getactivespreadsheet(); var sheetname=""; switch (month){ case 1: sheetname="urtarrila01"; //english=january01 break; case 2: sheetname="otsaila02"; //english=february02 , on... break; case 3: sheetname="martxoa03"; break; case 4: sheetname="apirila04"; break; case 5: sheetname="maiatza05"; break; case 6: sheetname="ekaina06"; break; case 9: sheetname="iraila09"; break; case 10: etiketaizena="urria10"; break; case 11: sheetname="azaroa11"; break; case 12: sheetname="abendua12"; break; default: sheetname="laburpena-resumen"; } //here mysheet gets null value, although sheet exist, named "sheetname) var mysheet=ss.getsheetbyname(sheetname); //an here script fails, error message=invalid argument on next line ss.setactivesheet(mysheet); }
well final code works:
enter code here function hilehonetan(){ var now= new date(); var month= now.getmonth(); var mysheetname=""; var ss=spreadsheetapp.getactivespreadsheet(); var sheetnames= ['urtarrila01', 'otsaila02', 'martxoa03', 'apirila04', 'maiatza05', 'ekaina06', 'ekaina06', 'ekaina06', 'iraila09', 'urria10', 'azaroa11' , 'abendua12'] mysheetname= sheetnames[month]; var mysheet=ss.getsheetbyname(mysheetname); mysheet.activate(); }
your script more simple ... try : (all lines marked ;// can removed
can indeed removed , there demo purpose)
function selectmonth(){ var now= new date(); var month = now.getmonth(); logger.log(month);// can removed var ss = spreadsheetapp.getactivespreadsheet(); var sheetnames = [];// can removed var sheets = ss.getsheets();// can removed for( var n in sheets){;// can removed sheetnames.push(sheets[n].getname());// can removed };// can removed var mysheet = ss.getsheets()[month] ss.setactivesheet(mysheet); browser.msgbox("this sheet "+sheetnames[month]);// can removed }
and "short" version :
function selectmonth(){ var now= new date(); var month = now.getmonth(); var ss = spreadsheetapp.getactivespreadsheet(); var mysheet = ss.getsheets()[month] ss.setactivesheet(mysheet); }
edit : if want sheets names (in case cannot sure sheet order) implement below (which still quite simpler code) :
function selectmonth(){ var now= new date(); var month = now.getmonth(); logger.log(month); var ss = spreadsheetapp.getactivespreadsheet(); var sheetnames = ['sheet1','sheet2','sheet3','sheet4','sheet5','sheet6','sheet7','sheet8','sheet9','sheet10','sheet11','sheet12',];; logger.log(sheetnames) var mysheet = ss.getsheetbyname(sheetnames[month]); ss.setactivesheet(mysheet); browser.msgbox("this sheet "+sheetnames[month]);// can removed }
edit 2 : since seem keep getting issues let me suggest third version takes advantage of numbers included in sheetnames , find right sheet if not sorted... goes :
function selectmonth(){ var now= new date(); var month = now.getmonth(); var ss = spreadsheetapp.getactivespreadsheet(); var mysheet; var sheets = ss.getsheets(); for( var n in sheets){ var index = number(sheets[n].getname().replace(/[^0-9]/ig,'')); logger.log(index);// index number included in sheetname, can sheet in following loop. if(index == month+1){mysheet = ss.getsheets()[n] ; break} } ss.setactivesheet(mysheet); browser.msgbox("this sheet "+mysheet.getname()); }
Comments
Post a Comment