Home > Enterprise >  cannot read property "0" of undefined error
cannot read property "0" of undefined error

Time:09-08

I have a code that pulls information from all excel type files in some folders on google drive. The problem is that there are over 100 files and the code only pulls data from around 30 files and shows the following error: "TypeError: Cannot read property '0' of undefined" The error is in line 12, "console.log(lista_carpetas_ok2[i][0]) // Here, you can see the folder ID in the log."

function listfechas() {
  var ss2 = SpreadsheetApp.getActiveSpreadsheet();
  var carpetasSheet = ss2.getSheetByName("carpetas");
  var lista_carpetas = carpetasSheet.getRange("C2:C"   carpetasSheet.getLastRow()).getValues();
  var lista_carpetas_ok2 = lista_carpetas.filter(([a]) => a);
  var sheet2 = ss2.getSheetByName("SS23");
  sheet2.clear();
  sheet2.appendRow(["Folder", "Name", "SMS","rec SMS",  "FIT",  "rec FIT",  "2FIT", "rec 2FIT"  ,"3FIT" ,"rec 3FIT" ,"PP"   ,"rec PP"   ,"2PP"  ,"rec 2PP", "3PP",  "rec 3PP","SHIP",   "rec SHIP", "2SHIP","rec 2SHIP"]);
  

 for (var i = 0; i < 5; i  ) {
    console.log(lista_carpetas_ok2[i][0]) // Here, you can see the folder ID in the log.
    var folderid = lista_carpetas_ok2[i][0];
  try {
    var parentFolder =DriveApp.getFolderById(folderid);
    listFiles(parentFolder,parentFolder.getName())
    listSubFolders(parentFolder,parentFolder.getName());
  } catch (e) {
    Logger.log(e.toString());
  }
}

function listSubFolders(parentFolder,parent) {
  var childFolders = parentFolder.getFolders();
  while (childFolders.hasNext()) {
    var childFolder = childFolders.next();
    Logger.log("Fold : "   childFolder.getName());
    listFiles(childFolder,parent)
    listSubFolders(childFolder,parent   "|"   childFolder.getName());
  }
}

function listFiles(fold,parent){
  var data = [];
  var files = fold.getFilesByType(MimeType.GOOGLE_SHEETS);
   try {
  while (files.hasNext()) {
    var file = files.next();
      var sms = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("A2").getValue();
      var recsms = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("B2").getValue();
      var fit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("C2").getValue();
      var recfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("D2").getValue();
      var dfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("E2").getValue();
      var recdfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("F2").getValue();
      var tfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("G2").getValue();
      var rectfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("H2").getValue();
      var pp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("I2").getValue();
      var recpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("J2").getValue();
      var dpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("K2").getValue();
      var recdpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("L2").getValue();
      var tpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("M2").getValue();
      var rectpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("N2").getValue();
      var ship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("O2").getValue();
      var recship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("P2").getValue();
      var dship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("Q2").getValue();
      var recdship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("R2").getValue();
      

    var fullRange = sheet2.getRange("A1:Z1001");
    fullRange.setVerticalAlignment(DocumentApp.VerticalAlignment.TOP);
    data = [ 
     
    
      fold.getName(),
      file.getName(),
      sms,
      recsms,
      fit,
      recfit,
      dfit,
      recdfit,
      tfit,
      rectfit,
      pp,
      recpp,
      dpp,
      recdpp,
      tpp,
      rectpp,
      ship,
      recship,
      dship,
      recdship,

      ];
    sheet2.appendRow(data);
  }; 

    } catch (e) {
      // In this modification, when your folder ID cannot be used, that folder ID is skipped. At that time, an error message can be seen in the log.
      console.log(e.message);
    }
}
}

When I wrote the code, I added catch (e) which I thought skipped the folder if there was no excel file in that folder, but the code stops at folder "QM0201" which does not have an excel file in it. Does anybody know how I can fix it? I would like the code to run even if some folders don't have an excel files in them, those should just be skipped. Thank you so much in advance! Any help is appreciated. picture

CodePudding user response:

To avoid the error, replace

for (var i = 0; i < 5; i  ) {

by

for (var i = 0; i < lista_carpetas_ok2.length; i  ) {

This might also help to avoid the code to stop at certain folder.


Regarding the error > Info Cannot read property 'getRange' of null"

This occurs because the spreadsheet hasn't a sheet named fechas to avoid this error you might add

if(!SpreadsheetApp.open(file).getSheetByName("fechas")) break;

above of

var sms = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("A2").getValue();

if(


Debugging tips:

  • To verify that the correct values are assigned to lista_carpetas_ok2, add console.log(JSON.stringify(lista_carpetas_ok2)); just below lista_carpetas_ok2 declaration so you can review the values assigned to the variable.

  • To have more informative logs when an error occurs, instead of

      } catch (e) {
        Logger.log(e.toString()); //  or console.log(e.message);
      }
    

    use

    } catch (e) {
        console.log(e.message, e.stack);
    }
    

CodePudding user response:

This is hardly a cause of the error but who knows, the code has the very unefficient part — 72 calls to the server. It can be greatly improved if you change this:

var sms = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("A2").getValue();
var recsms = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("B2").getValue();
var fit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("C2").getValue();
var recfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("D2").getValue();
var dfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("E2").getValue();
var recdfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("F2").getValue();
var tfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("G2").getValue();
var rectfit = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("H2").getValue();
var pp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("I2").getValue();
var recpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("J2").getValue();
var dpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("K2").getValue();
var recdpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("L2").getValue();
var tpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("M2").getValue();
var rectpp = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("N2").getValue();
var ship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("O2").getValue();
var recship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("P2").getValue();
var dship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("Q2").getValue();
var recdship = SpreadsheetApp.open(file).getSheetByName("fechas").getRange("R2").getValue();

To this:

var values = SpreadsheetApp.open(file).getSheetByName("fechas").getRange('A2:R2').getValues().flat();
var [sms,recsms,fit,recfit,dfit,recdfit,tfit,rectfit,pp,recpp,dpp,recdpp,tpp,rectpp,ship,recship,dship,recdship] = values;

It will reduce the quantity of calls to 4!

It should work faster and, if the real cause of the error is exceed of time limit (~6 min) this improvement can help. Try it.

Probably you could simplify the code further. Instead of this:

 data = [ 
     
    
      fold.getName(),
      file.getName(),
      sms,
      recsms,
      fit,
      recfit,
      dfit,
      recdfit,
      tfit,
      rectfit,
      pp,
      recpp,
      dpp,
      recdpp,
      tpp,
      rectpp,
      ship,
      recship,
      dship,
      recdship,

      ];

You can use this:

var values = SpreadsheetApp.open(file).getSheetByName("fechas").getRange('A2:R2').getValues().flat();
var data = [fold.getName(), file.getName(), ...values];

No need to use the 18 variables, as far as I can see. It barely affects on the speed, though.

  • Related