google-sheetsgoogle-apps-script

Returning text from gsheet script


I have a Google Sheet containing a script. This script creates some text which should be:

My current solution, creating a new gdoc file with the text, (and opening it and copying the text to clipboard each time the script runs) is rather slow and creates plenty of docs which need to be deleted later.

I tried to write return ContentService.createPlainTextOutput(txt); in the gsheet script, but this results in error "TypeError: Cannot find function createPlainTextOutput in object ContentService".


Solution

  • Since Google Apps Script is executed on the server (Google servers), it has no access to your clipboard, or your file system. So those are not the options you are looking for.

    Presenting text output in a browser (outputting it to a web page) is possible, provided your script is run as a stand-alone web app, and not attached to a container like a Spreadsheet or Doc. In this case your server-side function should return the text back to the page, which you can then display in a div or another container using javascript.

    Other options, including what you are currently doing, are:

    The best option will depend on the amount of text you need to output, and any special requirements like formatting.

    Hope this helps!