What getAttachments() actually returns
The Gmail API's getAttachments() method gives you everything attached to the MIME envelope: PDFs, spreadsheets, Word docs, but also every inline image in every HTML signature, and the .ics calendar invite that Google Calendar automatically appends to meeting emails. The first time I ran a naive version of this script against a busy inbox I ended up with 400 tiny PNG files of company logos, an .ics for every calendar event in the last week, and exactly zero of the invoices I was trying to collect.
The fix is to filter on content type before writing to Drive. The script above skips application/ics unconditionally. For images it applies a size heuristic: anything under 20,000 bytes is almost certainly a signature icon or a spacer pixel, not a real attachment. That threshold holds well in practice; a genuine photograph or scanned document is rarely under 20 KB. Adjust the number if your domain sends small image attachments legitimately.
Note that getAttachments() does not expose inline images that are referenced via a Content-ID header but hosted remotely (tracked pixel style). Those never appear in the list, so you don't need to filter them. The ones you do need to filter are locally-attached inline parts, which Gmail encodes as standard MIME attachments with an image/* content type.