Fetch Book Title & Author From ISBN using Google Books API

If you want to log all of your books and create a personal library system, this post might be for you. I’ll talk about:

  • How it works

  • API selections and available fields

  • Setting up Google Sheets and Apps Script. (the full code is at the end of this post).

How it works:

  1. Scan the books’ barcode using a third-party scanner app (out of scope from this post today)

  2. Export the barcode to Google Sheets

  3. Run Apps Script on Google Sheets: it will fetch the Title & Author of a book from the ISBN, using Google Books API.

Demo Video

API Selection

There’s a list of APIs that can be used for this task, such as Open Library API (can even see the book’s content), WorldCat, and more in this article by Vinzius.

I am using Google Books API, and I can get a lot of information about a book, such as title, authors, publishers, page count, description, etc.

Setting Up The Google Sheet

Set up your columns with fields that you need, this is how I set up mine.

Setting up google sheet with 3 columns: isbn, title & author (these will be auto-populated)

Creating the script

  1. Open Apps Script (Open Google Sheet > Extensions > Apps Script)

  2. You can use this script, and run the script 😁

function getBookDetails() {
    // get the sheet where the ISBN data resides
    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const sheet = ss.getSheetByName('books');
    const [head, ...data] = sheet.getDataRange().getValues();

    // iterate through every row of data
    data.forEach((row,i) => {
      const isbn = row[head.indexOf('isbn')]
      const book_title = row[head.indexOf('title')]
      
      /* if book title column is already filled, 
          then we don't need to call the API to get book information again.
          we also make sure if isbn is empty, then no need to call the API */
     
     if (book_title){
          if (book_title == "" || isbn == "" || isbn.length == 0 ){
            return;
          }
      }

      // fetch the information
      else{
        try {
          // calling the API
          var url = 'https://www.googleapis.com/books/v1/volumes?q=isbn:' + isbn +'&country=US';
          var response = UrlFetchApp.fetch(url);
          var results = JSON.parse(response);

          if (results.totalItems) {
            // Making sure there'll be only 1 book per ISBN
            var book = results.items[0];
            var title = book['volumeInfo']['title'];
            var authors = book['volumeInfo']['authors'];
            
            // tell the script where to put the title and author information
            const selected_range_title = 'B'+(i+2);
            const selected_range_author = 'C'+(i+2);
            
            sheet.getRange(selected_range_title).setValue(title);
            sheet.getRange(selected_range_author).setValue(authors);
            
            }
          }
        catch(err) {
          console.log(err);
        }
      }
    }) 
    
  }

See more of my coding projects here!

ps. I’m grateful that my sabbatical allowed me extra energy and mind space to follow my procrastination’s whim (this started as a way for me to procrastinate on re-arranging my bookshelf).

Previous
Previous

Facilitating Design Sprints & Agile Framework for Heineken

Next
Next

Survey Planning Training (2024)