OCR receipts from Google Drive
工作流概述
这是一个包含10个节点的复杂工作流,主要用于自动化处理各种任务。
工作流源代码
{
"id": "dVDyWWhO5FdPM3qx",
"meta": {
"instanceId": "5b860a91d7844b5237bb51cc58691ca8c3dc5b576f42d4d6bbedfb8d43d58ece",
"templateCredsSetupCompleted": true
},
"name": "OCR receipts from Google Drive",
"tags": [],
"nodes": [
{
"id": "0794d7e7-196f-46a6-b3cf-85faa436e21e",
"name": "When clicking ‘Test workflow’",
"type": "n8n-nodes-base.manualTrigger",
"position": [
660,
200
],
"parameters": {},
"typeVersion": 1
},
{
"id": "94342020-7019-4565-8f18-5ca3d3512f80",
"name": "Sticky Note",
"type": "n8n-nodes-base.stickyNote",
"position": [
1320,
660
],
"parameters": {
"width": 1120.9554973821976,
"height": 1062.9450261780098,
"content": "# Recognize invoices and convert them into structured JSON
## Video Demo
https://youtu.be/mGPt7fqGQD8
## Quick OCR playground
### 1. Get your OakPDF OCR API key here:
https://rapidapi.com/restyler/api/receipt-and-invoice-ocr-api
### 2. Poceed to the OCR Playground and upload your document or use example files:
https://ocr.oakpdf.com/
**The API can recognize any document format: medical, financial, legal -- let me know which format you want to try and I will be happy to add it to the Playground!**
## Running the n8n Workflow
This workflow allows you to recognize a folder with receipts or invoices (make sure your files are in .pdf, .png, or .jpg format). The workflow can be triggered via the \"Test workflow\" button, and it also monitors the folder for new files, automatically recognizing them.
### 1. n8n import glitch
After import, the trigger node \"When clicking 'Test workflow'\" might be disconnected. You need to connect it via 2 arrows to \"Google Sheets1\" and \"Google Drive\" nodes. So, the workflow has 2 triggers - via button, and via Google Sheets \"new file\" event - both of these triggers should be connected to 2 nodes.
Here is how it should looks like: https://ocr.oakpdf.com/n8n_fix.png
### 2. Set up RapidAPI HTTP auth key
Create new \"HTTP header\" n8n credential and paste your RapidAPI key from https://rapidapi.com/restyler/api/receipt-and-invoice-ocr-api into it. https://ocr.oakpdf.com/n8n_api_key.png
Make sure \"HTTP Request\" node uses this credential.
### 3. Set up your Google Auth
You need a Google connection to work with your Google Sheets and Google Drive accounts: https://docs.n8n.io/integrations/builtin/credentials/google/oauth-generic/#finish-your-n8n-credential
### 4. Set up Google Sheets
Copy this Google Sheets document: https://docs.google.com/spreadsheets/d/1G0w-OMdFRrtvzOLPpfFJpsBVNqJ9cfRLMKCVWfrTQBg/edit?usp=sharing
# Custom document formats and advanced usage
Email: contact@scrapeninja.net
Linkedin: https://www.linkedin.com/in/anthony-sidashin/
"
},
"typeVersion": 1
},
{
"id": "77f96df1-8ee3-48aa-b602-d13df568c8ef",
"name": "OCR recognize",
"type": "n8n-nodes-base.httpRequest",
"position": [
1820,
420
],
"parameters": {
"url": "https://receipt-and-invoice-ocr-api.p.rapidapi.com/recognize",
"method": "POST",
"options": {},
"sendBody": true,
"contentType": "multipart-form-data",
"sendHeaders": true,
"authentication": "genericCredentialType",
"bodyParameters": {
"parameters": [
{
"name": "file",
"parameterType": "formBinaryData",
"inputDataFieldName": "data"
},
{
"name": "settings",
"value": "{ \"documentType\": \"invoice\" }"
}
]
},
"genericAuthType": "httpHeaderAuth",
"headerParameters": {
"parameters": [
{}
]
}
},
"credentials": {
"httpHeaderAuth": {
"id": "REKoulS8g286TBGw",
"name": "ScrapeNinja RapidAPI"
}
},
"typeVersion": 4.2
},
{
"id": "44a107a8-e658-4ad3-be75-497758621c7c",
"name": "Unserialize response JSON",
"type": "n8n-nodes-base.code",
"position": [
2040,
420
],
"parameters": {
"jsCode": "// Loop over input items and add a new field called 'myNewField' to the JSON of each one
for (const item of $input.all()) {
item.json.parsedData = JSON.parse(item.json.result.data);
}
return $input.all();"
},
"typeVersion": 2
},
{
"id": "4f34624f-3161-4baf-8ab7-1d84502c691b",
"name": "On new file in Google Drive",
"type": "n8n-nodes-base.googleDriveTrigger",
"position": [
660,
540
],
"parameters": {
"event": "fileCreated",
"options": {},
"pollTimes": {
"item": [
{
"mode": "everyMinute"
}
]
},
"triggerOn": "specificFolder",
"folderToWatch": {
"__rl": true,
"mode": "list",
"value": "1MjLoaDp2KgJgJDfgUce8RmniwGBUOZnI",
"cachedResultUrl": "https://drive.google.com/drive/folders/1MjLoaDp2KgJgJDfgUce8RmniwGBUOZnI",
"cachedResultName": "n8n_test_ocr"
}
},
"credentials": {
"googleDriveOAuth2Api": {
"id": "6kO9ougy9t3XrL52",
"name": "Google Drive account"
}
},
"typeVersion": 1
},
{
"id": "30591844-baaa-4f04-860b-436489780a2f",
"name": "Load files from Google Drive folder",
"type": "n8n-nodes-base.googleDrive",
"position": [
1040,
540
],
"parameters": {
"filter": {
"folderId": {
"__rl": true,
"mode": "list",
"value": "1MjLoaDp2KgJgJDfgUce8RmniwGBUOZnI",
"cachedResultUrl": "https://drive.google.com/drive/folders/1MjLoaDp2KgJgJDfgUce8RmniwGBUOZnI",
"cachedResultName": "n8n_test_ocr"
}
},
"options": {},
"resource": "fileFolder",
"returnAll": true
},
"credentials": {
"googleDriveOAuth2Api": {
"id": "6kO9ougy9t3XrL52",
"name": "Google Drive account"
}
},
"executeOnce": true,
"typeVersion": 3
},
{
"id": "f7887199-151d-4320-aa0c-5c2c9fdeca81",
"name": "Filter processed files",
"type": "n8n-nodes-base.merge",
"position": [
1360,
420
],
"parameters": {
"mode": "combine",
"options": {},
"joinMode": "keepNonMatches",
"outputDataFrom": "input2",
"fieldsToMatchString": "id"
},
"typeVersion": 3
},
{
"id": "9b1fac99-d718-4b47-8b7c-c19a6c9a8544",
"name": "Download file for OCR",
"type": "n8n-nodes-base.googleDrive",
"position": [
1600,
420
],
"parameters": {
"fileId": {
"__rl": true,
"mode": "id",
"value": "={{ $json.id }}"
},
"options": {},
"operation": "download"
},
"credentials": {
"googleDriveOAuth2Api": {
"id": "6kO9ougy9t3XrL52",
"name": "Google Drive account"
}
},
"typeVersion": 3
},
{
"id": "c91a5931-43b2-4eec-bb17-7becdc2e15a8",
"name": "Save OCR result into Sheets",
"type": "n8n-nodes-base.googleSheets",
"position": [
2260,
420
],
"parameters": {
"columns": {
"value": {
"id": "={{ $('Load files from Google Drive folder').item.json.id }}",
"data": "={{ $json.parsedData }}",
"from": "={{ $json.parsedData.from.company_name }} (country: {{ $json.parsedData.from.addr_country_code }})",
"amount": "={{ $json.parsedData.total_due }} {{ $json.parsedData.currency }}",
"filename": "={{ $('Load files from Google Drive folder').item.json.name }}",
"line1_cost": "={{ $json.parsedData.lines[0].line_cost }}",
"line1_descr": "={{ $json.parsedData.lines[0].descr }}"
},
"schema": [
{
"id": "id",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "id",
"defaultMatch": true,
"canBeUsedToMatch": true
},
{
"id": "filename",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "filename",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "data",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "data",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "from",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "from",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "amount",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "amount",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "line1_descr",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "line1_descr",
"defaultMatch": false,
"canBeUsedToMatch": true
},
{
"id": "line1_cost",
"type": "string",
"display": true,
"removed": false,
"required": false,
"displayName": "line1_cost",
"defaultMatch": false,
"canBeUsedToMatch": true
}
],
"mappingMode": "defineBelow",
"matchingColumns": [
"id"
]
},
"options": {},
"operation": "appendOrUpdate",
"sheetName": {
"__rl": true,
"mode": "list",
"value": "gid=0",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/19ACXNwlTDB73obmvSNJB3sA06ADF2myJGmAeiSa3NN8/edit#gid=0",
"cachedResultName": "Sheet1"
},
"documentId": {
"__rl": true,
"mode": "list",
"value": "19ACXNwlTDB73obmvSNJB3sA06ADF2myJGmAeiSa3NN8",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/19ACXNwlTDB73obmvSNJB3sA06ADF2myJGmAeiSa3NN8/edit?usp=drivesdk",
"cachedResultName": "n8n_test_ocr"
}
},
"credentials": {
"googleSheetsOAuth2Api": {
"id": "vowsrhMIxy2PRDbH",
"name": "Google Sheets account"
}
},
"typeVersion": 4.3
},
{
"id": "7c3a1afa-a3ce-454f-bb6e-481e45267f25",
"name": "Get already processed rows from Sheets",
"type": "n8n-nodes-base.googleSheets",
"position": [
1040,
200
],
"parameters": {
"options": {},
"sheetName": {
"__rl": true,
"mode": "list",
"value": "gid=0",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/19ACXNwlTDB73obmvSNJB3sA06ADF2myJGmAeiSa3NN8/edit#gid=0",
"cachedResultName": "Sheet1"
},
"documentId": {
"__rl": true,
"mode": "list",
"value": "19ACXNwlTDB73obmvSNJB3sA06ADF2myJGmAeiSa3NN8",
"cachedResultUrl": "https://docs.google.com/spreadsheets/d/19ACXNwlTDB73obmvSNJB3sA06ADF2myJGmAeiSa3NN8/edit?usp=drivesdk",
"cachedResultName": "n8n_test_ocr"
}
},
"credentials": {
"googleSheetsOAuth2Api": {
"id": "vowsrhMIxy2PRDbH",
"name": "Google Sheets account"
}
},
"executeOnce": true,
"typeVersion": 4.3,
"alwaysOutputData": true
}
],
"active": true,
"pinData": {},
"settings": {
"executionOrder": "v1"
},
"versionId": "085fcb5e-3972-4670-9e04-3fc1c7d722e2",
"connections": {
"OCR recognize": {
"main": [
[
{
"node": "Unserialize response JSON",
"type": "main",
"index": 0
}
]
]
},
"Download file for OCR": {
"main": [
[
{
"node": "OCR recognize",
"type": "main",
"index": 0
}
]
]
},
"Filter processed files": {
"main": [
[
{
"node": "Download file for OCR",
"type": "main",
"index": 0
}
]
]
},
"Unserialize response JSON": {
"main": [
[
{
"node": "Save OCR result into Sheets",
"type": "main",
"index": 0
}
]
]
},
"On new file in Google Drive": {
"main": [
[
{
"node": "Get already processed rows from Sheets",
"type": "main",
"index": 0
},
{
"node": "Load files from Google Drive folder",
"type": "main",
"index": 0
}
]
]
},
"When clicking ‘Test workflow’": {
"main": [
[
{
"node": "Load files from Google Drive folder",
"type": "main",
"index": 0
},
{
"node": "Get already processed rows from Sheets",
"type": "main",
"index": 0
}
]
]
},
"Load files from Google Drive folder": {
"main": [
[
{
"node": "Filter processed files",
"type": "main",
"index": 1
}
]
]
},
"Get already processed rows from Sheets": {
"main": [
[
{
"node": "Filter processed files",
"type": "main",
"index": 0
}
]
]
}
}
}
功能特点
- 自动检测新邮件
- AI智能内容分析
- 自定义分类规则
- 批量处理能力
- 详细的处理日志
技术分析
节点类型及作用
- Manualtrigger
- Stickynote
- Httprequest
- Code
- Googledrivetrigger
复杂度评估
配置难度:
维护难度:
扩展性:
实施指南
前置条件
- 有效的Gmail账户
- n8n平台访问权限
- Google API凭证
- AI分类服务订阅
配置步骤
- 在n8n中导入工作流JSON文件
- 配置Gmail节点的认证信息
- 设置AI分类器的API密钥
- 自定义分类规则和标签映射
- 测试工作流执行
- 配置定时触发器(可选)
关键参数
| 参数名称 | 默认值 | 说明 |
|---|---|---|
| maxEmails | 50 | 单次处理的最大邮件数量 |
| confidenceThreshold | 0.8 | 分类置信度阈值 |
| autoLabel | true | 是否自动添加标签 |
最佳实践
优化建议
- 定期更新AI分类模型以提高准确性
- 根据邮件量调整处理批次大小
- 设置合理的分类置信度阈值
- 定期清理过期的分类规则
安全注意事项
- 妥善保管API密钥和认证信息
- 限制工作流的访问权限
- 定期审查处理日志
- 启用双因素认证保护Gmail账户
性能优化
- 使用增量处理减少重复工作
- 缓存频繁访问的数据
- 并行处理多个邮件分类任务
- 监控系统资源使用情况
故障排除
常见问题
邮件未被正确分类
检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。
Gmail认证失败
确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。
调试技巧
- 启用详细日志记录查看每个步骤的执行情况
- 使用测试邮件验证分类逻辑
- 检查网络连接和API服务状态
- 逐步执行工作流定位问题节点
错误处理
工作流包含以下错误处理机制:
- 网络超时自动重试(最多3次)
- API错误记录和告警
- 处理失败邮件的隔离机制
- 异常情况下的回滚操作