根据当月,我打算在打开电子表格时激活月度表。
问题是在脚本结束时,选择工作表失败。似乎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();
}
答案 0 :(得分:10)
你的脚本可以简单得多......
试试这样:(所有标记为;// can be removed
的行确实可以删除,仅用于演示目的)
function selectmonth(){
var now= new Date();
var month = now.getMonth();
Logger.log(month);// can be removed
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = [];// can be removed
var sheets = ss.getSheets();// can be removed
for( var n in sheets){;// can be removed
sheetNames.push(sheets[n].getName());// can be removed
};// can be removed
var mysheet = ss.getSheets()[month]
ss.setActiveSheet(mysheet);
Browser.msgBox("this sheet is "+sheetNames[month]);// can be removed
}
“短”版本:
function selectmonth(){
var now= new Date();
var month = now.getMonth();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mysheet = ss.getSheets()[month]
ss.setActiveSheet(mysheet);
}
编辑:如果你真的想按照他们的名字获取工作表(如果你不能确定工作表顺序)你可以像下面那样实现它(这仍然比你的代码简单):
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 is "+sheetNames[month]);// can be removed
}
编辑2:,因为您似乎一直遇到问题,让我建议第三个版本利用您的sheetNames中包含的数字并找到正确的表格,即使它们没有排序......就是这样:
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 is the number included in your sheetName, with that we can get the sheet in the following loop.
if(index == month+1){mysheet = ss.getSheets()[n] ; break}
}
ss.setActiveSheet(mysheet);
Browser.msgBox("this sheet is "+mysheet.getName());
}