title: copy spreadsheet and add trigger to new one
body: <p>I have a large number of simple spreadsheets (each shared with a different agent), and one large "management" spreadsheet that is used to interface with all of them.</p>
  
  <p>When adding a new agent, I use a script (bound to the management-spreadsheet), to copy and rename a template-spreadsheet. The template-spreadsheet has an onEdit trigger which is <em>not</em> copied to the new instance. When I try to add it after copying (see script below), it is added to the management-spreadsheet instead of the new spreadsheet.</p>
  
  <pre><code>function NewAgent(filename) {
    // create new spreadsheet from template
    var folder = DriveApp.getFoldersByName(AgentFolder).next();
    var tmplt = DriveApp.getFilesByName(AgentTemplate);
    var newfile = tmplt.next().makeCopy(filename, folder );
    var newspread = SpreadsheetApp.openById( newfile.getId() );
  
    ScriptApp.newTrigger("MyFunction").forSpreadsheet(newspread).onEdit().create();
  
    SpreadsheetApp.flush();
  }
  </code></pre>
  
  <p>The function <code>MyFunction</code> is bound to the template (and also appears in the script bound to the new spreadsheet).
  The function <code>NewAgent</code> is, as stated bound to the management script.
  The result is that the trigger is added to the management-spreadsheet...</p>
  
  <p>ideas?</p>
  
  <p>Thanks</p>