Last active
October 4, 2023 15:17
-
-
Save therebelrobot/69183e37db57ae9aea5d5c7925bf2c3e to your computer and use it in GitHub Desktop.
Google Sheets - AppScript - Get value from JSON cell
This file contains hidden or 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
// This is the function to use in the cell | |
// e.g. =getJSONvalue(A2,"members.0.id") | |
// note: make sure you use double quotes! | |
function getJSONvalue(cell, path) { | |
try { | |
let content | |
try { | |
content = JSON.parse(cell); | |
} catch (e) { | |
return `Not JSON: ${cell}` | |
} | |
if (content) { | |
return getValue(content, path) || `no results found at path: ${path}` | |
} | |
return "no content parsed."; | |
} catch( e) { | |
return `Error returned in processing: ${JSON.stringify(e)}` | |
} | |
} | |
// DEPENDENCY: get-value npm package - source: https://github.com/jonschlinkert/get-value/blob/master/index.js | |
/*! | |
* get-value <https://github.com/jonschlinkert/get-value> | |
* | |
* Copyright (c) 2014-2018, Jon Schlinkert. | |
* Released under the MIT License. | |
* (edits made to function in Apps Scripts: named primary export) | |
*/ | |
function getValue (target, path, options) { | |
if (!isObject(options)) { | |
options = { default: options }; | |
} | |
if (!isValidObject(target)) { | |
return typeof options.default !== 'undefined' ? options.default : target; | |
} | |
if (typeof path === 'number') { | |
path = String(path); | |
} | |
const isArray = Array.isArray(path); | |
const isString = typeof path === 'string'; | |
const splitChar = options.separator || '.'; | |
const joinChar = options.joinChar || (typeof splitChar === 'string' ? splitChar : '.'); | |
if (!isString && !isArray) { | |
return target; | |
} | |
if (isString && path in target) { | |
return isValid(path, target, options) ? target[path] : options.default; | |
} | |
let segs = isArray ? path : split(path, splitChar, options); | |
let len = segs.length; | |
let idx = 0; | |
do { | |
let prop = segs[idx]; | |
if (typeof prop === 'number') { | |
prop = String(prop); | |
} | |
while (prop && prop.slice(-1) === '\\') { | |
prop = join([prop.slice(0, -1), segs[++idx] || ''], joinChar, options); | |
} | |
if (prop in target) { | |
if (!isValid(prop, target, options)) { | |
return options.default; | |
} | |
target = target[prop]; | |
} else { | |
let hasProp = false; | |
let n = idx + 1; | |
while (n < len) { | |
prop = join([prop, segs[n++]], joinChar, options); | |
if ((hasProp = prop in target)) { | |
if (!isValid(prop, target, options)) { | |
return options.default; | |
} | |
target = target[prop]; | |
idx = n - 1; | |
break; | |
} | |
} | |
if (!hasProp) { | |
return options.default; | |
} | |
} | |
} while (++idx < len && isValidObject(target)); | |
if (idx === len) { | |
return target; | |
} | |
return options.default; | |
}; | |
function join(segs, joinChar, options) { | |
if (typeof options.join === 'function') { | |
return options.join(segs); | |
} | |
return segs[0] + joinChar + segs[1]; | |
} | |
function split(path, splitChar, options) { | |
if (typeof options.split === 'function') { | |
return options.split(path); | |
} | |
return path.split(splitChar); | |
} | |
function isValid(key, target, options) { | |
if (typeof options.isValid === 'function') { | |
return options.isValid(key, target); | |
} | |
return true; | |
} | |
function isValidObject(val) { | |
return isObject(val) || Array.isArray(val) || typeof val === 'function'; | |
} | |
// END DEPENDENCY: get-value | |
// DEPENDENCY: isobject npm package | |
/*! | |
* isobject <https://github.com/jonschlinkert/isobject> | |
* | |
* Copyright (c) 2014-2017, Jon Schlinkert. | |
* Released under the MIT License. | |
* (edits made to function in Apps Scripts: named primary export) | |
*/ | |
function isObject(val) { | |
return val != null && typeof val === 'object' && Array.isArray(val) === false; | |
}; | |
// END DEPENDENCY: isobject |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment