Last active
August 5, 2023 09:22
-
-
Save oshliaer/70e04a67f1f5fd96a708 to your computer and use it in GitHub Desktop.
Extract Gmail content to a spreadsheet #gas #gmail #sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var SHEET_ID = YOUR_SPREADSHEET_ID; | |
var SHEET_NAME = YOUR_SHEET_NAME; | |
function getEmails_(q){ | |
var emails = []; | |
var thds = GmailApp.search(q); | |
for(var i in thds){ | |
var msgs = thds[i].getMessages(); | |
for(var j in msgs){ | |
emails.push([msgs[j].getBody().replace(/<.*?>/g, '\n').replace(/^\s*\n/gm, '').replace(/^\s*/gm, '').replace(/\s*\n/gm, '\n')]); | |
} | |
} | |
return emails; | |
} | |
function appendData_(sheet, array2d){ | |
sheet.getRange(sheet.getLastRow() + 1, 1, array2d.length, array2d[0].length).setValues(array2d); | |
} | |
function run(){ | |
//Gmail Advanced search https://support.google.com/mail/answer/7190 | |
var array2d = getEmails_("the"); | |
if(array2d) { | |
var ss = SpreadsheetApp.openById(SHEET_ID); | |
var sheet = ss.getSheetByName(SHEET_NAME); | |
if(!sheet) sheet = ss.insertSheet(SHEET_NAME); | |
appendData_(sheet, array2d); | |
} | |
} |
@hub2git replace in this code, https://ctrlq.org/code/20053-save-gmail-to-google-spreadsheet
getBody with getSubject
This script works very well, but it stops at 500 rows. The directory I am searching has 553 items in it. Any idea why?
For me it stops at number 512 items. What to do?
Is it some restriction of spreadsheet of the script itself?
getting a "Bad value (line 24, file "Code")"
then it highlights this part : var ss = SpreadsheetApp.openById(555982521);
i made sure that its the correct id
Can you please guide me how can i edit the code instead of getting 1 data i'll get more other info such as FROM, CC, EMAIL SUBJECT. I keep trying to alter the code it only save in 1 column.
You need to edit this row
emails.push([msgs[j].getBody().replace(
/<.*?>/g, '\n').replace(
/^\s*\n/gm, '').replace(
/^\s*/gm, '').replace(/\s*\n/gm,
'\n')]);
Just add something like this
emails.push([
msgs[j].getBody().replace(/<.*?>/g, '\n').replace(/^\s*\n/gm, '').replace(
/^\s*/gm, '').replace(/\s*\n/gm, '\n'),
msgs[j].getSubject(),
msgs[j].getFrom()
]);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Somehow the script doesn't get more than 553 rows but I've got more than 3000 emails. Any idea why is this happening?