I'm trying to get the data out of a CSV report, located in a specific folder from a shared email and replace the old data from a specific tab in Google Sheets with new ones.
I already tried different solutions, but none seems to get me where I want. The code below is the cleanest that I produced but, for some reason, is not doing anything.
Steps that I followed:
- Open email where the folder is located(it is a shared email, managed in Gmail)
- Search for the folder where is located (label:rpt-its-all-data)
- Take the last email received
- Open email/attachment
- Take the data
- Open Google sheets by ID
- Update date in cell B1 in tab INFO with the current date(I didn't even try to sort this one out)
- Open tab ALLDATA
- Paste values in tab ALLDATA
This is a copy of the Google Sheets to see the format: https://docs.google.com/spreadsheets/d/1GyGxSwUtITeje0-Qx63rB8xChMSbLvn1FDCOKOJITio/edit?usp=sharing
I already tried this code:
function importCSVFromGmail() {
var threads = GmailApp.search("id:AH1rexR2yzlanbk9GyP52tyVKSnXcMDz_miTs1-lLXtJwJB56l2r label:rpt-its-all-data");
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
if (attachment.getContentType() === "text/csv") {
var id = "1F_Z_00-ThdTRzlZ3nYd_pieJHkUjgye3pI_KJfydoP4";
var name = "ALLDATA";
var sheet = SpreadsheetApp.openById(id);
var tab = sheet.getSheetByName(name);
// Clear the content of the sheet
tab.clearContents().clearFormats();
tab.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
var csvData = tab.Utilities.parseCsv(attachment.getDataAsString(), ",");
}
}
With the above code, I didn't get any results. I'm not sure why this is happening.
I don't think that the mail ID in the query is correct to call that email, but I couldn't find any other way to do it.