loading...
Cover image for Apps Script + REST APIs + Google Sheets + Glide Apps

Apps Script + REST APIs + Google Sheets + Glide Apps

harishkotra profile image Harish Kotra (he/him) Originally published at log.harishkotra.me ・2 min read

View Demo

We will do 2 steps:
  • Create an Apps Script to fetch Top Movies and Popular Movies using the TMDb API.
  • Use Glide Apps and convert the Google Sheet into an app.
Things you need:

Google Sheets

Create a blank Google sheet and navigate to Tools -> Script Editor.

We have two functions i.e., getTopMovies(), getPopularMovies() and onOpen().

getTopMovies() – Fetch top movies from the API
getPopularMovies() – Fetch popular movies from the API
onOpen() – Create a custom menu to run REST API calls only when needed.

Paste the following code into the Script Editor and Save. Make sure you update API_KEY parameter with the actual key from TMDb API.

function getPopularMovies() {

  var response = UrlFetchApp.fetch("https://api.themoviedb.org/3/movie/popular?api_key=API_KEY&language=en-US&page=1");
  var values = [];
  var json = response.getContentText();
  var data = JSON.parse(json);
  var results = data["results"];

  results.forEach(function(item) {
    values.push([item["title"], item["overview"], "https://image.tmdb.org/t/p/w500/" + item["poster_path"], item["vote_average"], item["release_date"]]);
  });
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('Popular Movies');
  sheet.getRange(sheet.getLastRow()+1, 1, values.length, values[0].length).setValues(values);
}

function getTopMovies() {

  var response = UrlFetchApp.fetch("https://api.themoviedb.org/3/movie/top_rated?api_key=API_KEY&language=en-US&page=1");
  var values = [];
  var json = response.getContentText();
  var data = JSON.parse(json);
  var results = data["results"];

  results.forEach(function(item) {
    values.push([item["title"], item["overview"], "https://image.tmdb.org/t/p/w500/" + item["poster_path"], item["vote_average"], item["release_date"]]);
  });
  var spreadsheet = SpreadsheetApp.getActive();
  var sheet = spreadsheet.getSheetByName('Top Movies');
  sheet.getRange(sheet.getLastRow()+1, 1, values.length, values[0].length).setValues(values);
}

//menu items
function onOpen() {
  var ui = SpreadsheetApp.getUi();
  ui.createMenu('Movies DB Functions')
      .addItem('Get Top Movies','getTopMovies')
      .addItem('Get Popular Movies','getPopularMovies')
      .addToUi();
}

Posted on by:

harishkotra profile

Harish Kotra (he/him)

@harishkotra

I do/build: Chatbots | Hackathons | WordPress | Developer Events | Web & Mobile Apps

Discussion

pic
Editor guide
 

This is great! I followed the steps and was able to import the data easily. Have you tried using apipheny.io to import API data into Google Sheets?

 

No. I see you are one the founders! Will give it a shot this week! Also, thanks for trying this out :)