Skip to content Skip to sidebar Skip to footer

Filter CVS Before Importing To Google Spreadsheet

I have a Script in a Google Spreadsheet, the script downloads a zipped CSV from an URL and then import it to the spreadsheet. Actually, the CVS is too big and I don't need all the

Solution 1:

Try this:

function descargarzip() {
  var urldescarga = "http://187.191.75.115/gobmx/salud/datos_abiertos/datos_abiertos_covid19.zip"
  var url = urldescarga
  var zipblob = UrlFetchApp.fetch(url).getBlob(); 
  zipblob.setContentTypeFromExtension();
  var unzipblob = Utilities.unzip(zipblob); 
  var unzipstr=unzipblob[0].getDataAsString();
  var csv = Utilities.parseCsv(unzipstr);
  var x = 'You enter the contents x';
  csv.forEach(function(r,i){
    if(r[0]==x) {
      r[0]='';//You have to put something back in there because the csv has to be a rectangular array for setValues();
    }
  });//You could remove an entire line or an entire column
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  ss.getRange(1, 1, csv.length, csv[0].length).setValues(csv);
}

If @TheMaster is correct then try this:

function descargarzip() {
  var urldescarga = "http://187.191.75.115/gobmx/salud/datos_abiertos/datos_abiertos_covid19.zip"
  var url = urldescarga
  var zipblob = UrlFetchApp.fetch(url).getBlob(); 
  zipblob.setContentTypeFromExtension();
  var unzipblob = Utilities.unzip(zipblob); 
  var unzipstr=unzipblob[0].getDataAsString();
  var csv = Utilities.parseCsv(unzipstr);
  var x = 'You enter the contents x';
  var d=0;
  //I tested this on some of my data and I believe it works
  for(var i=0;(i-d)<csv.length;i++) {
    if(csv[i-d][0]==x) {
      csv.splice(i-d++,1);//I think this is correct but I could be wrong in here because I mostly use this approach for deleting rows not portions of the array.  So if you have problems the please share your csv data and I will debug it.
    }
  }
  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
  ss.getRange(1, 1, csv.length, csv[0].length).setValues(csv);
}

Post a Comment for "Filter CVS Before Importing To Google Spreadsheet"