简体   繁体   中英

IF statement in a while loop not working, Where am I going wrong?

So am still learning Javascript and trying to find my way. I'm trying to get something working that will save me hours of time per week at work.

It's a script for a googlespread sheet. It's meant to look for key values in columns and move data accordingly. It should then split the data, by region into an external spreadsheet, into induvidual tabs.

I think I have the split bit right, but where it's doesn't seem to work is decidng what should be moved and what shouldn't be. I'm pretty sure something is wrong with my While loop and possibly my if statement inside the while loop :(

I'd be really grateful if anyone can point out the error of my ways. Here is the full script.

The only items that should be moved are as follows. IF column AV = Need to remove details AND Column AW is not blank AND column AX is not blank. AND column BC is not blank. Then it should be pushed to archive.

OR

IF AV is blank AND aw is not blank AND bc is not blank. Then that should also be pushed to archive.

Many thanks

function testNinja(){


var sourceSheet = "Change of details requests";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sourceSheet);
var values = sheet.getDataRange().getValues();
var valuesLength = values.length;
var archive = [];
var counter = 1;
var agentSheet = "ID HERE";

// To make things easier, I've defined the worksheet names and the region names into arrays. Position is important, as entry 0 matches entry 0

var regionSheets = ["R/North East&Yorks","R/North West","R/South East","D/South London","D/East","D/North London","M/Midlands","M/Wales&S.West","M/Scotland/N.Ireland"];
var regionNames = ["North East & Yorks","North West","South East","South London","East England","North London","Midlands","Wales & S West","Scotland & N Ireland"];

// giving key columns & key values a var to save typing. Using column letters as var name.
  var av = values[counter][47];
  var aw = values[counter][48];
  var ax = values[counter][49];
  var bc = values[counter][54];
  var bf = values[counter][57];
  var bk = "Need to Remove Details";
  var sf = "SET SF TASK";



// put matching values into an archive. This bit doesn't seem to be working correctly

  while(counter < valuesLength)


{ 
if (bf == sf)

    {

    counter++;

    }
     if(av == bk && aw != "" && ax != "" && bc != "" || av == "" && aw != "" && bc !="")
    {
      archive.push(values.splice(counter, 1)[0]); 
    }

    else
    {

    counter++;
    }


  }



// create a function called 'move' to move columns into new positions

Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
}

//go through the archive, use the move function, and trim the end columns. Finally add a timestamp
var timestamp = new Date();
for (var i = 0;i<archive.length;i++)
{

archive[i].move(68,5);
archive[i].length = 73;
archive[i][61] = timestamp;
}


// Split the data by region. There is a region value in Column 65. This determinds what Sheet data is moved too.
var regionData = [];
var regionSs = SpreadsheetApp.openById(agentSheet);

for(var i = 0; i < regionSheets.length;i++){

var regionSheet = regionSs.getSheetByName(regionSheets[i]);
var regionName = regionNames[i];

for(var j = 0; j < archive.length;j++){

var value = archive[j][66];

if(value == regionName){

regionData.push(archive.splice(j, 1)[0]);


}



}
//Write data to sheet here.
 var regionDataLength = regionData.length;
  if (!regionDataLength) continue; 
  var lastRow = regionSheet.getLastRow();
  var requiredRows = lastRow + regionDataLength - regionSheet.getMaxRows();
  if (requiredRows > 0) regionSheet.insertRowsAfter(lastRow, requiredRows);
  regionSheet.getRange(lastRow + 1, 1, regionDataLength, regionData[0].length).setValues(regionData);

// clear array for new region data
  regionData = [];

// Get data validation from CC2, then apply to CC2:CC .

var CCrule = regionSheet.getRange("CC2").getDataValidation();
regionSheet.getRange("CC2:CC").setDataValidation(CCrule);


}



}
var counter = 1;
//...
var bf = values[counter][57];
//...
var sf = "SET SF TASK";
//...
while(counter < valuesLength)
{ 
if (bf == sf)
  {
    counter++;
  }
//...

I think the problem is the assignment to the variable bf (and the others). You are assigning values[1][57] to bf , so you are not actually iterating through the columns when the counter is increased (which it never will, if values[1][57] != sf ).

So if you wanted to use the column identifiers to save typing, I think you would need to assign values inside the while loop, not before it.

Try with this if :

if ((av == bk && aw != "" && ax != "" && bc != "") || (av == "" && aw != "" && bc !=""))

You must put parentheses around your logic blocks otherwise the computer has no way of knowing how to group the conditions and in this case the result depends on operator precedence.

Edit: I was wrong, in JS && has higher precedence than || so programmatically it doesn't change anything, but however it's a best practice for the clarity and readability of the code to put the parentheses.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM