-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
241 lines (203 loc) · 5.92 KB
/
Code.gs
File metadata and controls
241 lines (203 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// === CONFIGURE THIS PART ===
const PARSERDATA_API_KEY = 'pd_live_XXXXXXXX'; // <-- put your Parserdata API key here
const FOLDER_ID = 'YOUR_FOLDER_ID_HERE'; // <-- Google Drive folder with invoice PDFs
// Parserdata invoice schema
const INVOICE_SCHEMA = {
invoice_number: "string",
invoice_date: "date",
supplier_name: "string",
total_amount: "number",
line_items: "table"
};
/**
* Main entry: processes ONLY NEW PDF files in the Drive folder
* and appends data into a single "Flattened" sheet
* (one row per line item, invoice fields repeated).
*/
function processNewInvoicesFlattened() {
const folder = DriveApp.getFolderById(FOLDER_ID);
const files = folder.getFiles();
const ss = SpreadsheetApp.getActive();
const sheet = getOrCreateSheet_(ss, 'Flattened');
// Create header once
if (sheet.getLastRow() === 0) {
sheet.appendRow([
'file_name',
'invoice_number',
'invoice_date',
'supplier_name',
'total_amount',
'description',
'quantity',
'unit',
'unit_price'
]);
}
const processedIds = loadProcessedIds_(); // { fileId: true, ... }
const newIds = [];
const rows = [];
let processedCount = 0;
while (files.hasNext()) {
const file = files.next();
const fileId = file.getId();
// Only PDFs, and skip already processed files
if (file.getMimeType() !== MimeType.PDF) continue;
if (processedIds[fileId]) continue;
const raw = callParserdataWithFile_(file);
if (!raw) continue; // error already logged
// Deep-search important fields anywhere in the JSON
const fileNameFromApi =
findKeyDeep_(raw, 'fileName') ||
findKeyDeep_(raw, 'file_name');
const fileName = fileNameFromApi || file.getName();
const invoiceNumber = findKeyDeep_(raw, 'invoice_number') || '';
const invoiceDate = findKeyDeep_(raw, 'invoice_date') || '';
const supplierName = findKeyDeep_(raw, 'supplier_name') || '';
const totalAmount = findKeyDeep_(raw, 'total_amount') || '';
const lineItems = findKeyDeep_(raw, 'line_items') || [];
if (!Array.isArray(lineItems) || lineItems.length === 0) {
// still write one row with just invoice-level data
rows.push([
fileName,
invoiceNumber,
invoiceDate,
supplierName,
totalAmount,
'',
'',
'',
''
]);
} else {
lineItems.forEach(item => {
rows.push([
fileName,
invoiceNumber,
invoiceDate,
supplierName,
totalAmount,
item.description ?? '',
item.quantity ?? '',
item.unit ?? '',
item.unit_price ?? ''
]);
});
}
newIds.push(fileId);
processedCount++;
Utilities.sleep(500); // small pause if many files
}
if (rows.length > 0) {
sheet
.getRange(sheet.getLastRow() + 1, 1, rows.length, rows[0].length)
.setValues(rows);
}
if (newIds.length > 0) {
saveProcessedIds_(processedIds, newIds);
}
SpreadsheetApp.getActive().toast(
'Processed ' + processedCount + ' new PDF file(s) into Flattened sheet.'
);
}
/**
* Calls Parserdata /v1/extract sending the actual PDF file.
* Returns the raw parsed JSON (whatever shape it has).
*/
function callParserdataWithFile_(file) {
const apiUrl = 'https://api.parserdata.com/v1/extract';
const blob = file.getBlob(); // correct MIME type (application/pdf)
const formData = {
file: blob,
schema: JSON.stringify(INVOICE_SCHEMA)
};
const options = {
method: 'post',
payload: formData,
headers: {
'X-API-Key': PARSERDATA_API_KEY
},
muteHttpExceptions: true
};
const response = UrlFetchApp.fetch(apiUrl, options);
const status = response.getResponseCode();
const bodyText = response.getContentText();
Logger.log('Status for "%s": %s', file.getName(), status);
if (status !== 200) {
console.error(
'Parserdata API error for file ' + file.getName() + ': ' +
status + ' - ' + bodyText
);
return null;
}
try {
const raw = JSON.parse(bodyText);
Logger.log(
'Top-level keys for "%s": %s',
file.getName(),
Object.keys(raw || {}).join(', ')
);
return raw;
} catch (e) {
console.error('Failed to parse JSON for file ' + file.getName() + ': ' + e);
return null;
}
}
/**
* Recursively search an object/array for a key and return its value.
* Returns the first match found (depth-first).
*/
function findKeyDeep_(obj, targetKey) {
if (obj === null || obj === undefined) return null;
if (Array.isArray(obj)) {
for (let i = 0; i < obj.length; i++) {
const res = findKeyDeep_(obj[i], targetKey);
if (res !== null && res !== undefined) return res;
}
return null;
}
if (typeof obj === 'object') {
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue;
if (key === targetKey) {
return obj[key];
}
const res = findKeyDeep_(obj[key], targetKey);
if (res !== null && res !== undefined) return res;
}
}
return null;
}
/**
* Store / load processed file IDs in ScriptProperties
* so we don’t process the same PDF twice.
*/
function loadProcessedIds_() {
const props = PropertiesService.getScriptProperties();
const json = props.getProperty('processedFileIds');
if (!json) return {};
try {
return JSON.parse(json); // { fileId: true, ... }
} catch (e) {
Logger.log('Failed to parse processedFileIds, resetting: ' + e);
return {};
}
}
function saveProcessedIds_(current, newIds) {
newIds.forEach(id => {
current[id] = true;
});
PropertiesService.getScriptProperties().setProperty(
'processedFileIds',
JSON.stringify(current)
);
}
/**
* Utility: get sheet by name or create it if missing.
*/
function getOrCreateSheet_(ss, name) {
let sheet = ss.getSheetByName(name);
if (!sheet) {
sheet = ss.insertSheet(name);
}
return sheet;
}