Script to automatically import a .csv file into a google doc

jimbogarner

Board Regular
Joined
Apr 22, 2010
Messages
100
Hi,

I'm looking for a script that can automatically import an email I receive in my work gmail account on a daily basis into a google database. The file always comes in the same format/name and from the same email address. I need it to copy the CSV into a specific tab each day overwriting the past data (the CSV is a cumulative file).

I found the below online, however, being completely transparent, I have no idea what i'm really doing with scripts, therefore this was very much copied and pasted and then I attempted to update the emails etc for my specifics, however, it doesn't work

function importsearchresultsCSVFromGmail2() {
var threads = GmailApp.search("from:Analytics.Reports@xxxxxx.com");
var messages = threads[0].getMessages();
var message = messages[messages.length - 1];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
attachment.setContentType('text/csv');
//attachment.setContentTypeFromExtension();
if (attachment.getContentType() === "text/csv") {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var sh0 = sheet.getSheetByName("Daily_analytics")
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");

// Remember to clear the content of the sheet before importing new data
sh0.getRange(3, 1, csvData.length, csvData[0].length).setValues(csvData);
GmailApp.markMessageRead(message);

}

This is the error i'm getting;
1674646703796.png

Thanks,

James
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top