google apps script - Create files from sheet -


i'd have apps script create new spreadsheet based on names in sheet. instance, if list of names (in column 7) were:

  • bender@interpret.org.nz
  • fry@interpret.org.nz
  • hermes@interpret.org.nz
  • leela@interpret.org.nz
  • nibbler@interpret.org.nz

i'd script iterate through list, replace string name (minus email domain), search drive existing file matches name. also, if there existing file, nothing, else create sheet name.

my code below seems iterating through list , replacing string there issue last 2 steps. see bender not exist, , create bender file, creates additional bender files iterates through list, , not create other missing files.

any assistance appreciated.

function findexistingsheets() {       var sheet = spreadsheetapp.openbyid('spreadsheetkey').getsheetbyname('isheetname');       var data = sheet.getdatarange().getvalues();       var files = driveapp.getfolderbyid("filekeyid").getfiles();         (var = 1; < data.length; i++) {           var searchfor = data[i][7].replace("@interpret.org.nz","");             while (files.hasnext()) {               var file = files.next();               if(searchfor == file.getname()){                 //don't               }               else {                 //create speadsheet file matchfile name.                 var newfile = spreadsheetapp.create(searchfor);               }             }         }       logger.log(searchfor);     } 

try this. haven't tested it, think close.

function findexistingsheets() {       var sheet = spreadsheetapp.openbyid('spreadsheetkey').getsheetbyname('isheetname');       var data = sheet.getdatarange().getvalues();        var titles = [];       (var = 1; < data.length; i++)        {         titles.push(data[i][7].replace("@interpret.org.nz",""));       }           var files = driveapp.getfolderbyid("filekeyid").getfiles();         while (files.hasnext())        {           var file = files.next();           var idx = idxof(titles,file.getname());           if(idx>-1)           {             var newfile = spreadsheetapp.create(titles[idx]);           }       }     }  function idxof(array, x) {   for(var = 0;i < array.length;i++)   {     if(array[i]==x)     {       return i;      }   }   return -1; } 

Comments