Created
April 18, 2020 20:23
-
-
Save gmcdev/25db72b90fa1d041734177056d9b4151 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* | |
* Proxy Data Export | |
*/ | |
const assignObjToScopeByParams = (obj, scopes, matchParamsByScopes, depth) => { | |
// create clean nested-scope objects for parameters that are objects | |
const prepareNestScopes = () => { | |
return scopes.map(() => { | |
return {} | |
}) | |
} | |
// apply nested-scope objects back to master scopes | |
const applyNestedScopes = (key, nestedScopes) => { | |
for (let s = 0; s <= depth; s++) { | |
scopes[s][key] = nestedScopes[s] | |
} | |
} | |
// apply non-object values up to the nest-depth of this recursion | |
const updateScopes = (key, value) => { | |
for (let s = 0; s <= depth; s++) { | |
scopes[s][key] = value | |
} | |
} | |
const matchParams = matchParamsByScopes[depth] | |
Object.keys(obj).map(param => { | |
if (!matchParams || matchParams.includes(param)) { | |
if (typeof obj[param] === 'object') { | |
const nestDepth = depth + 1 | |
applyNestedScopes(param, assignObjToScopeByParams(obj[param], prepareNestScopes(), matchParamsByScopes, nestDepth)) | |
} else { | |
updateScopes(param, obj[param]) | |
} | |
} | |
}) | |
log(`..finished a recursion depth ${depth}:`) | |
log(scopes) | |
log('\n\n\n') | |
return scopes | |
} | |
const exportAsJson = async ({ spreadsheetId, sheetIds, rowIds, columns, assetIds, assets }) => { | |
log(`Exporting sheet-data as filtered JSON`) | |
// get all sheet-ids of the required spreadsheet-id | |
const sheets = await sheetsApi.getSheets({ spreadsheetId }) | |
let ssObj = { [spreadsheetId]: {} }, | |
sidsObj = {}, | |
ridsObj = {}, | |
colsObj = {}, | |
aidsObj = {}, | |
assetsObj = {} | |
const scopes = [ssObj[spreadsheetId], sidsObj, ridsObj, colsObj, aidsObj, assetsObj] | |
const matchParamsPerScopes = [[spreadsheetId], sheetIds.map(sid => sid.toString()), rowIds, columns, assetIds, assets] | |
const ss = { | |
[spreadsheetId]: {} | |
} | |
await Promise.all( | |
// Each SHEET: | |
sheets.map(async sheet => { | |
const sheetId = sheet.properties.sheetId | |
// ssObj[spreadsheetId][sheetId] = {} | |
// sidsObj[sheetId] = {} | |
// if no sheet-ids are requested, or this sheet-id is requested | |
if (!sheetIds || sheetIds.includes(sheetId)) { | |
// try to load the proxied JSON for this sheet-id | |
let rows | |
try { | |
rows = await json.load({ spreadsheetId, sheetId }) | |
} catch (err) { | |
return log(` ~no proxied data exists for sheet-id ${sheetId}`) | |
} | |
ss[spreadsheetId] = { | |
[sheetId]: rows | |
} | |
} | |
}) | |
) | |
const hydratedScopes = assignObjToScopeByParams(ss, scopes, matchParamsPerScopes, 0) | |
log(hydratedScopes) | |
let result | |
matchParamsPerScopes.find((mp, i) => { | |
if (!mp || !mp.length) { | |
result = hydratedScopes[i - 1] | |
return true | |
} | |
}) | |
return result | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
An attempt with a simplified scenario: