You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

在Google表格中使用Google Script调用Google Books API获取ISBN对应书籍数据时遇到的问题

Hi there! Let's break down the issues in your code one by one and fix them:

1. Why id returns null

In the Google Books API response, the book's unique id sits at the root level of the book object (book.id), not under the volumeInfo section. Your original code tried to access book['volumeInfo']['id']—this field doesn't exist, hence the null value.

2. Issues with the authors field

  • In your first function, you had duplicate assignments for authors: you correctly grabbed book['volumeInfo']['authors'] first, then overwrote it with book['volumeInfo'][['authors']]—this syntax creates a nested 2D array, which causes unexpected results.
  • In your second function, var [authors]=book['volumeInfo'][['authors']] destructures a nested array, so it only pulls the first author from the list. Instead, you should directly access the authors array and optionally join it into a comma-separated string for clean display in Google Sheets.

3. Why dimensions isn't working

  • You accidentally overwrote the description variable with dimensions (var description = book['volumeInfo']['dimensions'];), breaking both fields.
  • dimensions lives under volumeInfo, but not all books have this data available in the API. You need to check if it exists before accessing it, otherwise it'll return null.
  • Your return statement included dimensions as a variable, but you never defined it separately in the original code.

Here's the fixed version of your function that addresses all these problems:

function getBookDetails(isbn) {
  if (!isbn) return [['No ISBN provided']]; // Handle empty input gracefully
  
  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) {
    var book = results.items[0];
    var volumeInfo = book.volumeInfo; // Store for easier repeated access
    var accessInfo = book.accessInfo;

    // Get book ID from the correct root-level field
    var id = book.id;

    // Handle authors: join array to a readable string, with fallback
    var authors = volumeInfo.authors ? volumeInfo.authors.join(', ') : 'No authors listed';

    // Get basic fields with fallbacks to avoid nulls
    var title = volumeInfo.title || 'No title';
    var subtitle = volumeInfo.subtitle || 'No subtitle';
    var pageCount = volumeInfo.pageCount || 'N/A';
    var publisher = volumeInfo.publisher || 'No publisher';
    var publishedDate = volumeInfo.publishedDate || 'No date';
    var webReaderLink = accessInfo.webReaderLink || 'No link';
    var description = volumeInfo.description || 'No description';

    // Handle dimensions with existence check
    var dimensions = 'N/A';
    if (volumeInfo.dimensions) {
      // Format into a user-friendly string (adjust format as needed)
      dimensions = `${volumeInfo.dimensions.height} x ${volumeInfo.dimensions.width} x ${volumeInfo.dimensions.thickness}`;
    }

    // Return all fields in a structured array for Google Sheets
    return [[id, authors, title, subtitle, pageCount, publisher, publishedDate, description, dimensions]];
  } else {
    return [['No book found for this ISBN']];
  }
}

Key improvements:

  • Added fallbacks for all fields (so you never get null—instead, you get clear placeholders like "No authors listed").
  • Fixed the id path to point to the correct location in the API response.
  • Properly handles multi-author lists by joining them into a comma-separated string (perfect for Google Sheets cells).
  • Separated dimensions from description and added a check to handle cases where dimensions data isn't available.
  • Added error handling for empty ISBN inputs and cases where no book matches the ISBN.

Note about dimensions:

Keep in mind that not all books in the Google Books database have dimensions data. If the API doesn't return it, the function will show "N/A" for that field—this is expected behavior since the data simply isn't available for those titles.

内容的提问来源于stack exchange,提问作者Kray

火山引擎 最新活动