Script Output Stream represents a stream that can be used to produce a file.
There are two types of output stream.
The standard output stream writes to a file, and is created using the Script Application method newOutputStream(). The stream can be saved to the file store by calling the getStorageKey() method, or can be read as an input stream using the toInputStream() method. Using toInputStream() creates a temporary file which is deleted when the input stream is closed.
The string output stream writes to a string, and is created using the Script Application method newStringOutputStream(). The resultant string can be read using toString(), or as an input stream using the toInputStream() method.
The methods raise exceptions if there are errors, and should be called within a try .. finally block to ensure the stream is closed, as in the following example.
var outputStream = application.newOutputStream();
var storageKey, size;
try {
outputStream.write('Here is some content');
outputStream.finish();
storageKey = outputStream.getStorageKey();
size = outputStream.getSize();
} finally {
outputStream.close();
}
write(text) | Write text to the output stream. |
stringify(object) | Write a stringified version of the object to the stream. Logically equivalent to write(application.stringify(object)). |
copyFrom(inputStream) | Copy all of the input stream to this output stream. |
finish() | Finish writing the output stream. |
string = getStorageKey() |
Get the file storage key associated with the stream. This is set only after finish(). If getStorageKey() is not called, the file is not commited to the file store. Not available for string output streams. |
double = getSize() |
Get the number of bytes. This is only set after finish(). Not available for string output streams. |
string = toString() |
If a string output stream is used in place of a standard output stream, retrieve the resultant string. This is set only after finish(). Not available for a standard output stream. |
inputStream = toInputStream() |
Return an input stream that can be used to read data written to the output stream. This must be called after finish(). For file-based output streams, this should not be used with getStorageKey(). The data is held in a temporary file, which is deleted when the input stream is closed. The following example shows how to use getInputStream() with finally clauses to close the output stream and the input stream. var outputStream = application.newOutputStream(); |
close() |
Close the output stream. |