0

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.

Kray
  • 3
  • 1

1 Answers1

0

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);
  }
}
TheWizEd
  • 7,517
  • 2
  • 11
  • 19
  • Thank you very much for your help, i now am getting the google ID! – Kray Jun 20 '22 at 16:33
  • If this solves your issue please mark it as the Answer. – TheWizEd Jun 20 '22 at 16:36
  • Authors are still not coming in, however -- also, i had to remove the catch(err) because it was generating this error stopping the script from saving: Syntax error: SyntaxError: Unexpected token 'catch' line: 39 file: Code.gs For the dimensions, i see them listed under volumeInfo (see https://www.jmu.edu/web_ssi/php/google-api-python-client-54dc473e19f6%202/docs/dyn/books_v1.volumes.mybooks.html) – Kray Jun 20 '22 at 16:41
  • You've obviously made a typo in your script to generate the error you mention. I always enclose my code in a `try { } catch(err) { }` block. Regarding `dimension` it may not always be defined. Using "9781451648546" it is not defined. You may need to include a check for it or live with `undefined`. And in the same example author was an array containing one name so to see it you would need to access `authors[0]`; – TheWizEd Jun 20 '22 at 16:53
  • This is excellent, thank you so much (i had made a typo) -- i am now retrieving the first two authors along with the rest of the information with return [[id,authors[0],authors[1],title,subtitle,pageCount,publisher,publishedDate,description,dimensions]]; i am marking this as the answer; i will continue trying to find a way to access dimensions, as the information does seem to be in there, but i appreciate it involves different issues – Kray Jun 20 '22 at 17:04
  • You could use `[[id,authors.join(),title,subtitle,pageCount,publisher,publishedDate,description,dimensions]]`. It will concatenate all authors names seperated by a comma as a single string. – TheWizEd Jun 20 '22 at 17:16