这应该很简单,但我被困在这个脚本上......它有一个手动运行并在时间触发器上运行的函数(createnewsheet),所以我不得不选择openById()
访问电子表格,但是当我查看工作表并运行时手动功能我想将新创建的工作表设置为活动状态,这就是给我带来麻烦的原因。
当我从脚本编辑器运行函数(createnewsheet)时,一切都很好,但是当我从电子表格菜单中调用它时,我收到此错误消息:Specified sheet must be part of the spreadsheet
因为最后一行代码。AFAIK我没有在我的电子表格之外处理工作表...... 知道在这种情况下我做错了什么吗?
这是显示问题的简化代码,此处提供共享副本
var ss = SpreadsheetApp.openById('0AnqSFd3iikE3dGVtYk5hWUZVUFNZMzAteE9DOUZwaVE');
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sh = ss.getSheetByName('master')
var logsheet = ss.getSheetByName('logger')
var FUS1=new Date().toString().substr(25,6)+":00";
function onOpen() {
var menuEntries = [ {name: "Manual test", functionName: "createnewsheet"},
];
sheet.addMenu("Utilities",menuEntries);
SpreadsheetApp.setActiveSheet(logsheet);// this is working fine
}
function createnewsheet(){
var sheetName = "Control on "+ Utilities.formatDate(new Date(), FUS1, "MMM-dd-yy")
try{
var newsheet = ss.insertSheet(sheetName,2);// creates the new sheet in 3rd position
}catch(error){
FUS1=new Date().toString().substr(25,6)+":00";
var newsheet = ss.insertSheet(sheetName+' - '+Utilities.formatDate(new Date(), FUS1, "HH:mm:ss"),2);// creates the new sheet with a different name if already there
}
newsheet.getRange(1,1).setValue(sheetName);
SpreadsheetApp.setActiveSheet(ss.getSheets()[2]);// should make 3 rd sheet active but works only when run from script editor
// SpreadsheetApp.setActiveSheet(newsheet);// should have same result but works only when run from script editor
}