0

I'm new to coding in javascript and google apps. I'm making a database that will automatically update new users into an aggregate sheet nightly.

My searchfolder() function won't execute because i keep getting the error "TypeError: Cannot find function hasNext in object Thu Jul 14 09:48:01 PDT 2016 INFO: Target. (line 24, file working daily)"

var files has .getfiles after DriveApp.getFolderById(folderId) so i haven't the slightest idea what the problem is

CODE:

var counter = 0;

function timeStamp(){
counter= (counter+1)
}

var files = "null"

function searchFolder() {
  var folderId = '0B6wmHZ5c0fzfTjI1bFpKOHI3N3M'; // test folder
  // Log the name of every file in the folder.
 var files = DriveApp.getFolderById(folderId).getFiles(); //log files in      
 folder
  while (files.hasNext()) { //loop log files in folder
  var file = files.next(); //log files in folder
  Logger.log(file.getName()); //logs name of file in folder
  var files = Logger.getLog();
  }
}

function autoUpdate(){ //updates monthly from newly imported daily
  if (counter == 1){ //counter is made to be 1 when day is uploaded to 
  monthly
  var ss =    
  SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc"); 
  //defines target spreadsheet ie monthly
  SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as 
  active
  var range= ss.getRange("A1:A1"); 
  range.activate;  // activates range
  range.setValue('=IMPORTRANGE("1K7Rj4QK-  
  EVjf8lZ0BSew7iDeCtktqWjzgjoaVPe5jSc","sheet1!A1:G6")'); //Puts in 
  IMPORTRANGE into target as a STRING value (just words). Once it hits the 
  sheet, then SHEETS executes IMPORTRANGE not SCRIPTS. In Source sheet, 
  range is selected to import to target (ie A1:G6)
  counter=(counter-1)
  }
}

//searchFolder();
timeStamp();
autoUpdate();

1 Answer 1

0

You declared variable files two times. Also, pushing into log and then getting logs back is bad practice. Better use array instead. You got an error because variable files where you store file names is changed to log context after first loop, which means that there is no more function hasNext inside your files object (it is not of type string).

var counter = 0;

function timeStamp(){
counter= (counter+1)
}

var files = [];

function searchFolder() {
  var folderId = '0B6wmHZ5c0fzfTjI1bFpKOHI3N3M'; // test folder
  // Log the name of every file in the folder.
 var filesN = DriveApp.getFolderById(folderId).getFiles(); //log files in folder
  while (filesN.hasNext()) files.push(filesN.next().getName());
}

function autoUpdate(){ //updates monthly from newly imported daily
  if (counter == 1){ //counter is made to be 1 when day is uploaded to monthly
  var ss = SpreadsheetApp.openById("1lH9Y12P2Q2OFndIJoAU48ePggXFc9WGcWjolZMcABoc"); 
  //defines target spreadsheet ie monthly
  SpreadsheetApp.setActiveSpreadsheet(ss); //sets target spreadsheet as 
  active
  var range= ss.getRange("A1:A1"); 
  range.activate;  // activates range
  range.setValue('=IMPORTRANGE("1K7Rj4QK-EVjf8lZ0BSew7iDeCtktqWjzgjoaVPe5jSc","sheet1!A1:G6")'); //Puts in IMPORTRANGE into target as a STRING value (just words). Once it hits the sheet, then SHEETS executes IMPORTRANGE not SCRIPTS. In Source sheet, range is selected to import to target (ie A1:G6)
  counter=(counter-1)
  }
}

searchFolder(); // After this function all file names are stored in variable `files`
timeStamp();
autoUpdate();

If you want to log all files in folder too, you can execute Logger.log(files.join('\n'));

Sign up to request clarification or add additional context in comments.

1 Comment

This totally did the trick! thank you so much for educating me and fixing it, can't tell you how much of a lifesaver this is!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.