Skip to content

Instantly share code, notes, and snippets.

@eflynn
Last active December 24, 2015 07:19
Show Gist options
  • Select an option

  • Save eflynn/6763205 to your computer and use it in GitHub Desktop.

Select an option

Save eflynn/6763205 to your computer and use it in GitHub Desktop.
Files for Book Library App
<?
if (data.totalItems > 0) {
data.items.forEach(function(item) {
var book = item.volumeInfo;
?>
<div class="book">
<img src="<?= book.imageLinks.smallThumbnail ?>">
</div>
<p><?= book.title ?></p>
<p><?= book.authors.join(', ') ?></p>
<div class="previewBtn">
<a href="#"><img src="https://www.google.com/intl/en/googlebooks/images/gbs_preview_button1.gif"
style="border:0; margin:3px;" border="0"></a>
</div>
<div style="clear:left;"></div>
<? });
}
else {
?>
<p>No results found</p>
<? } ?>
function onOpen() {
SpreadsheetApp.getActive().addMenu('Book Library', [
{name: 'Search Google Books', functionName: 'addBook_'},
{name: 'Search by ISBN', functionName: 'addIsbn_'}
]);
}
function addBook_() {
var htmlApp = HtmlService
.createTemplateFromFile('index')
.evaluate()
.setTitle('Add Book')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
SpreadsheetApp.getActive().show(htmlApp);
}
function addIsbn_() {
var htmlApp = HtmlService
.createHtmlOutputFromFile('isbn')
.setTitle('Search by ISBN')
.setHeight(100)
.setWidth(300)
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
SpreadsheetApp.getActive().show(htmlApp);
}
function insertIsbn(form) {
var isbn = form.isbn;
Logger.log(isbn);
var bookUri = 'https://www.googleapis.com/books/v1/volumes?maxResults=1&q=' + encodeURIComponent('isbn:' + isbn);
var response = UrlFetchApp.fetch(bookUri);
var book = Utilities.jsonParse(response).items[0].volumeInfo;
var data = [isbn.trim(), book.title, book.authors.join(', '), book.publisher || ''];
SpreadsheetApp.getActive().getSheetByName('Library').appendRow(data);
}
function insertBook(id) {
/*
var bookUri = 'https://www.googleapis.com/books/v1/volumes/' + id;
var response = UrlFetchApp.fetch(bookUri);
var data = Utilities.jsonParse(response);
*/
}
function search(query) {
var data;
if (query != '') {
// Query the Google Books Service
var bookUri = 'https://www.googleapis.com/books/v1/volumes?maxResults=3&q=' + encodeURIComponent(query);
var response = UrlFetchApp.fetch(bookUri);
data = Utilities.jsonParse(response);
}
else {
data = {
totalItems: 0
};
}
var view = HtmlService.createTemplateFromFile('books');
view.data = data;
return view.evaluate().getContent();
}
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<form id="search" role="form">
<div class="form-group">
<label class="sr-only" for="query">Search Query</label>
<input id="query" type="search" placeholder="ISBN or title" class="form-control">
</div>
</form>
<div id="books">
</div>
<?!= include('script'); ?>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
<form role="form">
<div class="form-group">
<input type="search" class="form-control" name="isbn" placeholder="ISBN-10 or ISBN-13 number">
</div>
<button class="btn btn-default" onclick="returnIsbn(this.form)">Add to Library</button>
</form>
<script>
function returnIsbn(form) {
google.script.run.insertIsbn(form);
}
</script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('#search').submit(function(event) {
event.preventDefault();
var query = $('#query').val();
google.script.run
.withSuccessHandler(showBooks)
.withFailureHandler(showError)
.search(query);
});
$('#books').on('click', '.addbtn', function() {
var bookId = $(this).attr('book');
google.script.host.close();
});
});
function showBooks(bookHtml) {
$('#books').html(bookHtml);
}
function showError() {
$('#books').html("<p>Sorry, the books couldn't be retrieved.</p>");
}
</script>
function checkIsbn_(isbn) {
if (typeof isbn != 'string') {
throw "ISBN must be formatted as plain text";
}
isbn = isbn.trim();
if (!/^\d+$/.test(isbn) || (isbn.length != 10 && isbn.length != 13)) {
throw "invalid IBSN -- must be 10 or 13 digits long";
}
return isbn;
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment