Reputation: 11
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!
Upvotes: 0
Views: 393
Reputation: 11
The solution was simple.
Split each Script File out into its own project and trigger them individually using a time-based trigger.
Therefore the 7 actions I required each had their own project with a time-based trigger.
Upvotes: 1