Home > Blockchain >  Google sheet : Get values from the web and keep value matching each file in a folder
Google sheet : Get values from the web and keep value matching each file in a folder

Time:04-24

I am trying to do something and I am almost there but I have been scratching my head on a 'length' issue for quite some time now. What I want to do :

  • Get data from an URL (web csv file)
  • Loop through all spreadsheets in a folder
  • For each spreadsheet only keep the data from the web csv file that contains the spreadsheet's name.

My code works on a single page but not when I try to apply it to all spreadsheets in the folder. I have this error : "TypeError: Cannot read property 'length' of undefined myfunction @ Getproductfiles.gs:35"

Here is the full code :

function myfunction()
{ 
 var root = DriveApp.getFoldersByName("Produits");                                        
  while (root.hasNext())
  { 
   var folder = root.next();    //If the folder is available, get files in the folder
   var files = folder.getFiles();                                                                       
   while(files.hasNext())       //For each file,                                                                    
   { 
    var spreadsheet = SpreadsheetApp.open(files.next()); 

    //import data from URL
     var csvUrl = "myurl";
  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
  var csvData = Utilities.parseCsv(csvContent);
 
  var sheets = spreadsheet.getSheets()
  var sheetIndex=0
  var sheet = sheets[sheetIndex]
   sheet.getRange(2, 1, csvData.length, csvData[0].length).setValues(csvData);

// Always show 3 decimal points
var cell = sheet.getRange("D2:D");
cell.setNumberFormat("00");

//Only keep data that contains the file name
  var name = spreadsheet.getName();
  let range = sheet.getDataRange(),
      maxRows = sheet.getMaxRows(),
      srchCol_1 = 2,
      srchPatt_1 = new RegExp(name)
      newRangeVals = range.getValues().filter(r => r[0] && srchPatt_1.exec(r[srchCol_1])),   
      numRows = newRangeVals.length;  
  range.clearContent();
  sheet.getRange(2,1, numRows, newRangeVals[0].length).setValues(newRangeVals);
  sheet.deleteRows(numRows   1, maxRows - numRows);

//Clean headers
var cell = sheet.getRange(1,1);
cell.setValue("Column1");
var cell = sheet.getRange(1,2);
cell.setValue("Column2");
var cell = sheet.getRange(1,3);
cell.setValue("Column3");
var cell = sheet.getRange(1,4);
cell.setValue("Column4");
var cell = sheet.getRange(1,5);
cell.setValue("Column5");
var cell = sheet.getRange(1,6);
cell.setValue("Column6");

   }
  }
 }

CodePudding user response:

Try to add , at the end of the line:

srchPatt_1 = new RegExp(name)

CodePudding user response:

Try it this way:

function myfunction() {
  var csvUrl = "myurl";
  var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
  var csvData = Utilities.parseCsv(csvContent);
  if (csvData && csvData.length > 0) {
    var root = DriveApp.getFoldersByName("Produits");
    while (root.hasNext()) {
      var folder = root.next();
      var files = folder.getFiles();
      while (files.hasNext()) {
        var ss = SpreadsheetApp.open(files.next());
        var sh = ss.getSheets()[0];
        sh.getRange(2, 1, csvData.length, csvData[0].length).setValues(csvData);
        sh.getRange("D2:D"   sh.getLastRow()).setNumberFormat("00");
        var name = ss.getName();
        let range = sh.getDataRange(), maxRows = sh.getMaxRows(), newRangeVals = range.getValues().filter(r => r[0] && r[2] == name), numRows = newRangeVals.length;
        range.clearContent();
        if (newRangeVals && newRangeVals[0].length) {
          sh.getRange(2, 1, numRows, newRangeVals[0].length).setValues(newRangeVals);
          sh.deleteRows(numRows   1, maxRows - numRows);
          sh.getRange(1, 1, 1, 6).setValues([["Column1", "Column2", "Column3", "Column4", "Column5", "Column6"]]);
        }
      }
    }
  }
}
  • Related