Hi, I am very early in my development learning and automating some tasks with Gmail and Sheets. I get a CSV file each day with some data and want to add it to a spreadsheet.
I have found this code and it seems to work **if** my CSV file is sent to me correctly. Like the sample file attached, it has line breaks in one of the fields.
I'm struggling how best to 1) evaluate the 2d array and remove any line breaks before writing to Sheets and 2), adding to the bottom (appending the first data line in the CSV file; not erasing).
Appreciate any guidance here.
function importCSVFromGmail() {
var threads = GmailApp.search("from:{email_address}"); // enter search criteria here
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
var sheet = SpreadsheetApp.openById('{id}').getActiveSheet();
if (attachment.getContentType() === "text/csv") {
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
sheet.clearContents().clearFormats();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
}