Google Scripts how to delete extra rows from an array

I am working on a google sheets template that will have some roster maintenance built in. When rosters are updated on the main "roster" tab, I would like for all the other tabs in the sheet to check student ID #s against the updated roster tab. In the code, an example sheet is "anet" sheets the sheets. I am using indexOf and a for loop to check each value in the "anet" sheet against the IDs in the "roster" sheet. If an ID# has been removed from the "roster" sheet, I would like that row to be deleted in the "anet" sheet.

When I run the script right now, some of the rows are deleted, but not all of them. The list of IDs begins in A3 on the "roster" tab, and the other list begins in A15 on the "anet" tab. Can someone help me understand why it is deleting some of the rows returning an indexOf of -1, but not all of the rows I need deleted?

function withdrawnStudent (){
  let lastRowTyler = roster.getLastRow();
  let tylerData = roster.getRange(3,1,lastRowTyler,1).getValues();
  let tylerArray = tylerData.map(function(r){ return r[0]});

  
  let anetLastRow = anet.getLastRow();
  
 
  let anetLastColumn = anet.getLastColumn();
  let anetData = anet.getRange(15,1,anetLastRow,anetLastColumn).getValues();
  let anetIDArray = anetData.map(function(r){ return r[0]});'''

  

  for (let index = 14; index < 200; index++){
  
    if(tylerArray.indexOf(anetIDArray[index][0]) === -1){
       anet.deleteRow(index +14);
      
     Logger.log(tylerArray.indexOf(anetIDArray[index][0]))

Here is a link to an example spreadsheet. In the "roster" tab, it lists 4th grade student IDS. In the "anet" tab, all rows with a number should be deleted because these are 5th grade IDs. However, not all rows are getting deleted, only some.

https://docs.google.com/spreadsheets/d/1vDse6X6gs3bkgnlBfgo-vzERkAMud3rUDC6j8fEkcrk/edit#gid=447751616

48 thoughts on “Google Scripts how to delete extra rows from an array”

  1. I have been browsing online more than three hours today, yet I never found any interesting article
    like yours. It’s pretty worth enough for me. In my opinion, if all webmasters and bloggers made
    good content as you did, the web will be a lot more useful than ever
    before.

    Reply
  2. Greetings from Idaho! I’m bored at work so I decided to
    check out your blog on my iphone during lunch break.

    I really like the information you present here and can’t wait to take a look when I
    get home. I’m shocked at how fast your blog loaded on my mobile
    .. I’m not even using WIFI, just 3G .. Anyhow, awesome blog!

    Reply
  3. 515712 875481Wow, fantastic blog layout! How long have you been blogging for? you make blogging appear effortless. The overall appear of your internet web site is wonderful, let alone the content material! 492822

    Reply