Bunny
Bunny

Reputation: 323

Zoho Books to Google Sheets API

I'm trying to pull purchase order information into Google Sheets from Zoho Books through a Google apps script.

I'm new to apps script so I've been piecing together some code but its not returning anything, any ideas?

function Zoho() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //get active spreadsheet (bound to this script)
var sheet = ss.getSheetByName('Sheet1'); //The name of the sheet tab where you are sending the info


  var zohoOauthToken = "mytoken";
  var zohoOrganization = "myorg";

 var zohoUrl = [ 
    "https://books.zoho.com/api/v3/salesorders?",
    "organization_id=", zohoOrganization,
    "&authtoken=", zohoOauthToken,
  ].join("");

   try{   
var response = UrlFetchApp.fetch(zohoUrl); // get api endpoint
var json = response.getContentText(); // get the response content as text
var Sheet1 = JSON.parse(json); //parse text into json


Logger.log(Sheet1); //log data to logger

var stats=[]; //create empty array to hold data points

sheet.appendRow(stats);
}
catch (error) {
    Logger.log(error.toString());
  }
}

Upvotes: 2

Views: 491

Answers (1)

Terry A
Terry A

Reputation: 11

You've undoubtedly worked this out for yourself by now, but for others looking for an answer ....

    zUrl = 'https://books.zoho.com/api/v3/';
    zToken = '?authtoken=##############################';
    zOrgId = '&organization_id=#########';

    function getPurchaseorderById(iD) {
      //iD = "##############"
      var result = UrlFetchApp.fetch(zUrl + 'purchaseorders/' + iD + zToken);
      //var PO = parse(result).purchaseorder;
      return parse(result).purchaseorder;
    }


    function getOpenPurchaseOrders(){
      var result = UrlFetchApp.fetch(zUrl + 'purchaseorders/' + zToken + '&status=issued&sort_column=delivery_date&sort_order=A'); 
    //var PO = parse(result).purchaseorder;
      return parse(result).purchaseorders;
    }

    function getOpenPurchaseOrdersByDate(date){
      var date = toZohoDate(date);
      var result = UrlFetchApp.fetch(zUrl + 'purchaseorders/' + zToken + '&date=' + date); 
      //var POs = parse(result);
      return parse(result).purchaseorders;
    }

    function toZohoDate(date) {
        return Utilities.formatDate(new Date(date), "GMT", "yyyy-MM-dd");
    }

    function parse(x){
      return JSON.parse(x.getContentText());
    }

For different modules use....

    function getInvoiceByNumber(num){
      //num='#####';
      var result = UrlFetchApp.fetch(zUrl + 'invoices/' + zToken + "&search_text=" + num);
      //var inv = parse(result).invoices[0];
      return parse(result);
    }

Upvotes: 1

Related Questions