App script, Connect a Google spreadsheet with an API

In this post, we will use App script to connect a google spreadsheet with TheMovieDB API and get a list of movies known for a given actor.

Today, we will discover together some of the basic app script possibilities. At the end, you will be able to extend your knowledge via the documentation provided in the sources section.

Upidev: Connect a Google spreadsheet to an API with App script

Create a TheMovieDB account

Create an API Key

Go to Settings -> API Key

Test the request to the API

Download Postman

Create a collection and add a request inside

Upidev: Postman - Request to themoviedb to get details for a famous actor

Create a app script to interact with the API

Create a new google spreadsheet.

Go to Tools -> Script Editor to create a script and make a request and get data for the actor name passed in query.

function importListOfMoviesForActor() {
  /* Get current actorName from cell in the current spreadSheet */
  /* getRange(row, col) */
  let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var actorName = sheet.getRange(1, 2).getValue();
  /* In clear here, not very secure */
  let apiKey = 'moviedb_api_key';
  let apiUrl = `${apiKey}&query=${actorName}`;
  let responseJSON = UrlFetchApp.fetch(apiUrl);
  let responseDataObject = JSON.parse(responseJSON);
  if(responseDataObject['results'].length > 0){

    movieList = responseDataObject['results'][0]['known_for'];

    for(var index in movieList){
      sheet.getRange(3 + parseInt(index), 2).setValue(movieList[index]['title']);


    sheet.getRange(3, 2).setValue('Movies not found');
    sheet.getRangeList(['B4', 'B5']).clearContent();

/* importListOfMoviesForActor() */

To debug the procedure and see logs, go to View -> Logs.

Create a button to call the function to get list of movies

Go to Insert -> Drawing.

Upidev: Create a button in a google spreadsheet and call a app script

Now, let’s connect the app script with the button.

Then, Right click on the button -> Assign a script and copy the name of the procedure `importListOfMoviesForActor`.

Upidev: Assign a app script to a google spreadsheet button

In the script, one cell is used for the input and a list of cells to display the output with movies name. At the end, you should have something similar to the following sheet.

Upidev: Connect a google spreadsheet to an API with App script - UI

I hope you enjoyed this post. If you have any questions, please let me know in comments.

If you liked this post, help us by giving a like or sharing this post.

To know more about me: [FR version]



Laisser un commentaire