I have a Google Sheet containing a script. This script creates some text which should be:
- copied to clipboard or
- presented in a web browser (and then I'll copy it to clipboard
manually) or
- saved to a specific file on hard drive (and then I'll open the file
and copy the text to clipboard)
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".
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:
- Write the text to a Google Doc or Spreadsheet. An alternative to what
you are currently doing - creating a Google Doc each time you run
your script - may be to append the text to the end of the same
document every time, maybe including a timestamp at the beginning of
each new addition as well (for easier searching/referencing).
However, as this document grows in size, this operation may run
slower and slower. Some sort of rotation of the document may be in
order in that case. If the amount of text is not too big, appending
it to a sheet in a designated spreadsheet may be an option, too (with
a timestamp in the next column for reference). Either way, this will
let you have a record of all the text output from your script, all in
one place.
- Send the text as email. Using GmailApp.sendEmail() method is an easy
way to get your text output. It is very fast if you use an Google
Apps email account under same domain as your script is running under.
Set up a filter in Gmail to apply a label to such emails. I do this a
lot in my apps.
- Use Logger to log the text output. Logger.log() is a quick way to
display the text output on screen. View the log under menu View->Log.
Keep in mind that Logger does have a limit on amount of text it can
display - don't know exactly what the limit is, but I have definitely
run into truncated output before (though it was A LOT of data I was
logging). Logs are rotated with every execution of the script, so if
you need to preserve the output text, you will have to copy/paste it
from the log to somewhere else.
The best option will depend on the amount of text you need to output, and any special requirements like formatting.
Hope this helps!