Advertisement
Guest User

newscript name it 'ImportJSON.gs'

a guest
Mar 22nd, 2017
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Retrieves all the rows in the active spreadsheet that contain data and logs the
  3.  * values for each row.
  4.  * For more information on using the Spreadsheet API, see
  5.  * https://developers.google.com/apps-script/service_spreadsheet
  6.  */
  7. function readRows() {
  8.   var sheet = SpreadsheetApp.getActiveSheet();
  9.   var rows = sheet.getDataRange();
  10.   var numRows = rows.getNumRows();
  11.   var values = rows.getValues();
  12.  
  13.   for (var i = 0; i <= numRows - 1; i++) {
  14.     var row = values[i];
  15.     Logger.log(row);
  16.   }
  17. };
  18.  
  19. /**
  20.  * Adds a custom menu to the active spreadsheet, containing a single menu item
  21.  * for invoking the readRows() function specified above.
  22.  * The onOpen() function, when defined, is automatically invoked whenever the
  23.  * spreadsheet is opened.
  24.  * For more information on using the Spreadsheet API, see
  25.  * https://developers.google.com/apps-script/service_spreadsheet
  26.  */
  27. function onOOpen() {
  28.   var sheet = SpreadsheetApp.getActiveSpreadsheet();
  29.   var entries = [{
  30.     name : "Read Data",
  31.     functionName : "readRows"
  32.   }];
  33.   sheet.addMenu("Click", entries);
  34. };
  35. /*====================================================================================================================================*
  36.   ImportJSON by Trevor Lohrbeer (@FastFedora)
  37.   ====================================================================================================================================
  38.   Version:      1.1
  39.   Project Page: http://blog.fastfedora.com/projects/import-json
  40.   Copyright:    (c) 2012 by Trevor Lohrbeer
  41.   License:      GNU General Public License, version 3 (GPL-3.0)
  42.                 http://www.opensource.org/licenses/gpl-3.0.html
  43.   ------------------------------------------------------------------------------------------------------------------------------------
  44.   A library for importing JSON feeds into Google spreadsheets. Functions include:
  45.      ImportJSON            For use by end users to import a JSON feed from a URL
  46.      ImportJSONAdvanced    For use by script developers to easily extend the functionality of this library
  47.   Future enhancements may include:
  48.    - Support for a real XPath like syntax similar to ImportXML for the query parameter
  49.    - Support for OAuth authenticated APIs
  50.   Or feel free to write these and add on to the library yourself!
  51.   ------------------------------------------------------------------------------------------------------------------------------------
  52.   Changelog:
  53.  
  54.   1.1    Added support for the noHeaders option
  55.   1.0    Initial release
  56.  *====================================================================================================================================*/
  57. /**
  58.  * Imports a JSON feed and returns the results to be inserted into a Google Spreadsheet. The JSON feed is flattened to create
  59.  * a two-dimensional array. The first row contains the headers, with each column header indicating the path to that data in
  60.  * the JSON feed. The remaining rows contain the data.
  61.  *
  62.  * By default, data gets transformed so it looks more like a normal data import. Specifically:
  63.  *
  64.  *   - Data from parent JSON elements gets inherited to their child elements, so rows representing child elements contain the values
  65.  *      of the rows representing their parent elements.
  66.  *   - Values longer than 256 characters get truncated.
  67.  *   - Headers have slashes converted to spaces, common prefixes removed and the resulting text converted to title case.
  68.  *
  69.  * To change this behavior, pass in one of these values in the options parameter:
  70.  *
  71.  *    noInherit:     Don't inherit values from parent elements
  72.  *    noTruncate:    Don't truncate values
  73.  *    rawHeaders:    Don't prettify headers
  74.  *    noHeaders:     Don't include headers, only the data
  75.  *    debugLocation: Prepend each value with the row & column it belongs in
  76.  *
  77.  * For example:
  78.  *
  79.  *   =ImportJSON("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json", "/feed/entry/title,/feed/entry/content",
  80.  *               "noInherit,noTruncate,rawHeaders")
  81.  *
  82.  * @param {url} the URL to a public JSON feed
  83.  * @param {query} a comma-separated lists of paths to import. Any path starting with one of these paths gets imported.
  84.  * @param {options} a comma-separated list of options that alter processing of the data
  85.  *
  86.  * @return a two-dimensional array containing the data, with the first row containing headers
  87.  * @customfunction
  88.  **/
  89. function ImportJSON(url, query, options) {
  90.   return ImportJSONAdvanced(url, query, options, includeXPath_, defaultTransform_);
  91. }
  92.  
  93. /**
  94.  * An advanced version of ImportJSON designed to be easily extended by a script. This version cannot be called from within a
  95.  * spreadsheet.
  96.  *
  97.  * Imports a JSON feed and returns the results to be inserted into a Google Spreadsheet. The JSON feed is flattened to create
  98.  * a two-dimensional array. The first row contains the headers, with each column header indicating the path to that data in
  99.  * the JSON feed. The remaining rows contain the data.
  100.  *
  101.  * Use the include and transformation functions to determine what to include in the import and how to transform the data after it is
  102.  * imported.
  103.  *
  104.  * For example:
  105.  *
  106.  *   =ImportJSON("http://gdata.youtube.com/feeds/api/standardfeeds/most_popular?v=2&alt=json",
  107.  *               "/feed/entry",
  108.  *                function (query, path) { return path.indexOf(query) == 0; },
  109.  *                function (data, row, column) { data[row][column] = data[row][column].toString().substr(0, 100); } )
  110.  *
  111.  * In this example, the import function checks to see if the path to the data being imported starts with the query. The transform
  112.  * function takes the data and truncates it. For more robust versions of these functions, see the internal code of this library.
  113.  *
  114.  * @param {url}           the URL to a public JSON feed
  115.  * @param {query}         the query passed to the include function
  116.  * @param {options}       a comma-separated list of options that may alter processing of the data
  117.  * @param {includeFunc}   a function with the signature func(query, path, options) that returns true if the data element at the given path
  118.  *                        should be included or false otherwise.
  119.  * @param {transformFunc} a function with the signature func(data, row, column, options) where data is a 2-dimensional array of the data
  120.  *                        and row & column are the current row and column being processed. Any return value is ignored. Note that row 0
  121.  *                        contains the headers for the data, so test for row==0 to process headers only.
  122.  *
  123.  * @return a two-dimensional array containing the data, with the first row containing headers
  124.  **/
  125. function ImportJSONAdvanced(url, query, options, includeFunc, transformFunc) {
  126.   var jsondata = UrlFetchApp.fetch(url);
  127.   var object   = JSON.parse(jsondata.getContentText());
  128.  
  129.   return parseJSONObject_(object, query, options, includeFunc, transformFunc);
  130. }
  131.  
  132. /**
  133.  * Encodes the given value to use within a URL.
  134.  *
  135.  * @param {value} the value to be encoded
  136.  *
  137.  * @return the value encoded using URL percent-encoding
  138.  */
  139. function URLEncode(value) {
  140.   return encodeURIComponent(value.toString());  
  141. }
  142.  
  143. /**
  144.  * Parses a JSON object and returns a two-dimensional array containing the data of that object.
  145.  */
  146. function parseJSONObject_(object, query, options, includeFunc, transformFunc) {
  147.   var headers = new Array();
  148.   var data    = new Array();
  149.  
  150.   if (query && !Array.isArray(query) && query.toString().indexOf(",") != -1) {
  151.     query = query.toString().split(",");
  152.   }
  153.  
  154.   if (options) {
  155.     options = options.toString().split(",");
  156.   }
  157.    
  158.   parseData_(headers, data, "", 1, object, query, options, includeFunc);
  159.   parseHeaders_(headers, data);
  160.   transformData_(data, options, transformFunc);
  161.  
  162.   return hasOption_(options, "noHeaders") ? (data.length > 1 ? data.slice(1) : new Array()) : data;
  163. }
  164.  
  165. /**
  166.  * Parses the data contained within the given value and inserts it into the data two-dimensional array starting at the rowIndex.
  167.  * If the data is to be inserted into a new column, a new header is added to the headers array. The value can be an object,
  168.  * array or scalar value.
  169.  *
  170.  * If the value is an object, it's properties are iterated through and passed back into this function with the name of each
  171.  * property extending the path. For instance, if the object contains the property "entry" and the path passed in was "/feed",
  172.  * this function is called with the value of the entry property and the path "/feed/entry".
  173.  *
  174.  * If the value is an array containing other arrays or objects, each element in the array is passed into this function with
  175.  * the rowIndex incremeneted for each element.
  176.  *
  177.  * If the value is an array containing only scalar values, those values are joined together and inserted into the data array as
  178.  * a single value.
  179.  *
  180.  * If the value is a scalar, the value is inserted directly into the data array.
  181.  */
  182. function parseData_(headers, data, path, rowIndex, value, query, options, includeFunc) {
  183.   var dataInserted = false;
  184.  
  185.   if (isObject_(value)) {
  186.     for (key in value) {
  187.       if (parseData_(headers, data, path + "/" + key, rowIndex, value[key], query, options, includeFunc)) {
  188.         dataInserted = true;
  189.       }
  190.     }
  191.   } else if (Array.isArray(value) && isObjectArray_(value)) {
  192.     for (var i = 0; i < value.length; i++) {
  193.       if (parseData_(headers, data, path, rowIndex, value[i], query, options, includeFunc)) {
  194.         dataInserted = true;
  195.         rowIndex++;
  196.       }
  197.     }
  198.   } else if (!includeFunc || includeFunc(query, path, options)) {
  199.     // Handle arrays containing only scalar values
  200.     if (Array.isArray(value)) {
  201.       value = value.join();
  202.     }
  203.    
  204.     // Insert new row if one doesn't already exist
  205.     if (!data[rowIndex]) {
  206.       data[rowIndex] = new Array();
  207.     }
  208.    
  209.     // Add a new header if one doesn't exist
  210.     if (!headers[path] && headers[path] != 0) {
  211.       headers[path] = Object.keys(headers).length;
  212.     }
  213.    
  214.     // Insert the data
  215.     data[rowIndex][headers[path]] = value;
  216.     dataInserted = true;
  217.   }
  218.  
  219.   return dataInserted;
  220. }
  221.  
  222. /**
  223.  * Parses the headers array and inserts it into the first row of the data array.
  224.  */
  225. function parseHeaders_(headers, data) {
  226.   data[0] = new Array();
  227.  
  228.   for (key in headers) {
  229.     data[0][headers[key]] = key;
  230.   }
  231. }
  232.  
  233. /**
  234.  * Applies the transform function for each element in the data array, going through each column of each row.
  235.  */
  236. function transformData_(data, options, transformFunc) {
  237.   for (var i = 0; i < data.length; i++) {
  238.     for (var j = 0; j < data[i].length; j++) {
  239.       transformFunc(data, i, j, options);
  240.     }
  241.   }
  242. }
  243.  
  244. /**
  245.  * Returns true if the given test value is an object; false otherwise.
  246.  */
  247. function isObject_(test) {
  248.   return Object.prototype.toString.call(test) === '[object Object]';
  249. }
  250.  
  251. /**
  252.  * Returns true if the given test value is an array containing at least one object; false otherwise.
  253.  */
  254. function isObjectArray_(test) {
  255.   for (var i = 0; i < test.length; i++) {
  256.     if (isObject_(test[i])) {
  257.       return true;
  258.     }
  259.   }  
  260.  
  261.   return false;
  262. }
  263.  
  264. /**
  265.  * Returns true if the given query applies to the given path.
  266.  */
  267. function includeXPath_(query, path, options) {
  268.   if (!query) {
  269.     return true;
  270.   } else if (Array.isArray(query)) {
  271.     for (var i = 0; i < query.length; i++) {
  272.       if (applyXPathRule_(query[i], path, options)) {
  273.         return true;
  274.       }
  275.     }  
  276.   } else {
  277.     return applyXPathRule_(query, path, options);
  278.   }
  279.  
  280.   return false;
  281. };
  282.  
  283. /**
  284.  * Returns true if the rule applies to the given path.
  285.  */
  286. function applyXPathRule_(rule, path, options) {
  287.   return path.indexOf(rule) == 0;
  288. }
  289.  
  290. /**
  291.  * By default, this function transforms the value at the given row & column so it looks more like a normal data import. Specifically:
  292.  *
  293.  *   - Data from parent JSON elements gets inherited to their child elements, so rows representing child elements contain the values
  294.  *     of the rows representing their parent elements.
  295.  *   - Values longer than 256 characters get truncated.
  296.  *   - Values in row 0 (headers) have slashes converted to spaces, common prefixes removed and the resulting text converted to title
  297. *      case.
  298.  *
  299.  * To change this behavior, pass in one of these values in the options parameter:
  300.  *
  301.  *    noInherit:     Don't inherit values from parent elements
  302.  *    noTruncate:    Don't truncate values
  303.  *    rawHeaders:    Don't prettify headers
  304.  *    debugLocation: Prepend each value with the row & column it belongs in
  305.  */
  306. function defaultTransform_(data, row, column, options) {
  307.   if (!data[row][column]) {
  308.     if (row < 2 || hasOption_(options, "noInherit")) {
  309.       data[row][column] = "";
  310.     } else {
  311.       data[row][column] = data[row-1][column];
  312.     }
  313.   }
  314.  
  315.   if (!hasOption_(options, "rawHeaders") && row == 0) {
  316.     if (column == 0 && data[row].length > 1) {
  317.       removeCommonPrefixes_(data, row);  
  318.     }
  319.    
  320.     data[row][column] = toTitleCase_(data[row][column].toString().replace(/[\/\_]/g, " "));
  321.   }
  322.  
  323.   if (!hasOption_(options, "noTruncate") && data[row][column]) {
  324.     data[row][column] = data[row][column].toString().substr(0, 256);
  325.   }
  326.  
  327.   if (hasOption_(options, "debugLocation")) {
  328.     data[row][column] = "[" + row + "," + column + "]" + data[row][column];
  329.   }
  330. }
  331.  
  332. /**
  333.  * If all the values in the given row share the same prefix, remove that prefix.
  334.  */
  335. function removeCommonPrefixes_(data, row) {
  336.   var matchIndex = data[row][0].length;
  337.  
  338.   for (var i = 1; i < data[row].length; i++) {
  339.     matchIndex = findEqualityEndpoint_(data[row][i-1], data[row][i], matchIndex);
  340.  
  341.     if (matchIndex == 0) {
  342.       return;
  343.     }
  344.   }
  345.  
  346.   for (var i = 0; i < data[row].length; i++) {
  347.     data[row][i] = data[row][i].substring(matchIndex, data[row][i].length);
  348.   }
  349. }
  350.  
  351. /**
  352.  * Locates the index where the two strings values stop being equal, stopping automatically at the stopAt index.
  353.  */
  354. function findEqualityEndpoint_(string1, string2, stopAt) {
  355.   if (!string1 || !string2) {
  356.     return -1;
  357.   }
  358.  
  359.   var maxEndpoint = Math.min(stopAt, string1.length, string2.length);
  360.  
  361.   for (var i = 0; i < maxEndpoint; i++) {
  362.     if (string1.charAt(i) != string2.charAt(i)) {
  363.       return i;
  364.     }
  365.   }
  366.  
  367.   return maxEndpoint;
  368. }
  369.  
  370.  
  371. /**
  372.  * Converts the text to title case.
  373.  */
  374. function toTitleCase_(text) {
  375.   if (text == null) {
  376.     return null;
  377.   }
  378.  
  379.   return text.replace(/\w\S*/g, function(word) { return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase(); });
  380. }
  381.  
  382. /**
  383.  * Returns true if the given set of options contains the given option.
  384.  */
  385. function hasOption_(options, option) {
  386.   return options && options.indexOf(option) >= 0;
  387. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement