google-apps-scriptgoogle-docsadd-on

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


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"
}

Solution

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

    When the OP created this question, the developer had to use the legacy editor to use the "Test as add-on" feature of the Google Apps Script. It might be OK to test features like an onOpen simple trigger but not other features like installable triggers. Nowadays, the old editor is no longer available, but the test as an add-on feature is available in the new editor. Still, there are some features that can't be tested using this feature.

    Apparently, your editor add-on does not use installable triggers and other features that can't be tested with "Test as add-on." However, it's very likely that it will be good enough to test the simple triggers of your add-on.

    Also, your add-on is missing the onInstall simple trigger, which usually calls the onOpen trigger when the add-on is installed from an opened document.

    Reference

    Related