title: spreadsheet script running with trigger and manually
body: <p>This should be simple but I'm stuck on this script...
  It has a function (createnewsheet) that runs manually <strong>AND</strong> on a time trigger so I had to choose <code>openById()</code> to access the spreadsheet but when I'm looking at the sheet and run the function manually I want to set the newly created sheet active and that's what is causing me trouble.</p>
  
  <p>When I run the function (createnewsheet) from the script editor everything is fine but when I call it from the spreadsheet menu I get this error message : <code>Specified sheet must be part of the spreadsheet</code> because of the last line of code.  AFAIK I'm not addressing a sheet outside my spreadsheet...  <em>Any idea what I'm doing wrong in this context ?</em></p>
  
  <p>Here is a simplified code that shows the problem, a <a href="https://docs.google.com/spreadsheet/ccc?key=0AnqSFd3iikE3dGVtYk5hWUZVUFNZMzAteE9DOUZwaVE" rel="nofollow">shared copy is available here</a></p>
  
  <pre><code>  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
  }
  </code></pre>