Home > Software design >  Take selected text, send it over to Scryfall API, then take the link and put it in the selected text
Take selected text, send it over to Scryfall API, then take the link and put it in the selected text

Time:12-10

I've been able to sort out the middle bit (the API seems to be called to just fine) along with the submenu displaying. Originally I thought that just the end part wasn't working but I'm now thinking that the selection part isn't either.
What am I doing wrong with the getSelection() and what do I need to do to insert a link into said selection? (to clarify, not to replace the text with a link, but to insert a link into the text)

//Open trigger to get menu
function onOpen(e) {
 DocumentApp.getUi().createAddonMenu()
  .addItem('Scry', 'serumVisions')
  .addToUi();
}

//Installation trigger
function onInstall(e) {
 onOpen(e);
}

//I'm not sure if I need to do this but in case; declare var elements first

var elements

// Get selected text (not working)
function getSelectedText() {
const selection = DocumentApp.getActiveDocument().getSelection();
 if (selection) {
  var elements = selection.getRangeElements();
  Logger.log(elements);
 } else {
  var elements = "Lack of selection"
  Logger.log("Lack of selection");
 }
}

//Test run
// insert here

// Search Function
function searchFunction(nameTag) {
 // API call   inserted Value
 let URL = "https://api.scryfall.com/cards/named?exact="   nameTag;
 // Grabbing response
 let response = UrlFetchApp.fetch(URL, {muteHttpExceptions: true});
 let json = response.getContentText();
 // Translation
 let data = JSON.parse(json);
 // Jackpot
 let link = data.scryfall_uri;
 // Output
 Logger.log(link);
}

// Test run
searchFunction("Lightning Bolt");

//Let's hope this works how I think it works
function serumVisions() {
 const hostText = getSelectedText();
 const linkage = searchFunction(hostText);
 // Unsure what class I'm supposed to use, this doesn't
 const insertLink = DocumentApp.getActiveDocument().getSelection().newRichTextValue()
 .setLinkUrl(linkage);
 Logger.log(linkage);
}

For the first part, I tried the getSelection() and getCursor() examples from the Google documentation but they don't seem to work, they all just keep returning null.

For the inserting link bit, I read all those classes from the Spreadsheet section of the documentation, at the time I was unaware but now knowing, I haven't been able to find a version of the same task for Google Docs. Maybe it works but I'm writing it wrong as well, idk.

CodePudding user response:

Modification points:

  • In your script, the functions of getSelectedText() and searchFunction(nameTag) return no values. I think that this might be the reason for your current issue of they all just keep returning null..
  • elements of var elements = selection.getRangeElements(); is not text data.
  • DocumentApp.getActiveDocument().getSelection() has no method of newRichTextValue().
  • In the case of searchFunction("Lightning Bolt");, when the script is run, this function is always run. Please be careful about this.

When these points are reflected in your script, how about the following modification?

Modified script:

Please remove searchFunction("Lightning Bolt");. And, in this case, var elements is not used. Please be careful about this.

From your script, I guessed that in your situation, you might have wanted to run serumVisions(). And also, I thought that you might have wanted to run the individual function. So, I modified your script as follows.

function getSelectedText() {
  const selection = DocumentApp.getActiveDocument().getSelection();
  var text = "";
  if (selection) {
    text = selection.getRangeElements()[0].getElement().asText().getText().trim();
    Logger.log(text);
  } else {
    text = "Lack of selection"
    Logger.log("Lack of selection");
  }
  return text;
}

function searchFunction(nameTag) {
  let URL = "https://api.scryfall.com/cards/named?exact="   encodeURIComponent(nameTag);
  let response = UrlFetchApp.fetch(URL, { muteHttpExceptions: true });
  let json = response.getContentText();
  let data = JSON.parse(json);
  let link = data.scryfall_uri;
  Logger.log(link);
  return link;
}

// Please run this function.
function serumVisions() {
  const hostText = getSelectedText();
  const linkage = searchFunction(hostText);
  if (linkage) {
    Logger.log(linkage);
    DocumentApp.getActiveDocument().getSelection().getRangeElements()[0].getElement().asText().editAsText().setLinkUrl(linkage);
  }
}
  • When you select the text of "Lightning Bolt" in the Google Document and run the function serumVisions(), the text of Lightning Bolt is retrieved, and the URL like https://scryfall.com/card/2x2/117/lightning-bolt?utm_source=api is retrieved. And, this link is set to the selected text of "Lightning Bolt".

Reference:

  • Related