I have a script that will get the content from a .csv file in Google drive and pull it in to a Google Sheet. The script uses the DocsList service which is deprecated. The below works as required but I want to transition the DocsList components to DriveApp to ensure it works for the foreseeable future.
The main issue I see is that the getContentAsString() method does not appear to be available for DriveApp. Is there a direct alternative to getContentAsString() or a combination of other elements that can achieve the same outcome as the below script?
function importFromCSV() {
var fileName = "0B2n-RwpLExXnaXRBWG1aT3NLbm8";
var FileId = DriveApp.getFileById("0B2n-RwpLExXnaXRBWG1aT3NLbm8");
var files = DocsList.getFiles();
var csvFile = "";
for (var i = 0; i < files.length; i++) {
if (files[i].getId() == fileName) {
csvFile = files[i].getContentAsString();
Logger.log(files[i]);
break;
}
}
var csvData = CSVToArray(csvFile, ",");
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
for (var i = 0; i < csvData.length; i++) {
sheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
}
}
The DriveApp file object has a
getBlob()
method, the blob object has agetDataAsString()
method... You can chain both methods to get a string content.Below is the full code, simplified as it should (there were other inconsistencies in your initial code , as already mentioned in your former question).