-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheetFiles.gs
100 lines (89 loc) · 3.54 KB
/
sheetFiles.gs
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
function createMainSheet(data, spreadsheet) {
var sheet_name = "files";
var sheet = spreadsheet.getSheetByName(sheet_name);
if (sheet == null) {
var sheet = spreadsheet.insertSheet(sheet_name);
}
var header_row = [
"Identifier",
"Publisher",
"Title",
"Valid", // "valid",
"Acceptable license", // "acceptable_license",
"License",
"Issued",
"Days since issued",
"Last modified",
"File size", // "file_size",
"File type", // "file_type",
"Grants", // "count",
"Grant amount (GBP)",
"Unique recipients",
"Funders",
"Earliest grant",
"Latest grant",
"Access URL",
"Download URL",
];
sheet.clear();
var filter = sheet.getFilter();
if (filter) {
filter.remove();
}
sheet.appendRow(header_row);
sheet.setFrozenRows(1);
sheet.setFrozenColumns(3);
var all_data = []
for (var i = 0; i < data.length; i++) {
var metadata = data[i]["datagetter_metadata"] || {};
var aggregates = data[i]["datagetter_aggregates"] || {};
if (!aggregates["currencies"]) { aggregates["currencies"] = {}; }
if (!aggregates["currencies"]["GBP"]) { aggregates["currencies"]["GBP"] = { "count": 0, "total_amount": 0 }; }
var min_date = Date.parse(aggregates["min_award_date"]) || null;
if (min_date) { min_date = new Date(min_date); }
var max_date = Date.parse(aggregates["max_award_date"]) || null;
if (max_date) { max_date = new Date(max_date); }
var datetime_downloaded = Date.parse(metadata["datetime_downloaded"]) || null;
if (datetime_downloaded) { datetime_downloaded = new Date(datetime_downloaded); }
var last_modified = Date.parse(data[i]["modified"]) || null;
if (last_modified) { last_modified = new Date(last_modified); }
var issued = Date.parse(data[i]["issued"]) || null;
if (issued) { issued = new Date(issued); }
var row_data = [
data[i]["identifier"],
hyperlink(data[i]["publisher"]["website"], data[i]["publisher"]["name"]),
data[i]["title"],
metadata["valid"] || null,
metadata["acceptable_license"] || null,
hyperlink(data[i]["license"], data[i]["license_name"]),
issued,
"=NOW()-G" + (i + 2),
last_modified,
metadata["file_size"] || null,
metadata["file_type"] || null,
aggregates["count"] || null,
aggregates["currencies"]["GBP"]["total_amount"] || null,
aggregates["distinct_recipient_org_identifier_count"] || null,
aggregates["distinct_funding_org_identifier_count"] || null,
min_date,
max_date,
data[i]["distribution"][0]["accessURL"],
data[i]["distribution"][0]["downloadURL"],
]
all_data.push(row_data)
}
writeMultipleRows(sheet, all_data)
// format date columns
var date_columns = [7, 9, 16, 17];
for (var i = 0; i < date_columns.length; i++) {
sheet.getRange(2, date_columns[i], sheet.getLastRow() - 1).setNumberFormat('yyyy"-"mm"-"dd');
}
// format number columns
var num_columns = [8, 10, 12, 13, 14, 15];
for (var i = 0; i < num_columns.length; i++) {
sheet.getRange(2, num_columns[i], sheet.getLastRow() - 1).setNumberFormat('#,##0');
}
// set up filter
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
range.createFilter();
}