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

jimbogarner

Board Regular
Joined
Apr 22, 2010
Messages
102
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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,223,908
Messages
6,175,307
Members
452,633
Latest member
DougMo

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