-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"scriptId":"14tK6PD4C624ivRyGk-S6eYCbYJnDfA24xeP0Jhb1U8sPgAvZXeZm5gpb"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
function getBook(id) { | ||
const apiKey = 'YOUR_API_KEY'; // Replace with your API key | ||
const apiEndpoint = `https://www.googleapis.com/books/v1/volumes/${id}?key=${apiKey}&country=US`; | ||
const response = UrlFetchApp.fetch(apiEndpoint); | ||
return JSON.parse(response); | ||
} | ||
|
||
function bookLinkPreview(event) { | ||
if (event.docs.matchedUrl.url) { | ||
const segments = event.docs.matchedUrl.url.split('/'); | ||
const volumeID = segments[segments.length - 1]; | ||
|
||
const bookData = getBook(volumeID); | ||
const bookTitle = bookData.volumeInfo.title; | ||
const bookDescription = bookData.volumeInfo.description; | ||
const bookImage = bookData.volumeInfo.imageLinks.small; | ||
const bookAuthors = bookData.volumeInfo.authors; | ||
const bookPageCount = bookData.volumeInfo.pageCount; | ||
|
||
const previewHeader = CardService.newCardHeader() | ||
.setSubtitle('By ' + bookAuthors) | ||
.setTitle(bookTitle); | ||
|
||
const previewPages = CardService.newDecoratedText() | ||
.setTopLabel('Page count') | ||
.setText(bookPageCount); | ||
|
||
const previewDescription = CardService.newDecoratedText() | ||
.setTopLabel('About this book') | ||
.setText(bookDescription).setWrapText(true); | ||
|
||
const previewImage = CardService.newImage() | ||
.setAltText('Image of book cover') | ||
.setImageUrl(bookImage); | ||
|
||
const buttonBook = CardService.newTextButton() | ||
.setText('View book') | ||
.setOpenLink(CardService.newOpenLink() | ||
.setUrl(event.docs.matchedUrl.url)); | ||
|
||
const cardSectionBook = CardService.newCardSection() | ||
.addWidget(previewImage) | ||
.addWidget(previewPages) | ||
.addWidget(CardService.newDivider()) | ||
.addWidget(previewDescription) | ||
.addWidget(buttonBook); | ||
|
||
return CardService.newCardBuilder() | ||
.setHeader(previewHeader) | ||
.addSection(cardSectionBook) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{ | ||
"timeZone": "America/Los_Angeles", | ||
"exceptionLogging": "STACKDRIVER", | ||
"runtimeVersion": "V8", | ||
"oauthScopes": [ | ||
"https://www.googleapis.com/auth/workspace.linkpreview", | ||
"https://www.googleapis.com/auth/script.external_request" | ||
], | ||
"addOns": { | ||
"common": { | ||
"name": "Preview Books Add-on", | ||
"logoUrl": "https://developers.google.com/workspace/add-ons/images/library-icon.png", | ||
"layoutProperties": { | ||
"primaryColor": "#dd4b39" | ||
} | ||
}, | ||
"docs": { | ||
"linkPreviewTriggers": [ | ||
{ | ||
"runFunction": "bookLinkPreview", | ||
"patterns": [ | ||
{ | ||
"hostPattern": "*.google.*", | ||
"pathPrefix": "books" | ||
}, | ||
{ | ||
"hostPattern": "*.google.*", | ||
"pathPrefix": "books/edition" | ||
} | ||
], | ||
"labelText": "Book", | ||
"logoUrl": "https://developers.google.com/workspace/add-ons/images/book-icon.png", | ||
"localizedLabelText": { | ||
"es": "Libros" | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} |