1

edit: wound up using cheerio to manipulate the elements instead of creating them in a sidebar.

Is it possible to create an execute htmlOutput in a background page, or do so without showing the user anything?

Sample code below:

plugin.gs

function onOpen(e) {
    DocumentApp.getUi().createAddonMenu()
        .addItem('Start', 'run')
        .addToUi();
}

/**
 * Opens a sidebar in the document containing the add-on's user interface.
 */
function run() {
    var ui = HtmlService.createTemplateFromFile('sidebar').evaluate()
        .setTitle(constants.title);

    DocumentApp.getUi().showSidebar(ui);
}

sidebar.html

<html>
<head>
<script>
  console.log("Hello world!");
</script>
</head>
</html>

This works, but it pops open the sidebar. If I comment out DocumentApp.getUi().showSidebar(ui);, then the page is never created or executed.

Context: I'd like to run some scripts that need to use basic APIs/DOM manipulation like window, document, etc. These don't run on serverside gs files. I want this to happen without having to open a sidebar.

7
  • Do a Logger.log( ui). Commented May 3, 2020 at 19:05
  • You can use the service to load data in to the html ouput and then use it in an email for htmlBody without ever having run an html page. Commented May 3, 2020 at 19:06
  • 1
    @lamblichus OP wants to run these to get something in the html.document.getElementById(...).value. Basically, He's looking for a html parser but with script capabilities-like puppeteer or selenium Commented May 4, 2020 at 11:35
  • 1
    I think a better way to go about this is to use a vitual dom like cheerio.js.org. Thanks everyone. Commented May 4, 2020 at 16:25
  • 2
    @Chanok Consider adding a answer Commented May 4, 2020 at 17:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.