javascript - Problems with code listing unique items in a list and copying them into new worksheet -


i'm trying run following code on spreadsheet. column of names 4th column. i'm attempting run through list of names, pushing each new unique name array (listofnames), , add new worksheet (nameslist), , add array cell a1 of new worksheet. when run code below, blank popup ok , cancel button. when view log, blank well. i'm quite new @ this, , feel i'm missing obvious... not sure is. misunderstanding specific gas rather js?

var sheet = spreadsheetapp.getactivesheet(); var listofnames = new array ();  function copynames() {   var data = sheet.getdatarange().getvalues();   (i=0; i<=sheet.getlastrow(); i++){     var tempname = sheet.getdatarange(i,4).getvalue();     (i=0; i<=listofnames.length; i++){       if (tempname != listofnames[i]){         listofnames.push(tempname);         logger.log(listofnames);       }     }   } sheet.insertsheet(listofeds); sheet.getrange('a1').setvalue(listofeds); } 

edit: i'm starting see push values multiple times list... maybe it's drawing board together. have found other code create list of unique elements, wasn't sure how code worked. thought i'd try figure out myself i'd @ least understand it.

edit 2: ok... tried new code, i'm still getting blank message box, , nothing on log. wasn't sure if having iterator for loop within loop bad thing, switched j. also, know there's remove duplicates example, , have been looking @ that, unsure why 1 needs use join.

function copynames() { var sheet = spreadsheetapp.getactivesheet(); var listofnames = new array (); var data = sheet.getdatarange().getvalues();   (i=2; i<=data.length; i++){ //starting @ 2 because first row header     var tempname = data[i][4];     (j=0; j<=listofnames.length+1; j++){         if (tempname != listofnames[j]){           listofnames.push(tempname);           logger.log(listofnames);         }       }     } sheet.insertsheet("listofnames"); sheet.getrange('a1').setvalue(listofnames); } 

as far understand went instead of ready solution, here go.

first:

try avoid using global variables. keep variables inside of functions. otherwise have issues when add more functions spreadsheet. code should start this:

function copynames() { var sheet = spreadsheetapp.getactivesheet(); var data = sheet.getdatarange().getvalues(); var listofnames = new array (); 

second:

try decrease amount of service calls spreadsheet. takes several seconds script go sheet , take values. instead of doing each time, take these values once , push them in variable.

in case, did that, did not use variable , kept using service calls. check line:

var data = sheet.getdatarange().getvalues(); 

now of data on sheet in variable called data.

sheet.getdatarange(i,4).getvalue(); 

is same

data[i][4]; 

the difference in first case take approximately 2 seconds value, while in second case few milliseconds.

same goes

sheet.getlastrow(); 

either call once , push variable , use that, or in case use

data.length; 

third:

listofnames empty array, it's length 0. line

for (i=0; i<=listofnames.length; i++) 

will not run, both i , listofnames.length 0. why logger not give output. script never get's line.

fourth:

you not have variable called listofeds, therefore last 2 rows of code give error. script not know listofeds doesn't exist.

hope helps.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -