'AppsScript code show execution completed, but nothing happens to my google sheet
I need to import a csv attachment from my email to a google sheet. I found a script online that seemed like what I needed and when I click 'run' it shows execution completed, but my google sheet is still blank.
I would prefer the code was able to append the data, but if not that's okay as well.
Here's the script I currently have and links to the sheet with current data, email example, report example
function importCSVFromGmail() {
var threads = GmailApp.search("from:MY EMAIL");
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
attachment.setContentTypeFromExtension();
if (attachment.getContentType() === "CSV") {
var sheet = SpreadsheetApp.getActiveSheet();
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
sheet.clearContents().clearFormats();
sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
Solution 1:[1]
Import attachment to google sheet
I have only one message that has an attachment
function myfunk() {
let ts = GmailApp.getInboxThreads();
ts.forEach(t => {
let msgs = t.getMessages();
msgs.forEach(m => {
if(m.getAttachments().length > 0) {
let csv = m.getAttachments()[0].getDataAsString();
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName('Sheet1');
const vs = Utilities.parseCsv(csv);
sh.getRange(1,1,vs.length,vs[0].length).setValues(vs);
}
})
})
}
The original file:
COL1,COL2,COL3,COL4,COL5,COL6,COL7,COL8,COL9,COL10
1,23,31,14,24,27,12,9,33,18
7,19,23,20,16,33,25,18,12,21
16,11,19,35,14,30,4,14,31,15
33,25,9,29,23,1,26,34,1,14
32,19,20,6,34,20,12,3,7,15
29,31,12,33,33,24,36,20,35,23
13,2,9,39,6,23,5,37,30,38
36,5,10,1,32,24,19,6,28,12
38,39,6,15,30,4,3,13,26,37
Sheet1:
| COL1 | COL2 | COL3 | COL4 | COL5 | COL6 | COL7 | COL8 | COL9 | COL10 |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 23 | 31 | 14 | 24 | 27 | 12 | 9 | 33 | 18 |
| 7 | 19 | 23 | 20 | 16 | 33 | 25 | 18 | 12 | 21 |
| 16 | 11 | 19 | 35 | 14 | 30 | 4 | 14 | 31 | 15 |
| 33 | 25 | 9 | 29 | 23 | 1 | 26 | 34 | 1 | 14 |
| 32 | 19 | 20 | 6 | 34 | 20 | 12 | 3 | 7 | 15 |
| 29 | 31 | 12 | 33 | 33 | 24 | 36 | 20 | 35 | 23 |
| 13 | 2 | 9 | 39 | 6 | 23 | 5 | 37 | 30 | 38 |
| 36 | 5 | 10 | 1 | 32 | 24 | 19 | 6 | 28 | 12 |
| 38 | 39 | 6 | 15 | 30 | 4 | 3 | 13 | 26 | 37 |
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Cooper |
