From 04a414ebed06ac50d150d08a305fb089d26d0670 Mon Sep 17 00:00:00 2001 From: h4dr0 Date: Sun, 9 Feb 2020 21:47:46 +0000 Subject: [PATCH 1/3] Update ImportJSON.gs parse nulls fix + parse numbers as numeric --- ImportJSON.gs | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/ImportJSON.gs b/ImportJSON.gs index 731806e..9f108a6 100644 --- a/ImportJSON.gs +++ b/ImportJSON.gs @@ -1,7 +1,7 @@ /*====================================================================================================================================* ImportJSON by Brad Jasper and Trevor Lohrbeer ==================================================================================================================================== - Version: 1.5.0 + Version: 1.5.0 + parse nulls fix + parse numbers as numeric Project Page: https://github.com/bradjasper/ImportJSON Copyright: (c) 2017-2019 by Brad Jasper (c) 2012-2017 by Trevor Lohrbeer @@ -48,12 +48,13 @@ * * To change this behavior, pass in one of these values in the options parameter: * - * noInherit: Don't inherit values from parent elements - * noTruncate: Don't truncate values - * rawHeaders: Don't prettify headers - * noHeaders: Don't include headers, only the data - * allHeaders: Include all headers from the query parameter in the order they are listed - * debugLocation: Prepend each value with the row & column it belongs in + * noInherit: Don't inherit values from parent elements + * noTruncate: Don't truncate values + * rawHeaders: Don't prettify headers + * noHeaders: Don't include headers, only the data + * allHeaders: Include all headers from the query parameter in the order they are listed + * debugLocation: Prepend each value with the row & column it belongs in + * noParseNumbers: Don't parse numbers (keep as text) * * For example: * @@ -434,7 +435,7 @@ function transformData_(data, options, transformFunc) { * Returns true if the given test value is an object; false otherwise. */ function isObject_(test) { - return Object.prototype.toString.call(test) === '[object Object]'; + return typeof(test) == 'object' && test !== null; } /** @@ -493,7 +494,7 @@ function applyXPathRule_(rule, path, options) { * debugLocation: Prepend each value with the row & column it belongs in */ function defaultTransform_(data, row, column, options) { - if (data[row][column] == null) { + if (data[row][column] === undefined) { if (row < 2 || hasOption_(options, "noInherit")) { data[row][column] = ""; } else { @@ -509,15 +510,29 @@ function defaultTransform_(data, row, column, options) { data[row][column] = toTitleCase_(data[row][column].toString().replace(/[\/\_]/g, " ")); } + if (!hasOption_(options, "noParseNumbers")) { + var num = filterFloat(data[row][column]); + if (!isNaN(num)) { + data[row][column] = num; + } + } + if (!hasOption_(options, "noTruncate") && data[row][column]) { data[row][column] = data[row][column].toString().substr(0, 256); } - + if (hasOption_(options, "debugLocation")) { data[row][column] = "[" + row + "," + column + "]" + data[row][column]; } } +function filterFloat(value) { + if(/^(-|\+)?([0-9]+(.[0-9]+)?|Infinity)$/.test(value)) { + return Number(value); + } + return NaN; +} + /** * If all the values in the given row share the same prefix, remove that prefix. */ @@ -632,3 +647,7 @@ function getDataFromNamedSheet_(sheetName) { Logger.log(jsonText); return JSON.parse(jsonText); } + +function test() { + ImportJSONBasicAuth("https://phenomsports.myshopify.com/admin/api/2019-07/products.json?limit=5", "db33b6765ccf3d13e80fbbafc3194149","2da80ad0bce18eeec69a8d45aa683db9", "/products/template_suffix,/products/id,/products/product_type,/products/title,/products/variants/id,/products/variants/sku,/products/variants/price,/products/variants/compare_at_price"); +} From f6bf3c0e899a19f46b7e58bb50d9873c0db95488 Mon Sep 17 00:00:00 2001 From: h4dr0 Date: Sun, 9 Feb 2020 22:52:52 +0000 Subject: [PATCH 2/3] Update ImportJSON.gs parse stringified numbers as numeric + don't substring truncate numbers (and thus keep numeric) --- ImportJSON.gs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ImportJSON.gs b/ImportJSON.gs index 9f108a6..7fb6c19 100644 --- a/ImportJSON.gs +++ b/ImportJSON.gs @@ -1,7 +1,7 @@ /*====================================================================================================================================* ImportJSON by Brad Jasper and Trevor Lohrbeer ==================================================================================================================================== - Version: 1.5.0 + parse nulls fix + parse numbers as numeric + Version: 1.5.0 + parse nulls fix + parse stringified numbers as numeric + don't truncate numbers (and thus keep numeric) Project Page: https://github.com/bradjasper/ImportJSON Copyright: (c) 2017-2019 by Brad Jasper (c) 2012-2017 by Trevor Lohrbeer @@ -510,14 +510,14 @@ function defaultTransform_(data, row, column, options) { data[row][column] = toTitleCase_(data[row][column].toString().replace(/[\/\_]/g, " ")); } - if (!hasOption_(options, "noParseNumbers")) { + if (!hasOption_(options, "noParseNumbers") && typeof(data[row][column]) != 'number') { var num = filterFloat(data[row][column]); if (!isNaN(num)) { data[row][column] = num; } } - if (!hasOption_(options, "noTruncate") && data[row][column]) { + if (!hasOption_(options, "noTruncate") && typeof(data[row][column]) != 'number' && data[row][column]) { data[row][column] = data[row][column].toString().substr(0, 256); } From 1b9f157c6ba8fb39f8411f19652259ae4b027588 Mon Sep 17 00:00:00 2001 From: h4dr0 Date: Mon, 5 Oct 2020 23:54:59 +0100 Subject: [PATCH 3/3] Update ImportJSON.gs --- ImportJSON.gs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/ImportJSON.gs b/ImportJSON.gs index 7fb6c19..42a03cb 100644 --- a/ImportJSON.gs +++ b/ImportJSON.gs @@ -647,7 +647,3 @@ function getDataFromNamedSheet_(sheetName) { Logger.log(jsonText); return JSON.parse(jsonText); } - -function test() { - ImportJSONBasicAuth("https://phenomsports.myshopify.com/admin/api/2019-07/products.json?limit=5", "db33b6765ccf3d13e80fbbafc3194149","2da80ad0bce18eeec69a8d45aa683db9", "/products/template_suffix,/products/id,/products/product_type,/products/title,/products/variants/id,/products/variants/sku,/products/variants/price,/products/variants/compare_at_price"); -}