title: Import CSV from Gmail attachment into Google Sheets
body: <p>I'm looking to import a CSV attachment of the latest email received from a specific email address into a Google Sheet.</p>
  
  <p>I've used a collection of solutions found on this site and none seem to work for me. I'm importing an attachment from a Gmail message and all that gets imported into the Google sheet is a bunch of weird characters and question marks. It's almost looks like it imported the email itself in another font as opposed to the contents of the attachment.</p>
  
  <p>If i switch the attachment.setContentType with the commented out alternative, then nothing gets imported at all.</p>
  
  <pre><code>function importCSVFromGmail() {
  
  var threads = GmailApp.search("from:EMAIL ADDRESS");
  var messages = threads[0].getMessages();
  var message = messages[messages.length - 1];
  var attachment = message.getAttachments()[0];
  
  // Is the attachment a CSV file
  
  attachment.setContentType('text/csv'); 
  //attachment.setContentTypeFromExtension();
  
  if (attachment.getContentType() === "text/csv") {
  
  var sheet = SpreadsheetApp.getActiveSheet();
  var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
  
  // Remember to clear the content of the sheet before importing new data
  sheet.clearContents().clearFormats();
  sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
   }
  }
  </code></pre>
  
  <p>Actual results look something like this:</p>
  
  <p>�PNG</p>
  
  <p>"IHDR
  �h*BĠW@1ࣨE�
  o�$5���g�C)P$@�E(�HU��</p>