I receive 12 emails every day containing CSV data (Temp01,Temp02.....
) that I have to import into 12 different sheets (CapTemp01, CapTemp02...
).
Each email is labeled from 01 to 12 (label CapTemp01, 02, 03...
).
The problem is that the script applies to the whole thread, not just the message, which is problematic because all these emails are part of the same thread.
For example, the current script imports CSV data on email labeled 05 regardless of the GmailApp.search(query)
function importCapTemp05() {
var labelName = "CapTemp05";
var start = 0;
var max = 20;
var threads = GmailApp.search("is:unread label:" + labelName, start, max);
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
if (attachment.getContentType() === 'text/csv') {
var sheet = SpreadsheetApp.openById("1SRo3iBaza4l0G9mlg-jZ589lwaORtx_jfPgX3gR73P0").getSheetByName("CapTemp05_G");
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ',').slice(1);
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
var column = sheet.getRange("B2:B");
column.setNumberFormat("@")
var ranges = sheet.getRange("B2:B");
var data = ranges.getValues();
for (var row = 0; row < data.length; row++) {
for (var col = 0; col < data[row].length; col++) {
data[row][col] = (data[row][col]).toString().replace(".", ",");
}
}
ranges.setValues(data);
}
GmailApp.markMessageRead(message);
}
Maybe using Gmail.Users.Messages.list
Does anyone know how to import CSV data only to a message with a specific label?
Thanks