I am new to Google Scripts so I have been struggling with this issue for some days now. Gone through multiple sources and none have the same issue as me.
I have set a time-based trigger for my script to import a CSV from my Gmail into a sheet. The emails sent are reports pulled daily from DV360 which are then used to populate a Data Studio Dashboard.
My first issue is that either the trigger isn't working properly (which may be unlikely) or that there is something wrong with my code. One weird thing which I have noticed is that the trigger works for one of the reports but does not work for the remaining 6.
I have multiple scripts in this project which all import different CSV attachments from different emails and each populate their own tab in a Google Sheets file.
Once I have run the script once and it has populated its respective sheet, it won't run again even if there is an email with a more recent attachment in my inbox (for example if I ran the script the following day, it won't populate the sheet with that specific day's report).
Below is the code I have used.
function importCSVFromGmail() {
var threads = GmailApp.search("Label:Audience_List");
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 = "1cl5nZiJ2Jh__pMig-BoGTKpE9kM8-5nYe178WdI6ChI";
var name = "Audience_List_Backend";
var sheet = SpreadsheetApp.openById(id);
var tab = sheet.getSheetByName(name);
var tabInfo = sheet.getSheetByName("Audience_List_Backend");
tabInfo.getRange("A1").setValue(new Date());
// Clear the content of the sheet
tab.clearContents().clearFormats();
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
tab.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
}
}
Thank you in advance for any assistance!
Trigger not working or script only runs once, the function ofimportCSVFromGmailis not run. Although the function ofimportCSVFromGmailis run, the script in the if statement is not run. Which is your situation? - TanaikeimportCSVFromGmailis run, the script in the if statement is not run. In that case, how about checking the values used at the if statement again? In your script, it seems that those areattachmentandattachName.substring(attachName.length-4). If this didn't lead to the solution, I apologize. - TanaikeIFcheck to the style described here. Please, report back with your findings so we can help you better. - Jacques-Guzel Heron