I need to pull csv from a url that requires basic auth.
I found this code that works, in addition to parsing csv, cleaning and installing cells. I think there are a few errors, as this is old code, for example there clear.contents()should be clear.content().
And even with hard-coding the data in the sheets, I'm still trying to get it to work, has anyone else found a solution?
function parseCsvResponse(csvString) {
var retArray = [];
var strLines = csvString.split(/\n/g);
var strLineLen = strLines.length;
for (var i = 0; i < strLineLen; i++) {
var line = strLines[i];
if (line != '') {
retArray.push(line.replace(/"/g, "").split(/,/));
}
}
return retArray;
}
function populateSheetWithCSV(sheet, csvUrl, user, pw) {
var resp = UrlFetchApp.fetch(csvUrl, {
headers: {
'Authorization': 'Basic ' + Utilities.base64Encode(user + ':' + pw, Utilities.Charset.UTF_8)
}
});
var csvContent = parseCsvResponse(resp.getContentText());
sheet.clearContents().clearFormats();
sheet.getRange(1, 1, csvContent.length , csvContent[0].length ).setValues(csvContent);
}
source
share