Saves each new Gmail message that matches your search as a row in your spreadsheet, and never saves the same message twice.
Uses
Gmail
Google Sheets
Save matching emails to a spreadsheet
emailSearch to blank.Set emailSearch to blank.spreadsheetUrl to blank.Set spreadsheetUrl to blank.sheetTabName to Emails.Set sheetTabName to Emails.savedLabel to Saved to sheet.Set savedLabel to Saved to sheet.maxEmailsPerRun to 10.Set maxEmailsPerRun to 10.excerptLength to 300.Set excerptLength to 300.gmailAccountIndex to 0.Set gmailAccountIndex to 0.emailLimit to maxEmailsPerRun multiplied by 1.Set emailLimit to maxEmailsPerRun multiplied by 1.emailLimit is greater than or equal to 1 and emailLimit is less than or…If not emailLimit is greater than or equal to 1 and emailLimit is less than or…Yes
No
No steps
excerptLimit to excerptLength multiplied by 1.Set excerptLimit to excerptLength multiplied by 1.excerptLimit is greater than or equal to 0 and excerptLimit is less than…If not excerptLimit is greater than or equal to 0 and excerptLimit is less than…Yes
No
No steps
accountIndex to gmailAccountIndex multiplied by 1.Set accountIndex to gmailAccountIndex multiplied by 1.accountIndex is greater than or equal to 0 and accountIndex is less than…If not accountIndex is greater than or equal to 0 and accountIndex is less than…Yes
'gmailAccountIndex' is not a whole number from 0 up, so the links in the sheet will open in the first Google account you are signed in to..Show in the output: 'gmailAccountIndex' is not a whole number from 0 up, so the links in the sheet will open in the first Google account you are signed in to..accountIndex to 0.Change accountIndex to 0.No
No steps
searchQuery to emailSearch -label:@savedLabel.Set searchQuery to emailSearch -label:@savedLabel.searchQuery. Save it as found.List emails matching searchQuery. Save it as found.matchCount to 0.Set matchCount to 0.listErrorIf a step below fails, save the error as listErrorTry
found.emails and save it as matchCount.Count the items in found.emails and save it as matchCount.On error
listError as text.Stop and report a problem: Gmail did not return a readable list of messages for the search '@emailSearch', so nothing was saved this run. Details: listError as text.processedCount to 0.Set processedCount to 0.savedCount to 0.Set savedCount to 0.failedCount to 0.Set failedCount to 0.message in found.emails:For each message in found.emails:processedCount is greater than or equal to emailLimitIf processedCount is greater than or equal to emailLimitYes
No
No steps
messageErrorIf a step below fails, save the error as messageErrorTry
message.id and save it as messageId.Trim message.id and save it as messageId.messageId is emptyIf messageId is emptyYes
failedCount.Add 1 to failedCount.Skipped a message that Gmail returned without an id..Show in the output: Skipped a message that Gmail returned without an id..No
savedLabel to email messageId.Add label savedLabel to email messageId.processedCount.Add 1 to processedCount.subject to (no subject).Set subject to (no subject).missingFieldIf a step below fails, save the error as missingFieldTry
message.subject and save it as fieldValue.Trim message.subject and save it as fieldValue.fieldValue isn't emptyIf fieldValue isn't emptyYes
subject to fieldValue.Change subject to fieldValue.No
No steps
On error
No steps
sender to (unknown sender).Set sender to (unknown sender).missingFieldIf a step below fails, save the error as missingFieldTry
message.from and save it as fieldValue.Trim message.from and save it as fieldValue.fieldValue isn't emptyIf fieldValue isn't emptyYes
sender to fieldValue.Change sender to fieldValue.No
No steps
On error
No steps
recipient to blank.Set recipient to blank.sentAt.Get the current date and time and save it as sentAt.headers to an empty record.Set headers to an empty record.missingHeadersIf a step below fails, save the error as missingHeadersTry
messageId. Save it as headers.Get the sender, subject and date of email messageId. Save it as headers.On error
Could not read this message's headers; the row records the time it was saved instead of the time it was sent, and leaves the To column empty..Show in the output: Could not read this message's headers; the row records the time it was saved instead of the time it was sent, and leaves the To column empty..missingDateIf a step below fails, save the error as missingDateTry
headers.Date and save it as dateHeader.Trim headers.Date and save it as dateHeader.dateHeader isn't emptyIf dateHeader isn't emptyYes
sentAt to dateHeader.Change sentAt to dateHeader.No
No steps
On error
No steps
missingRecipientIf a step below fails, save the error as missingRecipientTry
headers.To and save it as toHeader.Trim headers.To and save it as toHeader.toHeader isn't emptyIf toHeader isn't emptyYes
recipient to toHeader.Change recipient to toHeader.No
No steps
On error
No steps
excerpt to blank.Set excerpt to blank.missingTextIf a step below fails, save the error as missingTextTry
message.body and save it as sourceText.Trim message.body and save it as sourceText.sourceText to excerptLimit characters and save it as excerpt.Shorten sourceText to excerptLimit characters and save it as excerpt.On error
No steps
excerpt is emptyIf excerpt is emptyYes
missingTextIf a step below fails, save the error as missingTextTry
message.snippet and save it as sourceText.Trim message.snippet and save it as sourceText.sourceText to excerptLimit characters and save it as excerpt.Shorten sourceText to excerptLimit characters and save it as excerpt.On error
No steps
No
No steps
messageLink to https://mail.google.com/mail/u/@accountIndex/#all/@messageId.Set messageLink to https://mail.google.com/mail/u/@accountIndex/#all/@messageId.rowErrorIf a step below fails, save the error as rowErrorTry
sheetTabName in spreadsheetUrl.Add a row to sheetTabName in spreadsheetUrl.savedCount.Add 1 to savedCount.On error
failedCount.Add 1 to failedCount.unlabelErrorIf a step below fails, save the error as unlabelErrorTry
savedLabel from email messageId.Remove label savedLabel from email messageId.On error
messageId after its row failed - remove it by hand so the message is saved on the next run.Show in the output: Could not take the '@savedLabel' label back off message messageId after its row failed - remove it by hand so the message is saved on the next run.rowError as text.Show in the output: Could not save one email to the sheet; it will be tried again on the next run: rowError as text.On error
failedCount.Add 1 to failedCount.messageError as text.Show in the output: Could not process one email: messageError as text.failedCount is greater than 0If failedCount is greater than 0Yes
savedCount email(s) to the sheet, but failedCount could not be saved - the messages above say why. A message that is not saved is tried again on the next run.Stop and report a problem: Saved savedCount email(s) to the sheet, but failedCount could not be saved - the messages above say why. A message that is not saved is tried again on the next run.No
No steps
savedCount is 0If savedCount is 0Yes
No new email matching the search to save..Show in the output: No new email matching the search to save..No
savedCount email(s) to the sheet.Show in the output: Saved savedCount email(s) to the sheet.