简体   繁体   中英

Skipping blanks in a Javascript array

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?

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

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 }

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