Search code examples
google-apps-scriptgoogle-books-api

Problems Returning Results from Google books API using Google Script in Google Sheets


I have been trying to find a way to pull in book data to populate a google sheet for which I have one column of ISBNs. This is code I found online which is doing a great job with some information but seems stalled with some other:

    function getBookDetails(isbn) {
  // Query the book database by ISBN code.
  isbn = isbn ; 

  var url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn+'&country=DE';

  var response = UrlFetchApp.fetch(url);
  var results = JSON.parse(response);

  if (results.totalItems) {
    // There'll be only 1 book per ISBN
    var book = results.items[0];

    var title = book['volumeInfo']['title'];
    var subtitle = book['volumeInfo']['subtitle'];
    var authors = book['volumeInfo']['authors'];
    var printType = book['volumeInfo']['printType'];
    var pageCount = book['volumeInfo']['pageCount'];
    var publisher = book['volumeInfo']['publisher'];
    var publishedDate = book['volumeInfo']['publishedDate'];
    var webReaderLink = book['accessInfo']['webReaderLink'];
    var description = book['volumeInfo']['description'];
    var description = book['volumeInfo']['dimensions'];
    var id = book['volumeInfo']['id'];
    var authors=book['volumeInfo'][['authors']]
    
    

    
   return [[id,authors,title,subtitle,pageCount,publisher,publishedDate,description,dimensions]];

  }
}

This works fine for title,subtitle,pageCount,publisher,publishedDate,authors,description but it does not work for id, authors, or dimensions, which seem to be returning null. For authors, I did manage to get them via a separate function:

function getBookAuthors(isbn) {
  // Query the book database by ISBN code.
  isbn = isbn || '9781451648546'; 

  var url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn+'&country=CA';

  var response = UrlFetchApp.fetch(url);
  var results = JSON.parse(response);

  if (results.totalItems) {
    // There'll be only 1 book per ISBN
    var book = results.items[0];

 
    var [authors]=book['volumeInfo'][['authors']]
    
    

   
   return [[authors]];

  }
}

However, this only returns one author even for books with more than one author listed on Google Books. Furthermore, I have been unable to pull in the dimensions (height, width, length). Is this because these are arrays? I believe this can be done, but I am very new to coding (the above is something I found online, not something I coded myself) and am not sure what I am doing wrong.


Solution

  • Change your script accordingly. id is not in volumeInfo but in items and [['authors']] is incorrect. And I do not see dimension in book or volumeInfo. Everything is a value except authors which is an array. It may be an array of 1 or more.

    function getBookDetails() {
      try {
        // Query the book database by ISBN code.
        let isbn = "9781451648546" ; 
    
        var url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn+'&country=DE';
    
        var response = UrlFetchApp.fetch(url);
        var results = JSON.parse(response);
    
        if (results.totalItems) {
          // There'll be only 1 book per ISBN
          var book = results.items[0];
    
          var title = book['volumeInfo']['title'];
          var subtitle = book['volumeInfo']['subtitle'];
          var authors = book['volumeInfo']['authors'];
          var printType = book['volumeInfo']['printType'];
          var pageCount = book['volumeInfo']['pageCount'];
          var publisher = book['volumeInfo']['publisher'];
          var publishedDate = book['volumeInfo']['publishedDate'];
          var webReaderLink = book['accessInfo']['webReaderLink'];
          var description = book['volumeInfo']['description'];
          var dimensions = book['volumeInfo']['dimensions'];
          var id = book['id'];
          var authors=book['volumeInfo']['authors']
          //return [[id,authors,title,subtitle,pageCount,publisher,publishedDate,description,dimensions]];
          console.log( [[id,authors,title,subtitle,pageCount,publisher,publishedDate,description,dimensions]] );
        }
      }
      catch(err) {
        console.log(err);
      }
    }