title: Why did my code break when I copied it over from one workbook into another?
body: <p><strong>So I just had someone on Fiverr write this cote for me and it works great! I had him write it on a copy of the one I needed for work. when I copied into the one that was assigned to me at work it breaks at line 40. Any and all fixes would be great!!</strong></p>
  <p>I think it doesn't have the correct sheet selected so it is kicking back a null error</p>
  <p>TypeError: Cannot read property 'getRange' of null</p>
  <p>This is the error I get after running it. Again tho this works on the workbook I had him design it in but I need to know how to fix it.</p>
  <pre><code>  // If you want to change any tab's name, also change it from here.
    var ss = SpreadsheetApp.getActive();
    var tem = ss.getSheetByName(&quot;Template&quot;);
    var frm = ss.getSheetByName(&quot;Form Responses 1&quot;);
    var cde = ss.getSheetByName(&quot;Code&quot;);
  //---------------------------------------------------------------------------------------------------------------------------------------
    lr = frm.getLastRow();
   // Logger.log(lr)
  
    for(i=4; i&lt;=lr; i++){
      //----------These are values from the Form Responses tab, 2,1, 3, 4, 5 ... represent the column numbers. 
      // For example, date is in column 1, so  var date = frm.getRange(i,1).getValue();
      // store name is in column 3, so var store = frm.getRange(i,3).getValue();
  
      var tabName = frm.getRange(i,2).getValue();
      var date = frm.getRange(i,1).getValue();
      var store = frm.getRange(i,3).getValue();
      var add1 = frm.getRange(i,4).getValue();
      var add2 = frm.getRange(i,5).getValue();
      var add3 = frm.getRange(i,6).getValue();
      var zip = frm.getRange(i,7).getValue();
      var city = frm.getRange(i,8).getValue();
      var state = frm.getRange(i,9).getValue();
      var phone = frm.getRange(i,10).getValue();
      var email = frm.getRange(i,11).getValue();
      var po = frm.getRange (i,16).getValue();
      var glowHeart = frm.getRange(i,17).getValue();
      var glowSquare = frm.getRange(i,18).getValue();
      var rainHeart = frm.getRange(i,19).getValue();
      var rainSquare = frm.getRange(i,20).getValue();
      ///-----The following line of code checks if there is already a tab for a store name, if not create new.
      if(ss.getSheetByName(tabName)==null &amp;&amp; tabName!==&quot;&quot;){
        var create = ss.insertSheet(tabName);
      }
    if ( tabName==&quot;&quot;){
      break;
    }
    ///- - - - - - Now write the row values to the template tab.
      tem.getRange(&quot;B2&quot;).setValue(tabName); // cell B2 is set to name
      tem.getRange(&quot;B3&quot;).setValue(store);// cell B3 is set to store name and so on
      tem.getRange(&quot;F3&quot;).setValue(date);
      tem.getRange(&quot;B5&quot;).setValue(add1);
      tem.getRange(&quot;B6&quot;).setValue(add2);
      tem.getRange(&quot;B7&quot;).setValue(add3);
      tem.getRange(&quot;B8&quot;).setValue(zip);
      tem.getRange(&quot;B9&quot;).setValue(city);
      tem.getRange(&quot;B10&quot;).setValue(state);
      tem.getRange(&quot;B12&quot;).setValue(phone);
      tem.getRange(&quot;B13&quot;).setValue(email);
      tem.getRange(&quot;C17&quot;).setValue(glowHeart);
      tem.getRange(&quot;C18&quot;).setValue(glowSquare);
      tem.getRange(&quot;D17&quot;).setValue(rainHeart);
      tem.getRange(&quot;D18&quot;).setValue(rainSquare);
      tem.getRange(&quot;F4&quot;).setValue(po);
  
      /// Once the template is filled, it is moved to the newly created tab
      moveVal(tabName)
      // After that, new file is created for that 
      createNewFile(tabName)
  
  /// These lines clear the template once done.
          tem.getRange(&quot;B2&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B3&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;F3&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B5&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B6&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B7&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B8&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B9&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B10&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B12&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;B13&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;C17&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;C18&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;D17&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;D18&quot;).setValue(&quot;&quot;);
      tem.getRange(&quot;F4&quot;).setValue(&quot;&quot;);
      
    }
  }
  
  
  // This fucntion creates new tabs
  function moveVal(tabName) {
      var ss = SpreadsheetApp.getActive();
    var spreadsheet = ss.getSheetByName(&quot;Template&quot;);
    //var spreadsheet = SpreadsheetApp.getActive();
    spreadsheet.getRange('A1:G29').activate();
    ss.setActiveSheet(ss.getSheetByName(tabName), true);
    ss.getRange('Template!A1:G29').copyTo(ss.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, false);
    ss.setActiveSheet(ss.getSheetByName('Template'), true);
    spreadsheet.getRange('F9').activate();
  };
  
  // This fucntion creates new files.
  function createNewFile(tabName){
   
      var ss = SpreadsheetApp.getActive();
    var cde = ss.getSheetByName(&quot;Code&quot;);
    var newF = SpreadsheetApp.create(tabName);
    var dd = newF.getId();
    Logger.log(dd)
  
  var source = SpreadsheetApp.getActiveSpreadsheet();
  
    var sheet = source.getSheetByName(&quot;Price&quot;);
  
   var destination = SpreadsheetApp.openById(dd);
  
   sheet.copyTo(destination);
   destination.getSheetByName(&quot;Copy of Price&quot;).setName(&quot;Price&quot;);
  
   var sheet = source.getSheetByName(&quot;Template&quot;);
  
   var destination = SpreadsheetApp.openById(dd);
  
   sheet.copyTo(destination);
    destination.getSheetByName(&quot;Copy of Template&quot;).setName(tabName);
  
  
    destination.getSheetByName(&quot;Sheet1&quot;).getRange('D26').activate();
    destination.deleteActiveSheet();
    destination.getSheetByName(tabName).activate()
    destination.setActiveSheet(destination.getSheetByName('Price'), true);
    destination.getActiveSheet().hideSheet();
  
      sourceFileId =dd;
    //targetFolderId = &quot;xxxxxxxxxxxxxx&quot;;
    ////////////////////////////// You can change the cell from here. ////////////
    targetFolderId = cde.getRange(&quot;G3&quot;).getValue(); // This gets the folder's id from cell G3 in Code tab
  
    ///..........................................................................................
    //Logger.log(targetFolderId)
    moveFiles(sourceFileId,targetFolderId)
  }
  
  // THis fucntion moves the file to the specified folder.
  function moveFiles(sourceFileId,targetFolderId) {
  
    var file = DriveApp.getFileById(sourceFileId);
    var folder = DriveApp.getFolderById(targetFolderId);
    file.moveTo(folder);
  }
  </code></pre>
  <p>It breaks right when it gets to these lines</p>
  <pre><code> tem.getRange(&quot;B2&quot;).setValue(tabName); // cell B2 is set to name
      tem.getRange(&quot;B3&quot;).setValue(store);// cell B3 is set to store name and so on
      tem.getRange(&quot;F3&quot;).setValue(date);
      tem.getRange(&quot;B5&quot;).setValue(add1);
      tem.getRange(&quot;B6&quot;).setValue(add2);
      tem.getRange(&quot;B7&quot;).setValue(add3);
      tem.getRange(&quot;B8&quot;).setValue(zip);
      tem.getRange(&quot;B9&quot;).setValue(city);
      tem.getRange(&quot;B10&quot;).setValue(state);
      tem.getRange(&quot;B12&quot;).setValue(phone);
      tem.getRange(&quot;B13&quot;).setValue(email);
      tem.getRange(&quot;C17&quot;).setValue(glowHeart);
      tem.getRange(&quot;C18&quot;).setValue(glowSquare);
      tem.getRange(&quot;D17&quot;).setValue(rainHeart);
      tem.getRange(&quot;D18&quot;).setValue(rainSquare);
      tem.getRange(&quot;F4&quot;).setValue(po);
  </code></pre>