title: Copy several sheets from one spreadsheet to another spreadsheet
body: <p>I'm trying to create a script to copy several sheets from one (unopened) spreadsheet to another (active spreadsheet). </p>
  
  <p>I have searched through other solutions and using some other suggestions I have put together this piece of code. I have no programming experience and just started learning Javascript when my school started using Google apps.</p>
  
  <p>It has taken me about 5 hours get this far and I think I am almost there, but I can't get it to work.</p>
  
  <pre><code>function copySheets() {
  
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var infoPageSheet = ss.getSheetByName("infoPage");
      var sheetID = infoPageSheet.getRange("B11").getValues().toString(); //gets the ID of the spreadsheet that contains the sheets I need to copy from cell B11 
      var classSelect = infoPageSheet.getRange("B12:B17").getValues().toString().split(","); // gets the names of the sheets I need to copy (one name per cell)
      var stuNameList = SpreadsheetApp.openById(sheetID); // opens the target sheet
  
      for (var i = 0; i &lt; classSelect.length; i++) {
          var copy = stuNameList.getSheetByName(classSelect[i]);
          if (copy) {
              Logger.log("copy " + classSelect[i] + " already exists");
          } else {
              stuNameList.copyTo(ss).setName(classSelect[i]).showSheet();
          } 
      }
  }
  </code></pre>
  
  <p>I will input the ID and the names of the sheets I want to copy into the active spreadsheet. I will run the script and the script should get the sheets from the source file (unopened) and copy them to the active spreadsheet with the same name.</p>
  
  <p>Please feel free to be as critical of my code as you want, any help would be greatly appreciated.. </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I have got this working now </p>
  
  <pre><code>function getVars() {
  
     var ss = SpreadsheetApp.getActiveSpreadsheet();
     var sortSheet = ss.getSheetByName("Sorted Scores");
     var infoPageSheet = ss.getSheetByName("infoPage");
     var sheetID = infoPageSheet.getRange("B11").getValue().toString();
     var classSelect1 = infoPageSheet.getRange("B12:B18").getValues().toString().split(",");
  
     var classSelect = new Array();
       for (var i = 0; i &lt; classSelect1.length; i++) {
          if (classSelect1[i]) {
            classSelect.push(classSelect1[i]);
    }
  }
         var stuNameList = SpreadsheetApp.openById(sheetID);
  
        Logger.log(sheetID)
         for (var i = 0; i &lt; classSelect.length; i++) {
          var get = (classSelect[i]);
          var copy = ss.getSheetByName(classSelect[i]);
             if (copy) {
               Logger.log("copy " + classSelect[i] + "already exists");
           } else {
              stuNameList.getSheetByName(get)
              .copyTo(ss)
              .setName(get);
               Logger.log(classSelect)
      }
    }
  }
  </code></pre>