Save Tables and Fields into Spreadsheet

Export all Fields and Tables

When you're working with API you'll often need to reference tables and all the field slugs in each table. You can use this little script to export all of these into a spreadsheet. 

get-fields-optimized.gif

 

Create a new Google Sheet and go to the App Scripts: 


Paste the following code and make sure to update the first 3 lines with your API details: 

/*************
* Variables
**************/
var Tadabase_App_Id = 'APPID';
var Tadabase_Api_Key = 'APIKEY';
var Tadabase_Api_Secret = 'APISECRET';

/*************
* API Settings
**************/
var Api_Url = 'https://api.tadabase.io/api/v1/data-tables';
var Request_Var = {
        'method' : 'GET',
        'headers': {
           "X-Tadabase-App-id" : Tadabase_App_Id,
           "X-Tadabase-App-Key" : Tadabase_Api_Key,
           "X-Tadabase-App-Secret" : Tadabase_Api_Secret
        }
    };

/*************
* Custom Menu
**************/
/* 
Function : onOpen
Descriptions : Add Custom Menu in Spreatsheet menus
*/
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Tadabase')
      .addItem('Get Tables and Fields', 'saveMenu')
      .addToUi();
}

function saveMenu() {
  SpreadsheetApp.getUi();
  var r = Browser.msgBox('Confirmation', 'Are you sure want Save Table Fields ?', Browser.Buttons.YES_NO);
  
  if(r == 'yes'){
    save();
  }
}


/* 
Function : getFields
Descriptions : Get All the Fields For Each Table
*/
function getFields(table_id) {
  var response = UrlFetchApp.fetch(Api_Url+"/"+table_id+"/full-fields-info", Request_Var);
  return JSON.parse(response);
}

/* 
Function : getFields
Descriptions : Get API DataTable
*/
function getDatatables() {
  var response = UrlFetchApp.fetch(Api_Url + "/", Request_Var);
  response = JSON.parse(response);
  if(response.type == "error"){
     SpreadsheetApp.getUi().alert(response.msg);
     return false;
  }
  return response.data_tables;
}

/* 
Function : Save
Descriptions : Save API Table Fields
*/
function save() {
  
  var data_tables = getDatatables();
  var activeSpreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var Sheet = activeSpreadsheet.getActiveSheet();
  
  Sheet.appendRow(['Field Name', 'Type', 'Slug', 'Table Name', 'Table ID']);
  for ( var i in data_tables) {
    
    var table = data_tables [ i ];
    var getfields = getFields(table.id);
    
    if(getfields.type == "success"){
      
      for ( var j in getfields.fields) {
      
         var field = getfields.fields[j];
        
         Sheet.appendRow([field.name, field.type, field.slug, table.name, table.id]);
    
      } // End for
  
    } // End if
  
  } // End for
  
}

Press Save, or Ctrl + s. 

Go back to your spreadsheet and refresh the page. After several seconds you will see a new menu option called "Tadabase" 

Press "Get Tables and Fields"

After confirming the selection  you will see all your tables and fields displayed on the page: