Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function insertItemRow (result, rowIndex, sheet) {
- // Inserting Item Image
- var imgUrl = result.img;
- // Inserts image, if any were found
- if (imgUrl != "" && typeof imgUrl != "undefined") {
- sheet.insertImage(result.image, 1, rowIndex)
- }
- // Item Name
- sheet.getRange(rowIndex, 2).setValue(result.name);
- // Item Rarity
- sheet.getRange(rowIndex, 3).setValue(result.rarity);
- // Item Last Price Change
- sheet.getRange(rowIndex, 4).setValue(result.price_last_changed);
- // Item Maximum Offered Price
- sheet.getRange(rowIndex, 5).setValue(result.max_offer_unit_price);
- // Item Minimum Sale Price
- sheet.getRange(rowIndex, 6).setValue(result.min_sale_unit_price);
- // Item Offer Availability
- sheet.getRange(rowIndex, 7).setValue(result.offer_availability);
- // Item Sale Availability
- sheet.getRange(rowIndex, 8).setValue(result.sale_availability);
- }
- /* Updates the labels with the initial status of the progress
- * including the number of items and pages found into the API request
- */
- function updateStartStatus (methodName){
- // Reaching Active Sheet
- var sheet = SpreadsheetApp.getActiveSheet();
- // Log Range
- var logRange = sheet.getRange("F2");
- // Logging Start
- Logger.log("Started: " + methodName);
- logRange.setValue("Started: " + methodName);
- // Updating "Status" Label
- var sheetRange = sheet.getRange("A2");
- sheetRange.setValue("Running");
- // Green Color to indicate the status of the script
- sheetRange.setBackgroundRGB(0,102,51);
- // Executing Request for the first page, to update the spreadsheet labels with the total ammounts
- var jsonResult = getItemsByPage ("1");
- // "Current Page" Range
- sheetRange = sheet.getRange("B2");
- sheetRange.setValue(1);
- // "Pages Found" Range
- sheetRange = sheet.getRange("C2");
- sheetRange.setValue(jsonResult.last_page);
- // "Total Itens Found" Range
- sheetRange = sheet.getRange("E2");
- sheetRange.setValue(jsonResult.total);
- // Logging Status
- Logger.log("Total Pages:" + jsonResult.last_page);
- Logger.log("Total Items:" + jsonResult.total);
- logRange.setValue("Total Items:" + jsonResult.total);
- // Returning the number of pages
- return jsonResult.last_page;
- }
- /**
- * Retrieves all the rows in the active spreadsheet that contain data and logs the
- * values for each row.
- * For more information on using the Spreadsheet API, see
- * https://developers.google.com/apps-script/service_spreadsheet
- */
- function listAllItems() {
- // Reaching Active Sheet
- var sheet = SpreadsheetApp.getActiveSheet();
- // Updates the Labels with the starting status
- var totalPages = updateStartStatus("List All Items");
- // Starting Row
- var currentRow = 5;
- // Log Range
- var logRange = sheet.getRange("F2");
- // Iterating over pages, making API Calls
- for (var currentPage = 1; currentPage <= totalPages; currentPage++) {
- logRange.setValue("Getting Itens of Page:" + currentPage);
- // Executing API Call using Paging parameter
- var jsonResult = getItemsByPage (currentPage);
- logRange.setValue("got itens of page:" + currentPage);
- sheet.getRange("F2").setValue(currentPage);
- // Updating Progress
- logRange.setValue("Received Page:" + currentPage);
- // Iterating over results of this page
- for (var resultIndex = 0 ; resultIndex <= jsonResult.results.length ; resultIndex++) {
- // Logging Progress
- Logger.log ("Page:" + currentPage + " - Item:" + resultIndex);
- logRange.setValue("Page:" + currentPage + " - Item:" + resultIndex);
- // Adding new Item to Sheet
- insertItemRow (jsonResult.results[resultIndex], currentRow, sheet);
- }
- }
- };
- /*
- * Event: OnOpen (Native Google Docs Event)
- * This method is fired once the Sheet is openned (And everytime you refresh the page/reopen the sheet)
- *
- * This method will render a Custom Menu on the Sheet exposing the methods implemented here in a easy way
- */
- function onOpen() {
- var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
- var menu = [{
- name : "List All",
- functionName : "listAllItems"
- }];
- spreadsheet.addMenu("Guild Wars 2 Tracker", menu);
- spreadsheet.getRange ("A2").setValue("Stopped");
- spreadsheet.getRange ("A2").setBackgroundRGB(255,0,0);
- };
- /* GW2 SPIDY METHODS */
- /*
- * Guild Wars 2 Spidy Functions
- *
- */
- // URL BUILDERS - HELPS BUILDING URLS FOR EITHER RETRIEVING A SINGLE ITEM OR PAGING THROUGH ALL THE ITEMS
- // API Url Builder - Single Item Get
- function urlBuilderSingleItem (itemID,format,version){
- // Checking undefined version
- if (typeof version == "undefined"){
- version = "v0.9";
- }
- // Checking undefined format
- if (typeof format == "undefined"){
- format = "json";
- }
- // Building URL
- var url = "http://www.gw2spidy.com/api/" + escape(version) + "/" + escape(format) + "/item/" + escape(itemID);
- return url;
- }
- // API Url Builder - All Items Get (With Paging)
- function urlBuilderAllItemsPaging (page,format,version){
- // Checking undefined version
- if (typeof version == "undefined"){
- version = "v0.9";
- }
- // Checking undefined format
- if (typeof format == "undefined"){
- format = "json";
- }
- // Building URL
- var url = "http://www.gw2spidy.com/api/" + escape(version) + "/" + escape(format) + "/items/all/" + escape(page);
- return url;
- }
- // Http Request for Getting Data (Uses the URL Builders from this script)
- // Gets an item data from GW2 API
- function getItemData (itemID){
- // Building API Url for the received item
- var apiURL = urlBuilderSingleItem (itemID);
- // Executing Request for API
- var requestResponse = UrlFetchApp.fetch(apiURL);
- // Reading JSON from Response
- var jsonData = requestResponse.getContentText();
- return JSON.parse(jsonData);
- }
- // Gets all items from a certain page based on the "Paging" parameter of the API
- function getItemsByPage (page){
- var sheet = SpreadsheetApp.getActiveSheet();
- var logRange = sheet.getRange("F2");
- logRange.setValue("building url");
- // Building API Url for the received item
- var apiURL = urlBuilderAllItemsPaging (page);
- logRange.setValue("built url");
- // Executing Request for API
- var requestResponse = UrlFetchApp.fetch(apiURL);
- logRange.setValue("fetched url");
- // Reading JSON from Response
- var jsonData = requestResponse.getContentText();
- logRange.setValue("last log");
- return JSON.parse(jsonData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement