-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculate_filesize.js
56 lines (48 loc) · 1.86 KB
/
calculate_filesize.js
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
let settings = input.config({
title: "Calculate Attachment File Sizes",
description: `This script calculates the total size of attachments in a specified field and stores the result in another field.`,
items: [
input.config.table("table", { label: "Select the table" }),
input.config.view("view", {
parentTable: "table",
label: "Select the view"
}),
input.config.field("attachmentField", {
parentTable: "table",
label: "Select the attachment field",
description: "Field containing the attachments"
}),
input.config.field("fileSizeField", {
parentTable: "table",
label: "Select the file size field",
description: "Field to store the total file size"
}),
]
});
async function calculateAttachmentFileSizes() {
let { table, view, attachmentField, fileSizeField } = settings;
// Check if the fileSizeField is a number field
if (fileSizeField.type !== "number") {
output.text(
`${fileSizeField.name} is not a number field.\nRun the script again when you have a number field.`
);
return;
}
let records = await view.selectRecordsAsync({ fields: [attachmentField, fileSizeField] });
let updates = [];
for (let record of records.records) {
let attachments = record.getCellValue(attachmentField) || [];
let totalSize = attachments.reduce((acc, attachment) => acc + attachment.size, 0);
updates.push({
id: record.id,
fields: {
[fileSizeField.id]: totalSize
}
});
}
for (let i = 0; i < updates.length; i += 50) {
await table.updateRecordsAsync(updates.slice(i, i + 50));
}
output.text('Filesize calculation completed.');
}
await calculateAttachmentFileSizes();