Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //https://docs.google.com/spreadsheets/d/1Liq71Kdhe45sYh2Puk6hrpXIEpVw_GNHsZmKnMvoF4U/edit#gid=2100307022
- function onOpen(e) {
- SpreadsheetApp.getUi().createMenu('📚 Books')
- .addItem('📥 Load selected row', 'getBookData')
- .addToUi();
- }
- function getBookData() {
- const ss = SpreadsheetApp.getActiveSpreadsheet()
- const sheet = ss.getActiveSheet();
- if(sheet.getName() != 'Books'){
- throw new Error('Active sheet is not Books!')
- }
- const row = sheet.getActiveCell().getRow()
- const isbn = sheet.getRange(row,2).getValue()
- const urlDetails = `https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=details`
- const requestDetails = UrlFetchApp.fetch(urlDetails)
- const dataDetails = JSON.parse(requestDetails.getContentText())[`ISBN:${isbn}`]
- const urlData = `https://openlibrary.org/api/books?bibkeys=ISBN:${isbn}&format=json&jscmd=data`
- const requestData = UrlFetchApp.fetch(urlData)
- const dataData = JSON.parse(requestData.getContentText())[`ISBN:${isbn}`]
- const bookUrl = dataDetails.info_url
- const title = dataDetails.details.title
- const coverUrl = dataData.cover.large
- const author = dataData.authors[0].name
- const authorUrl = `https://openlibrary.org/${dataData.authors[0].key}`
- const year = dataDetails.details.publish_date
- const pages = dataDetails.details.number_of_pages
- const genre = [...new Set(dataDetails.details.genres)].join(" / ")
- const formulas = [[
- `=HYPERLINK("${bookUrl}",IMAGE("${coverUrl}"))`,
- `=HYPERLINK("${bookUrl}","${title}")`,
- `=HYPERLINK("${authorUrl}","${author}")`
- ]]
- sheet.getRange(row, 3,1,3).setFormulas(formulas)
- sheet.getRange(row, 6,1,3).setValues([[year, pages, genre]])
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement