Jerome Learman
Jerome Learman

Reputation: 87

look at all instances of an array in a if statement?

I have about 20 different sheets and I wrote some google script to combine all of the data into a master sheet. Now I'd like like to be able to exclude certain sheets. My idea to do this was to storage the names of those sheet in a variable. This is what I have so far, but I am getting an error? Any ideas?

label is the name of the Column that I am scanning each sheet for and masterSheetName is the sheet where I am storing the data.

 if (sheetName !== masterSheetName && sheetName !== skippedsheets) 

lines are the ones I am having trouble with. It is not going though all of the instances of skipped sheets.

Is there a way to do this with a for each loop?

function getColVals(label, masterSheetName) {

  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var colValues = []
  for ([i,sheet] in sheets) {
    var sheetName = sheet.getSheetName();
    var skippedsheets = ["HHS 1","HHS 2"];
    Logger.log(skippedsheets);
    Logger.log(skippedsheets[0]);
    if (sheetName !== masterSheetName && sheetName !== skippedsheets) {
    var colValues2 = getColValues(label,sheetName);
    colValues = colValues.concat(colValues2);
    } 

  }
  return colValues; 

}

thank you, Jerome

Upvotes: 0

Views: 33

Answers (1)

Jerome Learman
Jerome Learman

Reputation: 87

I found this function called inArray that some one wrote and shared and it worked perfectly.

function getColVals(label, masterSheetName) {

  var sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();
  var colValues = []
  for ([i,sheet] in sheets) {
    var sheetName = sheet.getSheetName();
    var skippedsheets = ["template","intro","games"];
    // Logger.log(skippedsheets);
    // Logger.log(skippedsheets[0]);
    if (sheetName !== masterSheetName && !(skippedsheets.inArray(sheetName))) {
    var colValues2 = getColValues(label,sheetName);
    colValues = colValues.concat(colValues2);
    Logger.log(sheetName);
    } 

  }
  return colValues; 

}


/*
 * @function
 * @name Object.prototype.inArray
 * @description Extend Object prototype within inArray function
 *
 * @param {mix}    needle       - Search-able needle
 * @param {bool}   searchInKey  - Search needle in keys?
 *
 */
Object.defineProperty(Object.prototype, 'inArray',{
    value: function(needle, searchInKey){

        var object = this;

        if( Object.prototype.toString.call(needle) === '[object Object]' || 
            Object.prototype.toString.call(needle) === '[object Array]'){
            needle = JSON.stringify(needle);
        }

        return Object.keys(object).some(function(key){

            var value = object[key];

            if( Object.prototype.toString.call(value) === '[object Object]' || 
                Object.prototype.toString.call(value) === '[object Array]'){
                value = JSON.stringify(value);
            }

            if(searchInKey){
                if(value === needle || key === needle){
                return true;
                }
            }else{
                if(value === needle){
                    return true;
                }
            }
        });
    },
    writable: true,
    configurable: true,
    enumerable: false
});

Upvotes: 1

Related Questions