-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmenuRefreshDataDependencies.ts
308 lines (286 loc) · 11.2 KB
/
menuRefreshDataDependencies.ts
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import { getValidConceptDataFasttrackCatalogEntry } from "../gsheetsData/conceptData";
import { ConceptDataRow, ConceptDataWorksheetData } from "../lib/conceptData";
import { parseConceptIdAndCatalogReference } from "../lib/parseConceptIdAndCatalogReference";
import { validateAndAliasTheGeoSetArgument } from "../lib/validateAndAliasTheGeoSetArgument";
import { validateConceptIdArgument } from "../lib/validateConceptIdArgument";
import { getOpenNumbersConceptData } from "../openNumbersData/conceptData";
import {
adjustSheetRowsAndColumnsCount,
refreshDataCatalog,
writeStatus
} from "./dataDependenciesCommon";
/**
* Menu item action for "Gapminder Data -> Import/refresh data dependencies"
*
* Imports data sets from the fasttrack catalog to the current spreadsheet,
* allowing GM_-functions to reference local data instead of importing data on-the-fly.
*
* Details:
* - Creates the data-dependencies and data-catalog spreadsheets if they don't exist
* - Verifies that the first headers of the data-dependencies spreadsheet are as expected
* - Does not attempt to import data with bad catalog status
* - Communicates import status as the import progresses
*/
export function menuRefreshDataDependencies() {
const activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
const {
dataDependenciesSheet,
dataDependenciesWithHeaderRow,
fasttrackCatalogDataPointsWorksheetData,
openNumbersWorldDevelopmentIndicatorsDatasetConceptListing
} = refreshDataCatalog(activeSpreadsheet);
// Read current data dependencies
const dataDependencies = dataDependenciesWithHeaderRow.slice(1);
// Do not attempt to import if there are no data dependencies
if (dataDependencies.length === 0) {
SpreadsheetApp.getUi().alert(
"Please add your data dependencies to the 'data-dependencies' sheet, then run this again."
);
return;
}
// For each data dependency - copy the catalog data worksheet values to corresponding sheets in the current spreadsheet
dataDependencies.map((dataDependencyRow, index) => {
const concept_id_and_catalog_reference: string = String(
dataDependencyRow[0]
);
const time_unit = dataDependencyRow[1];
const geo_set = dataDependencyRow[2];
const dataStatus = dataDependencyRow[3];
// Skip empty rows
if (concept_id_and_catalog_reference === "") {
return;
}
const {
conceptId,
conceptVersion,
catalog
} = parseConceptIdAndCatalogReference(concept_id_and_catalog_reference);
let validatedGeoSetArgument;
try {
validateConceptIdArgument(conceptId);
validatedGeoSetArgument = validateAndAliasTheGeoSetArgument(geo_set);
} catch (err) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: "Not imported: " + err.message
});
return;
}
// Do not attempt to import bad datasets
if (dataStatus.toString().substr(0, 3) === "BAD") {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: "Not checked/imported since catalog status is marked as BAD"
});
return;
}
const destinationSheetName = `data:${concept_id_and_catalog_reference}:${time_unit}:${validatedGeoSetArgument}`;
// Read values to import
let importValues;
let importRangeRows;
let importRangeColumns;
switch (catalog) {
case undefined:
case "":
case "fasttrack":
{
const conceptDataFasttrackCatalogEntry = getValidConceptDataFasttrackCatalogEntry(
conceptId,
time_unit,
validatedGeoSetArgument,
fasttrackCatalogDataPointsWorksheetData
);
// Import sheet from source document
let sourceDoc = SpreadsheetApp.openById(
conceptDataFasttrackCatalogEntry.docId
);
// Fetch specific version
if (conceptVersion) {
const versionCellNamedRange = sourceDoc.getRangeByName("version");
if (!versionCellNamedRange) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported since there was no "version" named range in the source doc ("${conceptDataFasttrackCatalogEntry.docId}")`
});
return;
}
const currentVersion = versionCellNamedRange.getValue();
if (conceptVersion !== currentVersion) {
const versionsTableRange = sourceDoc.getRangeByName(
"version_table"
);
if (!versionsTableRange) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported since there was no "version_table" named range in the source doc ("${sourceDoc.getUrl()}")`
});
return;
}
const versionsTableValues = versionsTableRange.getValues() as string[][];
const matchingVersionTableRows = versionsTableValues.filter(
(versionsTableRow, i) => {
return versionsTableRow[0] === conceptVersion;
}
);
const matchingVersionTableRow = matchingVersionTableRows[0];
if (!matchingVersionTableRow) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported since the requested version ("${conceptVersion}") was not listed in the "version_table" named range in the source doc ("${sourceDoc.getUrl()}")`
});
return;
}
const link = matchingVersionTableRow[1];
if (!link || link.trim() === "" || link.trim() === "-") {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported since the requested version ("${conceptVersion}") entry had an empty "Link" value in the "version_table" named range in the source doc ("${sourceDoc.getUrl()}")`
});
return;
}
try {
sourceDoc = SpreadsheetApp.openByUrl(link);
} catch (e) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported since there was a problem with opening the requested version ("${conceptVersion}"). The "Link" value was "${link}" for this version in the "version_table" named range in the source doc ("${sourceDoc.getUrl()}") and the error message was ${e.toString()}`
});
return;
}
}
}
const sourceSheet = sourceDoc.getSheetByName(
conceptDataFasttrackCatalogEntry.worksheetReference.name
);
if (!sourceSheet) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported since the source sheet "${conceptDataFasttrackCatalogEntry.worksheetReference.name}" was not available`
});
return;
}
// Import the relevant concept data
const sourceDataRange = sourceSheet.getDataRange();
const sourceValues = sourceDataRange.getValues();
importValues = sourceValues.map(row => [
row[0], // geo
row[1], // name
row[2], // time
row[2 + conceptDataFasttrackCatalogEntry.indicatorOrder] // the concept data column to import
]);
importRangeRows = sourceDataRange.getNumRows();
importRangeColumns = 4;
}
break;
case "open-numbers-wdi":
case "open-numbers/ddf--open_numbers--world_development_indicators":
{
try {
const openNumbersConceptData: ConceptDataWorksheetData = getOpenNumbersConceptData(
conceptId,
time_unit,
geo_set,
openNumbersWorldDevelopmentIndicatorsDatasetConceptListing
);
importValues = [openNumbersConceptData.headers].concat(
openNumbersConceptData.rows.map(
(conceptDataRow: ConceptDataRow) => {
return [
conceptDataRow.geo,
conceptDataRow.name,
conceptDataRow.time,
conceptDataRow.value
];
}
)
);
importRangeRows = importValues.length;
importRangeColumns = 4;
} catch (e) {
if (e.name === "UrlFetchAppFetchException") {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported due to a failed request to open-numbers-wdi url ${
e.url
}. Full error message was ${e.toString()}`
});
return;
} else {
throw e;
}
}
}
break;
default: {
writeStatus(dataDependenciesSheet, index, {
importRangeRows: null,
lastChecked: null,
notes: `Not imported - unknown catalog: "${catalog}"`
});
return;
}
}
const lastChecked = new Date();
// Do not import empty data sets
if (importRangeRows <= 1) {
writeStatus(dataDependenciesSheet, index, {
importRangeRows,
lastChecked,
notes: `Not imported since no data rows to import were found`
});
return;
}
// Make sure that the destination sheet exists
const destination = activeSpreadsheet;
let destinationSheet = destination.getSheetByName(destinationSheetName);
if (!destinationSheet) {
destinationSheet = destination.insertSheet(
destinationSheetName,
destination.getNumSheets()
);
}
writeStatus(dataDependenciesSheet, index, {
importRangeRows,
lastChecked,
notes: "About to import..."
});
// Remove excess rows and columns in case the import data range is smaller than the previously imported data range and vice versa
// This prevents stale data from lingering around after the import
adjustSheetRowsAndColumnsCount(
destinationSheet,
importRangeRows,
importRangeColumns
);
writeStatus(dataDependenciesSheet, index, {
importRangeRows,
lastChecked,
notes:
"[Importing...] Adjusted destination sheet rows and columns to accommodate the new data"
});
// Write values to import
destinationSheet
.getRange(
1,
1,
destinationSheet.getMaxRows(),
destinationSheet.getMaxColumns()
)
.setValues(importValues);
writeStatus(dataDependenciesSheet, index, {
importRangeRows,
lastChecked,
notes: "Imported the data successfully"
});
});
SpreadsheetApp.getUi().alert("Imported/refreshed data dependencies.");
return;
}