title: google sheets script conditionally move row to another spreadsheet
body: <p>I am new to this and I am having some trouble trying to figure this out. I have a spreadsheet that collects data from a google form. I am trying to find a way to move that data based on a column answer to a different google sheet. (Not a sheet in the same document but a different document all together). It seems like there is some information about moving to a different tab in the same document, but not a different document.  </p>
  
  <p>I will first say that I tried just an IMPORTRANGE function, but that only mirrors the data, and does not let you update or change cells.  </p>
  
  <p>Here is what I have been able to piece together so far, but I may be way off. </p>
  
  <p>I have a trigger that would run every hour. </p>
  
  <pre><code>function myFunction() {
   var ss = SpreadsheetApp.openById('1U0I9SkbGkHgm-vRkwf2Ppc_yxlqrVlg2t8yKRy3sYuI');
   var sheetOrg = ss.getSheetByName("Form Responses 1");
   var value1ToWatch = "ANDERSON";
   var value2ToWatch = "BARNES";
   var sheetNameToMoveTheRowTo = "Sheet1"; //sheet has same name for each target openByID(" *url key* ")
   var ss = SpreadsheetApp.getActiveSpreadsheet();
   var ts1 =SpreadsheetApp.openById("1PxV1PQrMdu_2aSru4dpan8cUgHYdlYPUp5anyzjMAms");
       sheet1in = ts1.getSheetByName("Sheet1");
   var ts2 = SpreadsheetApp.openById("1BYyQZNiXc2QqsyqWs7uazjl5B6mfFYM1xj3u8gWyYOQ");
       sheet1in = ts2.getSheetByName("Sheet1");
  
   arr = [], 
  values = sheetOrg.getDataRange().getValues(),
  i = values.length;
  while (--i) {
    if (value1ToWatch.indexOf(values[i][1]) &gt; -1) {
      arr.unshift(values[i])
     sheetOrg.deleteRow(i + 1) 
     sheet1in.getRange(sheet1in.getLastRow()+1, 1, arr.length, 
       arr[0].length).setValues(arr); 
    };
  
   if (value2ToWatch.indexOf(values[i][1]) &gt; -1) {
      arr.unshift(values[i])
     sheetOrg.deleteRow(i + 1) 
     sheet2in.getRange(sheet2in.getLastRow()+1, 1, arr.length, 
   arr[0].length).setValues(arr);
  };
  }
  }
  </code></pre>
  
  <ol>
  <li>The sheet google forms dumps the information into is "Form Responses 1".</li>
  <li>Column B is the cells I want to get the values from. (There are a total of 9 different values that it can be like "ANDERSON", "BARNES", "SMITH", etc). </li>
  <li>For sheetNameToMoveTheRowTo is "Sheet1"  - that may be confusing and I may need to change that, but for example
  
  <ul>
  <li>ts1 =SpreadsheetApp.openById("1PxV1PQrMdu_2aSru4dpan8cUgHYdlYPUp5anyzjMAms") the sheet name that I want the information moved to is "Sheet1". </li>
  <li>ts2 = SpreadsheetApp.openById("1BYyQZNiXc2QqsyqWs7uazjl5B6mfFYM1xj3u8gWyYOQ") the sheet name is also "Sheet1" but in a different document. </li>
  </ul></li>
  </ol>
  
  <p>I think if I were able to get the "ANDERSON" one to work, then I can just add additional variables for each possible response, and just copy and paste additional "IF" statements, just changing the valueToWatch and targetSheet values.  &lt;= if that is not correct please let me know </p>
  
  <p>I have tried to both debug, and run the script above but nothing happens.  There are no errors reported on the debug, but it is not moving any information over. </p>
  
  <p>Any idea what I am doing wrong? </p>
  
  <p>//  UPDATE  I got this to work.  I have updated the code listed with what worked for me.  </p>