Advertisement
Guest User

ServiceError:Spreadsheet

a guest
Apr 18th, 2014
808
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function insertItemRow (result, rowIndex, sheet) {
  2.  
  3.   // Inserting Item Image
  4.   var imgUrl = result.img;
  5.  
  6.   // Inserts image, if any were found
  7.   if (imgUrl != "" && typeof imgUrl != "undefined") {
  8.     sheet.insertImage(result.image, 1, rowIndex)
  9.   }
  10.  
  11.   // Item Name
  12.   sheet.getRange(rowIndex, 2).setValue(result.name);
  13.  
  14.   // Item Rarity
  15.   sheet.getRange(rowIndex, 3).setValue(result.rarity);
  16.  
  17.   // Item Last Price Change
  18.   sheet.getRange(rowIndex, 4).setValue(result.price_last_changed);
  19.  
  20.   // Item Maximum Offered Price
  21.   sheet.getRange(rowIndex, 5).setValue(result.max_offer_unit_price);
  22.  
  23.   // Item Minimum Sale Price
  24.   sheet.getRange(rowIndex, 6).setValue(result.min_sale_unit_price);
  25.  
  26.   // Item Offer Availability
  27.   sheet.getRange(rowIndex, 7).setValue(result.offer_availability);
  28.  
  29.   // Item Sale Availability
  30.   sheet.getRange(rowIndex, 8).setValue(result.sale_availability);
  31. }
  32.  
  33. /* Updates the labels with the initial status of the progress
  34. *  including the number of items and pages found into the API request
  35. */
  36. function updateStartStatus (methodName){
  37.  
  38.   // Reaching Active Sheet
  39.   var sheet = SpreadsheetApp.getActiveSheet();
  40.  
  41.   // Log Range
  42.   var logRange = sheet.getRange("F2");
  43.  
  44.   // Logging Start
  45.   Logger.log("Started: " + methodName);
  46.   logRange.setValue("Started: " + methodName);
  47.  
  48.   // Updating "Status" Label
  49.   var sheetRange = sheet.getRange("A2");
  50.   sheetRange.setValue("Running");
  51.  
  52.   // Green Color to indicate the status of the script
  53.   sheetRange.setBackgroundRGB(0,102,51);
  54.  
  55.   // Executing Request for the first page, to update the spreadsheet labels with the total ammounts
  56.   var jsonResult = getItemsByPage ("1");
  57.  
  58.   // "Current Page" Range
  59.   sheetRange = sheet.getRange("B2");
  60.   sheetRange.setValue(1);
  61.  
  62.   // "Pages Found" Range
  63.   sheetRange = sheet.getRange("C2");
  64.   sheetRange.setValue(jsonResult.last_page);
  65.  
  66.   // "Total Itens Found" Range
  67.   sheetRange = sheet.getRange("E2");
  68.   sheetRange.setValue(jsonResult.total);
  69.  
  70.   // Logging Status
  71.   Logger.log("Total Pages:" + jsonResult.last_page);
  72.   Logger.log("Total Items:" + jsonResult.total);
  73.   logRange.setValue("Total Items:" + jsonResult.total);
  74.  
  75.   // Returning the number of pages
  76.   return jsonResult.last_page;
  77. }
  78.  
  79. /**
  80.  * Retrieves all the rows in the active spreadsheet that contain data and logs the
  81.  * values for each row.
  82.  * For more information on using the Spreadsheet API, see
  83.  * https://developers.google.com/apps-script/service_spreadsheet
  84.  */
  85. function listAllItems() {
  86.  
  87.   // Reaching Active Sheet
  88.   var sheet = SpreadsheetApp.getActiveSheet();
  89.  
  90.   // Updates the Labels with the starting status
  91.   var totalPages = updateStartStatus("List All Items");
  92.  
  93.   // Starting Row
  94.   var currentRow = 5;
  95.  
  96.   // Log Range
  97.   var logRange = sheet.getRange("F2");
  98.  
  99.   // Iterating over pages, making API Calls
  100.   for (var currentPage = 1; currentPage <= totalPages; currentPage++) {
  101.    
  102.     logRange.setValue("Getting Itens of Page:" + currentPage);
  103.    
  104.     // Executing API Call using Paging parameter
  105.     var jsonResult = getItemsByPage (currentPage);
  106.    
  107.     logRange.setValue("got itens of page:" + currentPage);
  108.    
  109.     sheet.getRange("F2").setValue(currentPage);
  110.    
  111.     // Updating Progress
  112.     logRange.setValue("Received Page:" + currentPage);
  113.    
  114.     // Iterating over results of this page
  115.     for (var resultIndex = 0 ; resultIndex <= jsonResult.results.length ; resultIndex++) {
  116.      
  117.       // Logging Progress
  118.       Logger.log ("Page:" + currentPage + " - Item:" + resultIndex);
  119.       logRange.setValue("Page:" + currentPage + " - Item:" + resultIndex);
  120.      
  121.       // Adding new Item to Sheet
  122.       insertItemRow (jsonResult.results[resultIndex], currentRow, sheet);      
  123.     }
  124.   }
  125. };
  126.  
  127. /*
  128.  * Event: OnOpen (Native Google Docs Event)
  129.  * This method is fired once the Sheet is openned (And everytime you refresh the page/reopen the sheet)
  130.  *
  131.  * This method will render a Custom Menu on the Sheet exposing the methods implemented here in a easy way
  132.  */
  133. function onOpen() {
  134.   var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  135.   var menu = [{
  136.     name : "List All",
  137.     functionName : "listAllItems"
  138.   }];
  139.   spreadsheet.addMenu("Guild Wars 2 Tracker", menu);
  140.  
  141.   spreadsheet.getRange ("A2").setValue("Stopped");
  142.   spreadsheet.getRange ("A2").setBackgroundRGB(255,0,0);
  143.  
  144. };
  145.  
  146. /* GW2 SPIDY METHODS */
  147.  
  148. /*
  149.  * Guild Wars 2 Spidy Functions
  150.  *
  151.  */
  152.  
  153. // URL BUILDERS - HELPS BUILDING URLS FOR EITHER RETRIEVING A SINGLE ITEM OR PAGING THROUGH ALL THE ITEMS
  154.  
  155. // API Url Builder - Single Item Get
  156. function urlBuilderSingleItem (itemID,format,version){
  157.  
  158.   // Checking undefined version
  159.   if (typeof version == "undefined"){
  160.     version = "v0.9";
  161.   }
  162.  
  163.   // Checking undefined format
  164.   if (typeof format == "undefined"){
  165.     format = "json";
  166.   }
  167.  
  168.   // Building URL
  169.   var url = "http://www.gw2spidy.com/api/" + escape(version) + "/" + escape(format) + "/item/" + escape(itemID);
  170.  
  171.   return url;
  172. }
  173.  
  174. // API Url Builder - All Items Get (With Paging)
  175. function urlBuilderAllItemsPaging (page,format,version){
  176.  
  177.   // Checking undefined version
  178.   if (typeof version == "undefined"){
  179.     version = "v0.9";
  180.   }
  181.  
  182.   // Checking undefined format
  183.   if (typeof format == "undefined"){
  184.     format = "json";
  185.   }
  186.  
  187.   // Building URL
  188.   var url = "http://www.gw2spidy.com/api/" + escape(version) + "/" + escape(format) + "/items/all/" + escape(page);
  189.  
  190.   return url;
  191. }
  192.  
  193.  
  194. // Http Request for Getting Data (Uses the URL Builders from this script)
  195.  
  196. // Gets an item data from GW2 API
  197. function getItemData (itemID){
  198.  
  199.   // Building API Url for the received item
  200.   var apiURL = urlBuilderSingleItem (itemID);
  201.  
  202.   // Executing Request for API
  203.   var requestResponse = UrlFetchApp.fetch(apiURL);
  204.  
  205.   // Reading JSON from Response
  206.   var jsonData = requestResponse.getContentText();
  207.  
  208.   return JSON.parse(jsonData);
  209. }
  210.  
  211.  
  212. // Gets all items from a certain page based on the "Paging" parameter of the API
  213. function getItemsByPage (page){
  214.  
  215.   var sheet = SpreadsheetApp.getActiveSheet();
  216.  
  217.   var logRange = sheet.getRange("F2");
  218.   logRange.setValue("building url");
  219.  
  220.   // Building API Url for the received item
  221.   var apiURL = urlBuilderAllItemsPaging (page);
  222.  
  223.   logRange.setValue("built url");
  224.  
  225.   // Executing Request for API
  226.   var requestResponse = UrlFetchApp.fetch(apiURL);
  227.  
  228.   logRange.setValue("fetched url");
  229.  
  230.   // Reading JSON from Response
  231.   var jsonData = requestResponse.getContentText();
  232.  
  233.   logRange.setValue("last log");
  234.  
  235.   return JSON.parse(jsonData);
  236.  
  237. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement