How to loop pagination in Pipedrive to get all deals? - google-apps-script

I have more than 5000 deals in Pipedrive and I want to pull all the deals using Pipedrive integration to google sheet.
I tried some script and successfully pull some data but the problem is Pipedrive has only 500 max limit in each page.
So would like to ask how will I able to loop the pagination and to get all the deals.
please see the script below.
function GetPdriveSalesToday() {
var ss = SpreadsheetApp.openById('xxxxxxxxxxxxxxxxxxxx');
var sheet = ss.getSheetByName("Sheet6");
var lastrow = sheet.getLastRow();
sheet.getRange("Sheet6!A2:C3500").clearContent();
var url = "https://api.pipedrive.com/v1/deals?filter_id=2699&start=1&limit=500&api_token=xxxxxxxxxxxxxxxxxxxx";
var response = UrlFetchApp.fetch(url);
var dataSet = JSON.parse(response.getContentText());
var data;
for (var i = 0; i < dataSet.data.length; i++) {
data = dataSet.data[i];
sheet.appendRow([data.id,data.title,data.e2c4a2838c16e53c6f4cf3b54ac5bfe253310a7a]).getRange(lastrow +1,1);
}
}

They gave an official functional example function about retrieving all records using pagination data which given as additional data once you made your request, have a good at this:
https://pipedrive.readme.io/docs/using-pagination-to-retrieve-all-deal-titles

Related

API to Google sheets - unable to fetch data

I am trying to fetch data via API to Google sheets, I am able to get "NewConfirmed" and other few fields but not able to get "Countries" data. Please help.
function Covid19() {
// Call the COVID19 API
var response = UrlFetchApp.fetch("https://api.covid19api.com/summary");
// Parse the JSON reply
var json=response.getContentText();
var data=JSON.parse(json);
var sheet = SpreadsheetApp.getActiveSheet();
var i = 2;
for each (var info in data)
{
sheet.getRange(i,1).setValue([info['NewConfirmed']]);
sheet.getRange(i,2).setValue([info['Country']]);
i = i + 1;
}
If you log data, you will see
{Countries=[{CountryCode=AX, TotalRecovered=0.0, NewDeaths=0.0,
Slug=ala-aland-islands, Country=ALA Aland Islands, NewRecovered=0.0,
Date=2020-04-21T12:32:01Z, NewConfirmed=0.0, ...
Thus, in order to retrieve Country and NewConfirmed you need to define
var data=JSON.parse(json).Countries and then you have to iterate through all entries within a loop.
Sample based on your code:
function Covid19() {
var response = UrlFetchApp.fetch("https://api.covid19api.com/summary");
var json=response.getContentText();
var data=JSON.parse(json).Countries;
var sheet = SpreadsheetApp.getActiveSheet();
for(var i = 0; i < data.length; i++)
{
sheet.getRange(i+2,1).setValue([data[i]['NewConfirmed']]);
sheet.getRange(i+2,2).setValue([data[i]['Country']]);
}
}
Sidenote:
It is ot good practive to use getRange(...).setValue(..) during each
loop iteration. It would be better to write the data into an array and
assign the array with all the data to the sheet after finishing
iteration.

Google Sheets / YouTube Data API: Read channel ID list and output subscriber count and view count

I'm certain this is a dumb question but please excuse the complete noob here!
I'm trying to use the YouTube Data API with Google sheets to output the total subscriber/view counts for a long list of YouTube channel ID's in column T. I've gotten the following to work with a single cell (T2), but am uncertain how this would work with the list T2:T outputting to H2:I.
I'm guessing I should be reading up on how to handle the array...
function getChannelInfo() {
var ss = SpreadsheetApp.getActiveSheet();
var channel = ss.getRange("T2").getValue();
var data = YouTube.Channels.list('statistics', {id: channel});
var item = data.items[0];
var info = [item.statistics.viewCount, item.statistics.subscriberCount];
ss.getRange("H2:I2").setValues([info]);
}
Thanks in advance!
Simple for loop should do this for you.
Here's the snippet,
function getChannelInfo() {
var ss = SpreadsheetApp.getActiveSheet();
for(var i=2; i<ss.getLastRow(); i++)
{
var channel = ss.getRange("T"+[i]).getValue();
var data = YouTube.Channels.list('statistics', {id: channel});
var item = data.items[0];
var info = [item.statistics.viewCount, item.statistics.subscriberCount];
ss.getRange("H"+i+":I"+i).setValues([info]);
}
}
I used the upper Loop but when subscriber count is hidden then it will show value 0 and loop stops working. Use This code it will work even if sub count is hidden.
function getChannelInfo() {
var ss = SpreadsheetApp.getActiveSheet();
var i = 2;
do{ i++;
var channel = ss.getRange("C"+[i]).getValue();
var data = YouTube.Channels.list('statistics', {id: channel});
var item = data.items[0];
var info = [item.statistics.subscriberCount];
ss.getRange("D"+i).setValues([info]);
} while(i<ss.getLastRow())
}

Google Script, Run functions in sequence without exceeding execution time

I have a lot of functions that fetch JSON API data from a website, but if I run them in sequence in this way, I get the exceeding execution time error:
function fetchdata () {
data1();
data2();
data3();
data4();
...
}
I can schedule a trigger to run them at 5 minutes one of the other (cause a single one runs in 3 minutes), but I would like to know if there is any other way around. Thank you
EDIT:
Every "data" function is like this one:
function data1() {
var addresses = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import");
var baseUrl = 'https://myapiurl';
var address = addresses.getRange(2, 1, 500).getValues();
for(var i=0;i<address.length;i++){
var addrID = address[i][0];
var url = baseUrl.concat(addrID);
var responseAPI = UrlFetchApp.fetch(url);
var json = JSON.parse(responseAPI.getContentText());
var data = [[json.result]];
var dataRange = addresses.getRange(i+2, 2).setValue(data);
}
}
data2 is for rows 502-1001,
data3 is for rows 1002-1501,
and so on...
I just removed the concat because it has performance issues according to MDN but obviously the real problem is the fetch and there's not much we can do about that unless you can get your external api to dump a bigger batch.
You could initiate each function from a webapp and then have it return via withSuccessHandler and then start the next script in the series and daisy chain your way through all of the subfunctions until your done. Each sub function will take it's 3 minutes or so but you only have to worry about keeping each sub function under 6 minutes that way.
function data1()
{
var addresses = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Import");
var baseUrl = 'https://myapiurl';
var address = addresses.getRange(2, 1, 500).getValues();
for(var i=0;i<address.length;i++){
var responseAPI = UrlFetchApp.fetch(baseUrl + address[i][0]);
var json = JSON.parse(responseAPI.getContentText());
var data = [[json.result]];
var dataRange = addresses.getRange(i+2, 2).setValue(data);
}
}

How to get data from API with GAS

First, I'm not a non-native, so there're some mistakes.And I'm a newbie.
function myFunction() {
var response = UrlFetchApp.fetch("https://api.ookami.me/v1/news/public?sport_id=1");
var json = JSON.parse(response.getContentText());
var news = JSON.parse(json.getContentText());
Logger.log("id");
Logger.log("url");
Logger.log("image");
}
CODE
I wrote "news" in line8's code, and that time, displayed the log. but wrote again, didn't display.
And the log displayed with id, url, image and so on, so I added the code on line6 and wrote the code on line8 to line10. But a log displayed "undefined".
I want to get data about id, url, image from this API with GAS.
And the data, I'll export to spread sheet.
You're almost there:
At this line:
var json = JSON.parse(response.getContentText());
You have all the data in this format:
I you want the element news, you need to change your line var news = JSON.parse(json.getContentText()); to var news = json.news;
Then you can loop through all the values:
for(var i = 0; i < news.length; i++) {
var obj = news[i];
Logger.log(obj.summary);
}
To export to spreadsheet, you just need to populate an Array inside the loop, and it should look like this:
var rows = []; // new values
for(var i = 0; i < news.length; i++) {
var obj = news[i];
rows.push([obj.id, obj.image,obj.url]); //your JSON entities here
}
Logger.log(rows);

Error in Google Sheets Script when parsing XML

I have this function running in a Google Sheets script that pulls HTML from subreddits and returns them to a spreadsheet. It works for me some/most of the time, but other times I get an error "Could not parse text. (line 13)" which is the line with var doc = Xml.parse(page, true);. Any idea why this is happening or is this just a bug with Google Scripts? Here's the code that works...sometimes.
function getRedditHTML() {
var entries_array = [];
var subreddit_array = ['https://www.reddit.com/r/news/','https://www.reddit.com/r/funny/','https://www.reddit.com/r/science/'];
for (var s = 0; s < subreddit_array.length; s++) {
var page = UrlFetchApp.fetch(subreddit_array[s]);
//this is Line 13 that is breaking
var doc = Xml.parse(page, true);
var bodyHtml = doc.html.body.toXmlString();
doc = XmlService.parse(bodyHtml);
var root = doc.getRootElement();
var entries = getElementsByClassName(root,'thing');
for (var i = 0; i < entries.length; i++) {
var title = getElementsByClassName(entries[i],'title');
title = XmlService.getRawFormat().format(title[1]).replace(/<[^>]*>/g, "");
var link = getElementsByClassName(entries[i],'comments');
link = link[0].getAttribute('href').getValue();
var rank = getElementsByClassName(entries[i],'rank');
rank = rank[0].getValue();
var likes = getElementsByClassName(entries[i],'likes');
likes = likes[0].getValue();
entries_array.push([rank, likes, title, link]);
}
}
return entries_array.sort(function (a, b) {
return b[1] - a[1];
});
}
Here is what I found upon playing with importXML (my usual way of doing this) - for some reason I cannot narrow down - it DOES appear to randomly stall out and return null for a few minutes - so I'm guessing the issue with your thing is not the code but that the site or google temporarily blocks/won't return the data -
however I found the JSON endpoint to the piece you want - and I noticed that when XML went down - the JSON didnt.
You can take that and fix it to push your own array of topics/urls - I just left it for one link for now to show you how the URL breaks down and where it should be modified:
The URL is 'https://www.reddit.com/r/news/hot.json?raw_json=1&subredditName=news&sort=top&t=day&feature=link_preview&sr_detail=true&app=mweb-client
News is mentioned in 2 places so just modify all your URLs to follow that method - you can easily load that javascript in a browser to see all the fields available
Also the portion hot.json is where you can change whether you want the ranked list (called hot), or new,top,promoted, etc. you just change that keyword.
Score is the same as the upvotes/likes
function getSubReddit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet(); //get Active sheet
var subject = 'news';
var url = 'https://www.reddit.com/r/' + subject + '/hot.json?raw_json=1&subredditName=' + subject + '&sort=top&t=day&feature=link_preview&sr_detail=true&app=mweb-client'; //json endpoint for data
var response = UrlFetchApp.fetch(url); // get api endpoint
var json = response.getContentText(); // get the response content as text
var redditData = JSON.parse(json); //parse text into json
Logger.log(redditData); //log data to logger to check
//create empty array to hold data points
var statsRows = [];
var date = new Date(); //create new date for timestamp
//The following lines push the parsed json into empty stats array
for (var j=0;j<25;j++){
for (var i =0;i<25;i++){
var stats=[];
stats.push(date);//timestamp
stats.push(i+1);
stats.push(redditData.data.children[i].data.score); //score
stats.push(redditData.data.children[i].data.title); //title
stats.push(redditData.data.children[i].data.url); //article url
// stats.push('http://www.reddit.com' + redditData.data.children[i].data.permalink); //reddit permalink
statsRows.push(stats)
}
//append the stats array to the active sheet
sheet.appendRow(statsRows[j])
}
}