Populating a Google Sheet with Goodreads data

Here’s an example of how you can use the Goodreads API with Google Sheets and Apps Script to automatically populate a sheet with book data based only on the book’s ISBN.

google sheet with goodreads data

const API_KEY = "REPLACE_ME";

// paths to fields to extract from xml
// e.g., ["authors", "author", "name"] maps to "Charles Dickens" in this XML:
//   <authors>
//     <author>
//       <name>Charles Dickens</name>
//     </author>
//   </authors>
const FIELD_PATHS = [
  ["title"],
  ["authors", "author", "name"],
  ["publication_year"],
  ["description"],
  ["num_pages"],
  ["image_url"], // to display an image from this url, create a formula cell
                 // with `=IMAGE(X)`, where X is the cell containing this url
];

function bookDataFromISBN(isbn) {
  // isbn field must be text instead of number so that leading 0s are retained
  if (isbn == "") { return; }
  const url = `https://www.goodreads.com/book/isbn/${isbn}?format=xml&key=${API_KEY}`;
  const xml = UrlFetchApp.fetch(url).getContentText();
  const document = XmlService.parse(xml);
  const root = document.getRootElement();
  const book = root.getChildren("book")[0];

  return [FIELD_PATHS.map(fieldPath => {
    return fieldPath.reduce((obj, pathPart) => {
      return obj.getChild(pathPart);
    }, book).getText().replace( /(<([^>]+)>)/ig, '');
  })];
}