Last active
October 4, 2024 14:02
-
-
Save bramses/1b3a6f64a0a8c7e9361c98cea450f42c to your computer and use it in GitHub Desktop.
Search yCb in Google Docs and paste diresctly into editor
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
function onOpen() { | |
DocumentApp.getUi().createMenu('yCb') | |
.addItem('Send Selected Text to API', 'processSelectedText') | |
.addToUi(); | |
} | |
function processSelectedText() { | |
var doc = DocumentApp.getActiveDocument(); | |
var selection = doc.getSelection(); | |
if (!selection) { | |
DocumentApp.getUi().alert('No text selected.'); | |
return; | |
} | |
var selectedText = ''; | |
var elements = selection.getRangeElements(); | |
// Loop through the selected elements and capture the text | |
for (var i = 0; i < elements.length; i++) { | |
var element = elements[i].getElement(); | |
var text = element.asText(); | |
if (elements[i].isPartial()) { | |
// Capture partially selected text | |
selectedText += text.getText().substring(elements[i].getStartOffset(), elements[i].getEndOffsetInclusive() + 1); | |
} else { | |
// Capture fully selected text | |
selectedText += text.getText(); | |
} | |
} | |
if (selectedText) { | |
// Call the external API with the selected text | |
var apiUrl = 'YCB_URL'; | |
var response = UrlFetchApp.fetch(apiUrl, { | |
method: 'post', | |
contentType: 'application/json', | |
payload: JSON.stringify({ | |
query: selectedText, | |
dbPath: "YCB_DB_PATH", | |
apiKey: "YCB_API_KEY" | |
}), | |
}); | |
// Log the API response to debug | |
Logger.log(response.getContentText()); | |
// Try parsing the API response | |
try { | |
var apiResponse = response.getContentText(); | |
var results = JSON.parse(apiResponse); // Fix for multiple JSON objects | |
// Loop through the results and format each as a blockquote | |
for (var i = 0; i < results.length; i++) { | |
var result = results[i]; | |
var data = result.data || 'No data'; | |
// Safely parse the metadata field | |
var metadata; | |
try { | |
metadata = result.metadata ? JSON.parse(result.metadata) : { title: 'Unknown Title', author: 'Unknown Author' }; | |
} catch (e) { | |
metadata = { title: 'Unknown Title', author: 'Unknown Author' }; | |
} | |
// Insert the blockquote text (data) | |
var body = doc.getBody(); | |
var textElement = body.appendParagraph(data); | |
textElement.setIndentStart(36); // Indent for blockquote effect | |
// Insert the link text below the blockquote | |
var linkText = '-- ' + metadata.title; | |
var linkParagraph = body.appendParagraph(linkText); | |
// Set the link for the entire paragraph text | |
var textRange = linkParagraph.editAsText(); | |
textRange.setLinkUrl(0, linkText.length - 1, "YCB_URL/dashboard/entry/" + result.id); // Set the entire link text to hyperlink | |
// Add two newlines (as a blank paragraph) to separate the results | |
body.appendParagraph(''); // First line break | |
body.appendParagraph(''); // Second line break | |
} | |
} catch (e) { | |
DocumentApp.getUi().alert('Error parsing API response: ' + e.message); | |
} | |
} else { | |
DocumentApp.getUi().alert('No text selected.'); | |
} | |
} |
Author
bramses
commented
Oct 4, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment