title: Copy a row to new sheet based on value in a cell
body: <p>I'm trying to create a Google Script that copies rows from one Google Sheet into different sheets based on the value of a cell.</p>
  
  <p>The cell value are states in the United States. The master spreadsheet has data from a registration form that is being imported into it all the time. When a new registration happens (and the data is imported into the master sheet), I'd like the script to run and copy that row of data into the appropriate state sheet.</p>
  
  <p>Here's where I'm at:</p>
  
  <ol>
  <li>Get master sheet</li>
  <li>Find the last row</li>
  <li>Get the value of the cell in the column "state"</li>
  <li>Copy that row into one of 50 different sheets depending on what state it is.</li>
  <li>Run the script every time the master sheet is updated (via an API).</li>
  </ol>
  
  <p>Any help would be appreciated. I'm definitely a newbie when it comes to scripting and this is just hurting my head. </p>
  
  <p>Here's the code I have so far:</p>
  
  <pre class="lang-js prettyprint-override"><code>function myFunction() {
  
  // Get Source Spreadsheet
  var source = SpreadsheetApp.getActiveSpreadsheet();
  
  // Get Source Sheet from Spreadsheet
  var source_sheet = source.getActiveSheet();
  
  // Get Active Range from Sheet
  var lastRow = sheet.getLastRow();
  
  // Get the Value of the State Cell
  var cellValue = Range.getCell(lastrow,3);
  
  // Copy Last Row to Appropriate State Sheet
  if ( cellValue == 'Alaska') {
      var target = SpreadsheetApp.openById("");
      var target_sheet = target.getSheetByName("Sheet1");
      target_sheet.appendRow(lastRow);
  }
  }
  </code></pre>