Daniel
Daniel

Reputation: 1

unzip csv file from url in google sheets

I need to unzip a zip file from a url containing a CSV and post it in a sheet, but I'm having problems. I have tried with scripts such as following, but it doesn't seem to work:

function myFunction() {
    var url = 'my URL' 
var blob = UrlFetchApp.fetch(url).getBlob(); 
var zip = Utilities.zip([blob], "my.zip");
var files = Utilities.unzip(zip);
var unzipstr = files[0].getDataAsString();
var csv = Utilities.parseCsv(unzipstr);

var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
ss.getRange(1, 1, csv.length, csv[0].length).setValues(csv);
  
}

I really hope some of you can help solve this on! I'm open for all ideas

Upvotes: 0

Views: 545

Answers (1)

Nikko J.
Nikko J.

Reputation: 5533

From what I've understood, the file in your URL is already a zip file and you are trying to zip a zip file here:

var zip = Utilities.zip([blob], "my.zip");

Remove the zip part of your code and your code should look like this:

function myFunction() {
  var url = 'my URL' 
  var blob = UrlFetchApp.fetch(url).getBlob();
  var file = Utilities.unzip(blob);
  var unzipstr = file[0].getDataAsString();
  var csv = Utilities.parseCsv(unzipstr);

  var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheet1");
  ss.getRange(1, 1, csv.length, csv[0].length).setValues(csv);
}

Output: enter image description here

Upvotes: 1

Related Questions