根据当前月份,我正在尝试在打开电子表格时激活月度工作表。
问题出现在脚本的末尾,当它无法选择工作表时。似乎getSheetByName()
获得了一个null
不被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 and so 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 the sheet exist, named "sheetname)
var mysheet=ss.getSheetByName(sheetname);
//AN HERE THE SCRIPT FAILS, ERROR MESSAGE=invalid argument on next line
ss.setActiveSheet(mysheet);
}
好吧,这是有效的最终代码:
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();
}