From 23b68c9adea9f3285881891a40f2a90ad25dd173 Mon Sep 17 00:00:00 2001 From: dev Date: Tue, 18 Jun 2024 18:29:27 +0530 Subject: [PATCH] Added feature to reconcile artsdata entities --- appsscript.json | 3 ++- src/artsdata_search.js | 33 ++++++++++++++++++++++++++ src/create_person_organization_card.js | 11 ++++++--- src/index.js | 4 ++-- src/menuItem.js | 5 ---- src/onOpen.js | 6 ++--- 6 files changed, 47 insertions(+), 15 deletions(-) create mode 100644 src/artsdata_search.js delete mode 100644 src/menuItem.js diff --git a/appsscript.json b/appsscript.json index c13992c..f4680a5 100644 --- a/appsscript.json +++ b/appsscript.json @@ -16,7 +16,8 @@ "https://www.googleapis.com/auth/spreadsheets.currentonly" ], "urlFetchWhitelist": [ - "https://kg.artsdata.ca/" + "https://kg.artsdata.ca/", + "https://api.artsdata.ca/recon" ], "addOns":{ "common": { diff --git a/src/artsdata_search.js b/src/artsdata_search.js new file mode 100644 index 0000000..255aae1 --- /dev/null +++ b/src/artsdata_search.js @@ -0,0 +1,33 @@ +const artsdataSearch = () => { + const ui = SpreadsheetApp.getUi(); + const cell = SpreadsheetApp.getActiveSpreadsheet() + .getActiveSheet() + .getActiveCell(); + const value = cell.getValue(); + + const query = encodeURIComponent(`{"q0":{"query":"${value}"}}`); + const url = `https://api.artsdata.ca/recon?queries=${query}`; + + try { + const response = JSON.parse(UrlFetchApp.fetch(url).getContentText()); + const results = response?.q0?.result; + + if (results && results.length > 0) { + const choices = results.map(result => `${result.id} - ${result.name}`).join('\n'); + const userChoice = ui.prompt('Enter an ID from the list:', choices, ui.ButtonSet.OK_CANCEL); + + if (userChoice.getSelectedButton() === ui.Button.OK) { + const chosenId = userChoice.getResponseText(); + cell.setValue(`https://kg.artsdata.ca/resource/${chosenId}`); + } else { + ui.alert('No ID chosen.'); + } + } else { + ui.alert('No results found.'); + } + } catch (e) { + ui.alert(`An error occurred: ${e.message}`); + } +}; + +export default artsdataSearch; diff --git a/src/create_person_organization_card.js b/src/create_person_organization_card.js index cb82ffd..448278e 100644 --- a/src/create_person_organization_card.js +++ b/src/create_person_organization_card.js @@ -4,14 +4,19 @@ const createPersonOrganizationCard = entityData => { const entityName = getMultilingualFields(entityData?.name); const entityType = entityData?.type || ''; const disambiguatingDescription = getMultilingualFields(entityData.disambiguatingDescription); + const entityImage = entityData?.image || ''; const previewHeader = CardService.newCardHeader() .setTitle(entityName) .setSubtitle(entityType); - const cardContent = CardService.newCardSection().addWidget( - CardService.newTextParagraph().setText(disambiguatingDescription) - ); + const previewImage = CardService.newImage() + .setAltText('Image of entity') + .setImageUrl(entityImage); + + const cardContent = CardService.newCardSection() + .addWidget(CardService.newTextParagraph().setText(disambiguatingDescription)) + .addWidget(previewImage); return CardService.newCardBuilder() .setHeader(previewHeader) diff --git a/src/index.js b/src/index.js index efc3d18..9686e4a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,9 @@ import artsdataPreview from './artsdata_preview'; import getEntityDetails from './get_entity_details'; import onOpen from './onOpen'; -import menuItem from './menuItem'; +import artsdataSearch from './artsdata_search'; global.artsdataPreview = artsdataPreview; global.getEntityDetails = getEntityDetails; global.onOpen = onOpen; -global.menuItem = menuItem; +global.artsdataSearch = artsdataSearch; diff --git a/src/menuItem.js b/src/menuItem.js deleted file mode 100644 index c5a454e..0000000 --- a/src/menuItem.js +++ /dev/null @@ -1,5 +0,0 @@ -const menuItem = () => { - SpreadsheetApp.getUi().alert('You clicked the item!'); -}; - -export default menuItem; diff --git a/src/onOpen.js b/src/onOpen.js index f618237..7294385 100644 --- a/src/onOpen.js +++ b/src/onOpen.js @@ -1,10 +1,8 @@ const onOpen = () => { SpreadsheetApp.getUi() - .createMenu('Custom Menu') - .addItem('First Item', 'menuItem') + .createMenu('Artsdata Reconciliation') + .addItem('Search', 'artsdataSearch') .addToUi(); - - }; export default onOpen;