IF statement. Run function if data is listed in variable

Tags: , ,



I’m trying to get value from Google Spreadsheet. I’m looping through each column to get the values and want to set condition if the value in the column is 1 or 8 or 15 or 22, 29,……. (number increasing by 7 starting from 1 until 365), the code will run, else it will not run.

First I trying with while loop for increment.

var data = 1;
 while(data < 365){
 data += 7;
 Logger.log(data); //data 1,8,15,22,29

So now I want to compare above data with my next condition which is getCol condition. So every time var “data” numbers is same like the var “getCol” numbers, the code will run.

function runCode(){
var ss = SpreadsheetApp.getActiveSheet().getSheetByName("Data");
var lastRow = ss.getLastRow();

 for(var x=11; x<ss.getLastColumn(); x++){
 var getCol = ss.getRange(ss.getLastRow(),x).getValue(); //value=6,7,8,9,10,11,12........
   
    if(getCol === ""){
          false;

        }else if(getCol === 8 || getCol === 15){
          ssRota.getRange(ldap,getCol).setValue("Test");

        }
        else{
          ssRota.getRange(ldap,getCol).setValue("Input");
}

As you can see, I’m currently using the OR statement. If the values are 8 or 15, the code will run and set the value in each column to “Test”. But now it will be insane if I want to write OR statement 8,15,22,29…… until 365. I’m trying with

else if(getCol === data){
 console.log("same");
}

but seem like it didn’t work. Any suggestion?

Answer

Instead of

else if(getCol === 8 || getCol === 15){

you can try this

else if(getCol % 7 === 1){


Source: stackoverflow