Home > Back-end >  How to test an editor add-on for Google Docs with the new Apps Script editor?
How to test an editor add-on for Google Docs with the new Apps Script editor?

Time:12-23

I'm trying to test a simple add-on for Google Docs that I've made but it seems that this page of documentation is related to the legacy Apps Script editor, and I cannot find out how to do with the new Apps Script editor.

  1. I have read this topic, but he's trying to deploy a Workspace add-on (which is different to editor add-on)
  2. I know that I could simply copy-paste my code into an Apps Script directly bound to a Google Docs, but that's not what I want, I really want my add-on code in its own, independant, Apps Script project.
  3. My Apps Script project is linked to a proper GCP project (with billing and oauth consent screen ok)

My code, if it helps

const PASS = "PASSPHRASE";

function decrypt(text) {
  var cipher = new cCryptoGS.Cipher(PASS, 'aes');
  return cipher.decrypt(text)
}

function encrypt(text) {
  var cipher = new cCryptoGS.Cipher(PASS, 'aes');
  return cipher.encrypt(text)
}

function decryptDocument() {
  var doc = DocumentApp.getActiveDocument();
  var paragraphs = doc.getBody().getParagraphs();
  return paragraphs.reduce((previous, current) => {
    return previous   "\n"   decrypt(current.getText());
  }, ""); 
}

function onOpen() {
  DocumentApp.getUi()
      .createMenu('Décodeur')
      .addItem('Lancer le décodeur', 'showSidebar')
      .addToUi();
}

function showSidebar() {
  var html = HtmlService.createHtmlOutputFromFile('decoder')
      .setTitle('Décodeur');
  DocumentApp.getUi().showSidebar(html);
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    <button onclick="decrypt()">Décoder le contenu</button>
    <div id="decodedText">
      </div>
  </body>
  <script>
    var running = false;

    function onSuccess(decodedText) {
      running = false;
      document.getElementById("decodedText").innerHTML = decodedText;
    }

    function onFailure(e) {
      running = false;
      console.error(e);
    }

    function cleanDiv() {
      document.getElementById("decodedText").innerHTML = "";
    }

    function decrypt() {
      running = true;
      google.script.run.withSuccessHandler(onSuccess)
        .withFailureHandler(onFailure)
        .decryptDocument();
    }
    </script>
</html>
{
  "timeZone": "America/New_York",
  "dependencies": {
    "libraries": [
      {
        "userSymbol": "cCryptoGS",
        "version": "4",
        "libraryId": "1IEkpeS8hsMSVLRdCMprij996zG6ek9UvGwcCJao_hlDMlgbWWvJpONrs"
      }
    ]
  },
  "exceptionLogging": "STACKDRIVER",
  "runtimeVersion": "V8"
}

CodePudding user response:

The only way to thoroughly test an editor add-on is by publishing by using the Google Workspace SDK.

If you want to use "Test as add-on" feature of the Google Apps Script you have to use the legacy editor. This might be fine to test some features like an onOpen simple trigger but not to test other features like installable triggers.

As apparently your editor add-on is not using installable triggers and other features that can't be tested with "Test as add-on" it's very likely that it will be good enough to test the simple triggers of your add-on.

P.S. Your add-on is missing the onInstall simple trigger that usually is used to call the onOpen trigger when the add-on is installed from an opened document.

Related

  • Related