Skip to content

Instantly share code, notes, and snippets.

@gdalgas
Created December 21, 2019 00:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdalgas/7cd07ee3f6de422cd8050c76fb16a503 to your computer and use it in GitHub Desktop.
Save gdalgas/7cd07ee3f6de422cd8050c76fb16a503 to your computer and use it in GitHub Desktop.
function importCSVFromGmail() {
var threads = GmailApp.search("redacted");
var message = threads[0].getMessages().pop();
var attachment = message.getAttachments()[0];
if (attachment != null) {
var attachName = attachment.getName();
// Is the attachment a CSV file
if (attachName.substring(attachName.length-4) === ".csv") {
var id = "redacted";
var name = "Data Sheet";
var sheet = SpreadsheetApp.openById(id);
var tab = sheet.getSheetByName(name);
// Clear the content of the sheet
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
tab.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment