Importing CSV from Gmail - Line Breaks - Not Importing Correctly

93 views
Skip to first unread message

Chris Cantrell

unread,
Dec 14, 2021, 5:09:35 PM12/14/21
to Google Apps Script Community
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);
}
}
SampleFile.csv

Martin Hawksey

unread,
Dec 15, 2021, 4:04:34 AM12/15/21
to Google Apps Script Community
Hi Chris,

A similar question was asked on StackOverflow a while back which might have some useful solutions for you to try https://stackoverflow.com/a/57534079/1027723

Reply all
Reply to author
Forward
0 new messages