Skip to content

Instantly share code, notes, and snippets.

@Ryoga-exe
Last active July 12, 2022 15:46
Show Gist options
  • Save Ryoga-exe/993d400e8f2b7bce9d5262197459d548 to your computer and use it in GitHub Desktop.
Save Ryoga-exe/993d400e8f2b7bce9d5262197459d548 to your computer and use it in GitHub Desktop.
国立国会図書館サーチAPIを用いたISBNからの書籍情報取得
function fetchBookData(isbn) {
const url = `https://iss.ndl.go.jp/api/opensearch?isbn=${isbn}`;
const xml = UrlFetchApp.fetch(url).getContentText();
const document = XmlService.parse(xml);
const items = document.getRootElement().getChildren('channel')[0].getChildren('item');
if (items.length == 0) {
return;
}
else {
const item = items[0];
const namespaceDc = XmlService.getNamespace("dc", 'http://purl.org/dc/elements/1.1/');
const [
title,
author,
publisher
] = [
item.getChild('title').getValue(),
item.getChild('author').getValue(),
item.getChild('publisher', namespaceDc).getValue(),
];
return [title, author, publisher];
}
}
function update() {
try {
const ss = SpreadsheetApp.getActiveSpreadsheet();
if (!ss.getActiveSheet().getName().match(/data/)) {
return;
}
const range = ss.getActiveSheet().getActiveCell();
const isbn = range.getValue().toString();
if (range.getColumn() !== 1) {
return;
}
if ((isbn.length == 13 && (/\d{12}[0-9X]/g).test(isbn)) || (isbn.length == 10 && (/\d{9}[0-9X]/g).test(isbn))) {
const [title, author, publisher] = fetchBookData(isbn);
ss.getRange('B' + range.getRow()).setValue(title);
ss.getRange('C' + range.getRow()).setValue(author);
ss.getRange('D' + range.getRow()).setValue(publisher);
}
}
catch (error) {
Logger.log(error);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment