Reputation: 61
I'm importing a CSV from an email with the following code:
function RetrieveAttachment()
{
var threads = GmailApp.search("Report*")
var msgs = GmailApp.getMessagesForThreads(threads);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var today= new Date();today.setDate(today.getDate());
var today= Utilities.formatDate(today, "GMT+1", "dd.MM.yyyy");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import");
for(var i = 0; i < msgs.length; i++)
{
for(var j = 0; j < msgs[i].length; j++)
{
var msgdate = Utilities.formatDate(new Date(msgs[i][j].getDate()), "GMT+1", "dd.MM.yyyy");
if(msgdate == today)
{
var attachments = msgs[i][j].getAttachments();
for(var k = 0; k < attachments.length; k++)
{
var attachmentName = attachments[k].getName();
var stringValue = attachmentName.search("*");
if(stringValue > -1)
{
var attachmentData = attachments[k].getDataAsString();
var parseCsv = Utilities.parseCsv(attachmentData, ",");
sheet.clearContents();
sheet.getRange(1,1, parseCsv.length, parseCsv[0].length).setValues(parseCsv);
}
}
}
}
}
}
Usually this works just fine, but with one specific csv I get via email only this two question marks are being written in the Spreadsheet:
I checked with the debugger and apparently the whole content of the csv is being read, however not correctly parsed:
I presume this is some coding problem, however I tried with the base64Encode/base64Decode utilities without any success.
Does someone have any clue on how to solve this?
Upvotes: 1
Views: 973
Reputation: 61
The original CSV file was UTF-16 and tab separated, I solved it specifying the encoding and different separator:
var attachmentData = attachments[k].getDataAsString("UTF-16");
var parseCsv = Utilities.parseCsv(attachmentData, "\t");
Here the new Code
function RetrieveAttachment()
{
var threads = GmailApp.search("Report*")
var msgs = GmailApp.getMessagesForThreads(threads);
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var today= new Date();today.setDate(today.getDate());
var today= Utilities.formatDate(today, "GMT+1", "dd.MM.yyyy");
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import");
for(var i = 0; i < msgs.length; i++)
{
for(var j = 0; j < msgs[i].length; j++)
{
var msgdate = Utilities.formatDate(new Date(msgs[i][j].getDate()), "GMT+1", "dd.MM.yyyy");
if(msgdate == today)
{
var attachments = msgs[i][j].getAttachments();
for(var k = 0; k < attachments.length; k++)
{
var attachmentName = attachments[k].getName();
var stringValue = attachmentName.search("*");
if(stringValue > -1)
{
var attachmentData = attachments[k].getDataAsString("UTF-16");
var parseCsv = Utilities.parseCsv(attachmentData, "\t");
sheet.clearContents();
sheet.getRange(1,1, parseCsv.length, parseCsv[0].length).setValues(parseCsv);
}
}
}
}
}
}
Upvotes: 2