我一直在嘗試找到一種方法來提取書籍資料以填充我有一列 ISBN 的谷歌表格。這是我在網上找到的代碼,它在一些資訊方面做得很好,但似乎在其他一些方面停滯不前:
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]];
}
}
這適用于標題、副標題、pageCount、publisher、publishedDate、authors、description,但不適用于似乎回傳 null 的 id、作者或維度。對于作者,我確實設法通過一個單獨的函式來獲取它們:
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]];
}
}
但是,即使對于在 Google 圖書中列出的不止一位作者的圖書,這也只會回傳一位作者。此外,我無法提取尺寸(高度、寬度、長度)。這是因為這些是陣列嗎?我相信這是可以做到的,但是我對編碼很陌生(以上是我在網上找到的,不是我自己編碼的)并且不確定我做錯了什么。
uj5u.com熱心網友回復:
相應地更改您的腳本。 idis not in volumeInfobut initems并且[['authors']]是不正確的。而且我沒有dimension看到bookor volumeInfo。一切都是一個值,除了authors它是一個陣列。它可能是 1 個或更多的陣列。
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);
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/494452.html
