Close

initial code

A project log for Tindie Unshipped Orders Sheet

Google Sheet with app script to pull out just the unshipped ones. For download and import into shipping software.

Alastair YoungAlastair Young 02/16/2019 at 05:560 Comments
/*
Pull unshipped orders from Tindie API
Because the website doesn't offer it, and Endicia will make duplicates

Copyright 2019 Alastair Young

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
  modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

function onOpen(e) {
  var sheet = SpreadsheetApp.getActiveSpreadsheet();
  sheet.getSheetByName("Data").clear();
  var url = 'https://www.tindie.com/api/v1/order/?format=json&shipped=false';
  var username = sheet.getRange("Settings!B1").getValue()
  var apikey = sheet.getRange("Settings!B2").getValue()
  var options = {
    'muteHttpExceptions': true,
    'headers' : { "Authorization":"ApiKey " + username + ":" + apikey }
  };
  var response = UrlFetchApp.fetch(url, options);
  var json = response.getContentText();
  try { 
    var data = JSON.parse(json);
  } catch(err) {
    sheet.getRange("A1").setValue("No unshipped orders"); 
    return;
  }
  
  
  var values = [
    [
      "number",
      "date",
      "email",
      "shipping_name",
      "company_title",
      "phone",
      "shipping_street",
      "shipping_street_2",
      "shipping_street_3",
      "shipping_city",
      "shipping_state",
      "shipping_postcode",
      "shipping_country",
      "shipping_service"    
    ]
  ];
  for(var row in data.orders) {
    var newRow = [];
    for (var key in values[0]) 
    {
      if (values[0][key] == "shipping_street"){
        var address = data.orders[row][values[0][key]].split("\n");
        for (var i=0; i<=2; i++) {
          if (address[i]) {
            newRow.push(address[i]);
          } else {
            newRow.push('');
          }
        }
      } else if (values[0][key].indexOf("shipping_street")>-1){
        continue;
      } else {
        newRow.push(data.orders[row][values[0][key]]); 
      }
    }
    values.push(newRow);
  }
  
  var result = sheet.getRange("A1:N" + values.length).setValues(values);
}

Discussions