|
| 1 | +function getBook(id) { |
| 2 | + const apiKey = 'YOUR_API_KEY'; // Replace with your API key |
| 3 | + const apiEndpoint = `https://www.googleapis.com/books/v1/volumes/${id}?key=${apiKey}&country=US`; |
| 4 | + const response = UrlFetchApp.fetch(apiEndpoint); |
| 5 | + return JSON.parse(response); |
| 6 | +} |
| 7 | + |
| 8 | +function bookLinkPreview(event) { |
| 9 | + if (event.docs.matchedUrl.url) { |
| 10 | + const segments = event.docs.matchedUrl.url.split('/'); |
| 11 | + const volumeID = segments[segments.length - 1]; |
| 12 | + |
| 13 | + const bookData = getBook(volumeID); |
| 14 | + const bookTitle = bookData.volumeInfo.title; |
| 15 | + const bookDescription = bookData.volumeInfo.description; |
| 16 | + const bookImage = bookData.volumeInfo.imageLinks.small; |
| 17 | + const bookAuthors = bookData.volumeInfo.authors; |
| 18 | + const bookPageCount = bookData.volumeInfo.pageCount; |
| 19 | + |
| 20 | + const previewHeader = CardService.newCardHeader() |
| 21 | + .setSubtitle('By ' + bookAuthors) |
| 22 | + .setTitle(bookTitle); |
| 23 | + |
| 24 | + const previewPages = CardService.newDecoratedText() |
| 25 | + .setTopLabel('Page count') |
| 26 | + .setText(bookPageCount); |
| 27 | + |
| 28 | + const previewDescription = CardService.newDecoratedText() |
| 29 | + .setTopLabel('About this book') |
| 30 | + .setText(bookDescription).setWrapText(true); |
| 31 | + |
| 32 | + const previewImage = CardService.newImage() |
| 33 | + .setAltText('Image of book cover') |
| 34 | + .setImageUrl(bookImage); |
| 35 | + |
| 36 | + const buttonBook = CardService.newTextButton() |
| 37 | + .setText('View book') |
| 38 | + .setOpenLink(CardService.newOpenLink() |
| 39 | + .setUrl(event.docs.matchedUrl.url)); |
| 40 | + |
| 41 | + const cardSectionBook = CardService.newCardSection() |
| 42 | + .addWidget(previewImage) |
| 43 | + .addWidget(previewPages) |
| 44 | + .addWidget(CardService.newDivider()) |
| 45 | + .addWidget(previewDescription) |
| 46 | + .addWidget(buttonBook); |
| 47 | + |
| 48 | + return CardService.newCardBuilder() |
| 49 | + .setHeader(previewHeader) |
| 50 | + .addSection(cardSectionBook) |
| 51 | + .build(); |
| 52 | + } |
| 53 | +} |
0 commit comments