Getting only the correct attachment

Hi all,

PLEASE HELP ME! I am trying to download a file from a gmail node but I do not know what is the correct file to download.

In the image below you will see that I have 4 files, could be more or less. But I just want to get the files xlxs.

How can I do it?

Information on your n8n setup

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

hi,
try this in a code-Node to only extract the attachment from your email which matches the file-extension “.xls”

// Access the input data
const items = $input.all();

return items.flatMap(item => {
    // Ensure there is binary data
    if (!item.binary) {
        return [];
    }
    
    // Filter binary data to keep only .xls files
    return Object.keys(item.binary)
        .filter(key => item.binary[key].fileExtension === 'xls')
        .map(key => {
            // Create a new item from attachment 
            const attachment = item.binary[key];
            return {
                json: {
                    fileName: attachment.fileName,
                    mimeType: attachment.mimeType,
                    fileSize: attachment.fileSize,
                },
                binary: {
                    data: attachment // Keep the attachment binary
                }
            };
        });
});

Hi, Thanks but it did not work, I did try somethign similiar but is not working

yes, because you filter for .xls and in your screenshot / example the file-ending is .xlsx

maybe you wanna consider using mime-type for filtering or check both variants of fileEnding to be sure. depends on how much of control you have over the incoming data.

It seems is working. I add and If condition to tun twice to check for .xlsx or .xls.

It has been really usefull.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.