google apps script - JavaScript: If statement not working inside Loop -
i'm writing following code (a test of now) using google scripts pass data 1 spreadsheet another. passing of code working fine, second loop – intend use detect duplicate values , avoid passing rows on – not working. checking logs see though "i" , "j" values correctly being passed inside if block, "if(sheetsidhome[i] == sheetsidtarget[j])" statement never triggering, when confirm both values same. appreciated, thank in advance!
function move(){ var homebook = spreadsheetapp.getactivespreadsheet(); var sheet = homebook.getsheets()[0];//sheet home data stored var limit = sheet.getlastrow(); //number of rows content in them var evento = sheet.getrange(2, 1, limit-1).getvalues(); //even titles array var descript = sheet.getrange(2,2,limit-1).getvalues(); //event descriptions array var tags = sheet.getrange(2,3,limit-1).getvalues(); //tags array var sheetsidhome = sheet.getrange(2,4,limit-1).getvalues(); //id's array var targetbook = spreadsheetapp.openbyid("1t3qmtu2opyfflmfftuibv6brwsde9ilhzj_zt89khr8"); // traget workbook var target = targetbook.getsheets()[0]; //sheet1, target sheet if (target.getlastrow() > 1){ var sheetsidtarget = target.getrange(2, 4,target.getlastrow()-1).getvalues();} else{ var sheetsidtarget = target.getrange(2, 4, 1).getvalues();} var targetrow = target.getlastrow()+1; //target row start pasting content (var = 0; < evento.length; i++) { //loops throught every value home sheet in order pass target sheet var isklar = 1; //this works switch, data passing not activate if isklar set 0 logger.log("switch is: "+isklar); for(var j = 0; j < sheetsidtarget.length; j++){ //while having "i" value in place, loop though values in target array using counter "j" if(sheetsidhome[i] == sheetsidtarget[j]){ //if id of curent row home matches of values in target sheet, "isklar" switch should turn off , break loop exited. logger.log("if activated"); isklar = 0; break;} else{logger.log("id's: "+sheetsidhome[i] + " vs " + sheetsidtarget[j]);} } if(isklar === 1){ //data passing not activate if isklar set 0 //pass data target sheet target.getrange(targetrow,1).setvalue(evento[i]); target.getrange(targetrow,2).setvalue(descript[i]); target.getrange(targetrow,3).setvalue(tags[i]); target.getrange(targetrow,4).setvalue(sheetsidhome[i]); targetrow++; //select next available row in ny target sheet } } }
edit. - right i'm testing both id arrays same numbers (e.g. 1, 2, 3, 4). log inside else statement show correct values being read both arrays... thought scope issue, i'm not sure problem is.
the issue sheet range.getvalues() returns array of arrays, not array of values.
values[0] first row, , values[0][0] first value in first row. rework code knowing this.
Comments
Post a Comment