Chris Ward
Chris Ward

Reputation: 136

If statement that does nothing if the first condition is not met

I'm busy writing a script that includes an If statement and I'm trying to make the else part of the statement do nothing if the first condition isn't met. The script checks a google drive folder for a file name and if it is present the first condition should run, else if it isn't present it should do nothing.

I've tried using else{ return false;} , else{ } and none are working. I've also tried swapping the conditions around but that didn't work either

I keep getting an error saying "Exception: Cannot retrieve the next object: iterator has reached the end. (line 7, file "calls")" because the file is deleted once the data is retrieved from it.

The first condition works perfectly but if the csv file isn't present in the folder the above message is displayed. The idea is that I set a trigger that runs every minute to check the folder.

Below is what I have so far. Any help would be greatly appreciated.

function calls(){

var hotFolder = DriveApp.getFolderById('idHere');
var targetSheet = SpreadsheetApp.openById('idHere').getSheetByName('sheetNameHere');
var callsSheet = SpreadsheetApp.openById('idHere').getSheetByName('sheetNameHere');

var callsCsv = hotFolder.getFilesByName('fileName.csv.Here').next();

    if(callsCsv) {
    
          var csvData = Utilities.parseCsv(callsCsv.getBlob().getDataAsString());
          
          targetSheet.getRange(1,1,csvData.length,csvData[0].length).setValues(csvData);
          
          callsCsv.setTrashed(true);
          
          var targetSheetData = targetSheet.getRange(2,1,targetSheet.getLastRow()-1,targetSheet.getLastColumn()).getValues();
          
          var handleTime = targetSheetData.map(function(row){
          
                if([row[9]] != 'ABANDON') {
                
                return [row[6] + row[7] +15];
                
                } else {
                
                return [['']];
                
                } 
                                                         
             });
             
          targetSheet.getRange(2,12,handleTime.length).setValues(handleTime);
         
          var newData = targetSheet.getRange(2,1,targetSheet.getLastRow(),targetSheet.getLastColumn()).getValues();
          
          callsSheet.getRange(callsSheet.getLastRow,1,newData.length,newData[0].length).setValues(newData);
          
          targetSheet.clear();
         
         } else {
                               
      }
}
         

Upvotes: 0

Views: 852

Answers (1)

ale13
ale13

Reputation: 6062

The "Exception: Cannot retrieve the next object: iterator has reached the end. (line 7, file "calls")" error message you are receiving is due to the fact that the it's not possible to get the next item in the collection of files or folders - probably because of the deletion from the previous run.

What you can do in this situation is to use the hasNext() method. According to its documentation :

hasNext() > Determines whether calling next() will return an item.

Return

Boolean — true if next() will return an item; false if not

Therefore, you can change your code to this:

var callsCsv = hotFolder.getFilesByName('fileName.csv.Here');
    if (callsCsv.hasNext()) {
        var callsCsvFile = callsCsv.next();
        // your code here - callsCsv becomes callsCsvFile
    }

Reference

Upvotes: 2

Related Questions