0

I have a loop array function that I'm using to automatically fill in values. Some of these values are intentionally blank. However, it seems the array assigns an "undefined" label to all values that are blank in the array.

function SubGroup_Update() {

  var Metrics = SpreadsheetApp.openById("10Wl1B4AtdLHJXBbLbMQbSdtRyAb61biCWYpOQEEywIY"); // METRICS spreadsheet
  var Data = SpreadsheetApp.getActiveSpreadsheet(); // DATA spreadsheet
  var SubsMetricSheet = Metrics.getSheetByName('Sub Group Metrics'); // METRICS "Sub Group" sheet 
  var SubGroupDataSheet = Data.getSheetByName("The Sub Group_Numbers") // DATA "Sub Group" sheet
  var SubGroupAllValues = SubGroupDataSheet.getRange(2, 1, SubGroupDataSheet.getLastRow() - 1, SubGroupDataSheet.getLastColumn()).getValues();
  var SubsDataLastRow = SubGroupDataSheet.getLastRow(); // Get the number for the amount of rows with info in the DATA "Sub Group" Sheet
  var FeedDataSheet = Data.getSheetByName("The Feed_Raw") // DATA "Feed" sheet
  var FeedAllValues = FeedDataSheet.getRange(2, 1, FeedDataSheet.getLastRow() - 1, FeedDataSheet.getLastColumn()).getValues();
  //var SubsDataRange = SubGroupDataSheet.getRange("A2:K");  // The entire range of cells in the DATA "Sub Group" Sheet, for sorting
  var SubGroupObj = {}; // Object for "Subgroup" values

  for (var SG = SubGroupAllValues.length - 1; SG >= 0; SG--) // for each row in the "Sub Group" sheet...
  {
    //... Add "Sub Group KEY (Col. #11 [K])" & "FYI Category Name (Col. #1 [A])" in Feed 2D Array.
    SubGroupObj[SubGroupAllValues[SG][10]] = SubGroupAllValues[SG][0];
  }


  for (var F = FeedAllValues.length - 1; F >= 0; F--) // for each row in the "Feed" sheet...
  {
    var Feed_SubGroupKey = FeedAllValues[F][92]; // ...Add "Sub Group (Col. #93 [CO])" in array. 
    {
      // If Sub Group array dont match "Sub Group Key (Col. #93 [CO])" & "FYI Topic Name (Col. #4 [D])... "
      if (SubGroupObj[Feed_SubGroupKey] != FeedAllValues[F][3]) {
        FeedAllValues[F][3] = SubGroupObj[Feed_SubGroupKey]; // ...Change FYI Category Name in FYI Topic Sheet 
      }
    }
  }

  // Decalare var. from 2nd row to last row of FYI Topic sheet
  var FeedDestinationRange = FeedDataSheet.getRange(2, 1, FeedAllValues.length, FeedAllValues[0].length);

  FeedDestinationRange.setValues(FeedAllValues); // placed changed FYI Category 2D array in Mod sheet

  var SubGroupAllRange = SubGroupDataSheet.getRange(2, 1, SubGroupDataSheet.getLastRow() - 1, SubGroupDataSheet.getLastColumn()); // complete range of Mod Status sheet 

  // Sort Sheet by - Category Name, then Category Topic
  SubGroupAllRange.sort([{
    column: 1,
    ascending: true
  }, {
    column: 2,
    ascending: true
  }]);

  Logger.log("The Sub Group Data Sheet has updated  " + SubsDataLastRow - 1 + " data files")

}

Can anyone help me modify this function so that it will skip over any blank cells it will find in my file?

2 Answers 2

2

You haven't specified which array you wanted to filter, however the there are two options.

You check it in your loop

for(var F = FeedAllValues.length-1;F>=0;F--) {
    if (typeof FeedAllValues[F] !== 'undefined') {
}

or filter the array beforehand

FeedAllValues = FeedAllValues.filter(value => typeof value !== 'undefined')

Also a side note, you should define your FeedAllValues.length outside of your loop and use a variable, otherwise it will call FeedAllValues.length every iteration

Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the swift response! I updated my post with the full code if that helps you understand what I'm trying to do.
thanks for the tip about the .length. I'll make sure to fix my code to prevent that
Updated to be more specific @RobG
0

You can just test for empty cells at the same time as testing for non-matching cells, something like this:

for(var F = FeedAllValues.length-1;F>=0;F--) // for each row in the "Feed" sheet...
  {  
    var Feed_SubGroupKey = FeedAllValues[F][92]; // ...Add "Sub Group (Col. #93 [CO])" in array. 
    {
      // If Sub Group array dont match "Sub Group Key (Col. #93 [CO])" & "FYI Topic Name (Col. #4 [D])... "
      if ( (SubGroupObj[Feed_SubGroupKey] != FeedAllValues[F][3]) && ("" != FeedAllValues[F][3]) ) 
      { 
        FeedAllValues[F][3] = SubGroupObj[Feed_SubGroupKey]; // ...Change FYI Category Name in FYI Topic Sheet 
      }

5 Comments

thanks for the swift response! Let me give a spin and let you know
If the intention is to test for undefined, then comparison with an empty string is not the right test as '' != undefined. Better to test explicitly: ... && typeof FeedAllValues[F][3] != undefined.
worked like a charm! I originally did // if (FeedAllValues[F][92] != 'undefined' || FeedAllValues[F][92] != '') " so it seems i just the syntax wrong. Thanks so much for the help. Really appreciate it.
@Neo— FeedAllValues[F][92] != 'undefined' tests with the literal string "undefined", not the undefined value.
It's also perhaps something of an oddity in JavaScript that undefined != "" -- Not all falsey values are equal to each other when using ==. This is by design but can catch you off guard if you don't know what to expect. (For instance, undefined and null are always considered loosely equal to each other, but no other values are ever equal to them.) Check out sitepoint.com/javascript-truthy-falsy for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.